Switched from the service locator pattern to singleton pattern
This commit is contained in:
+5
-5
@@ -25,7 +25,7 @@
|
||||
#include "base_scene.hpp"
|
||||
#include "utilities.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
@@ -65,10 +65,10 @@ protected:
|
||||
void HandleDisconnection(::Disconnect&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = ServiceLocator<InformationManager>::Get();
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||
|
||||
//members
|
||||
RasterFont font;
|
||||
|
||||
+5
-5
@@ -25,7 +25,7 @@
|
||||
#include "base_scene.hpp"
|
||||
#include "utilities.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
@@ -72,10 +72,10 @@ protected:
|
||||
void BeginGame(JoinResponse&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = ServiceLocator<InformationManager>::Get();
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||
|
||||
//members
|
||||
Button refreshButton;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#define MAINMENU_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "surface_manager.hpp"
|
||||
#include "button.hpp"
|
||||
@@ -47,7 +47,7 @@ protected:
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
|
||||
Button startButton;
|
||||
Button optionsButton;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#define OPTIONSCREEN_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "surface_manager.hpp"
|
||||
#include "button.hpp"
|
||||
@@ -44,7 +44,7 @@ protected:
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
Button backButton;
|
||||
};
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ SceneManager::~SceneManager() {
|
||||
* operations.
|
||||
* Important things to note:
|
||||
* The APIs are initiated here.
|
||||
* The global objects are created here.
|
||||
* The global objects are initialized here.
|
||||
* The game's screen is created here, based on information loaded from the config file.
|
||||
* The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it.
|
||||
*/
|
||||
@@ -62,7 +62,6 @@ SceneManager::~SceneManager() {
|
||||
void SceneManager::Init() {
|
||||
//load the config file
|
||||
try {
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(new ConfigUtility());
|
||||
configUtil->Load("rsc/config.cfg");
|
||||
}
|
||||
catch(std::runtime_error& e) {
|
||||
@@ -89,12 +88,7 @@ void SceneManager::Init() {
|
||||
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
|
||||
flags);
|
||||
|
||||
//instanciate the remaining services
|
||||
surfaceMgr = ServiceLocator<SurfaceManager>::Set(new SurfaceManager());
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(new UDPNetworkUtility());
|
||||
infoMgr = ServiceLocator<InformationManager>::Set(new InformationManager());
|
||||
|
||||
//initiate the remaining services
|
||||
//initiate the remaining singletons
|
||||
netUtil->Open(0, sizeof(Packet));
|
||||
}
|
||||
|
||||
@@ -137,16 +131,10 @@ void SceneManager::Proc() {
|
||||
}
|
||||
|
||||
void SceneManager::Quit() {
|
||||
//clean up the services
|
||||
//clean up the singletons
|
||||
netUtil->Close();
|
||||
surfaceMgr->FreeAll();
|
||||
|
||||
//delete the services
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(nullptr);
|
||||
surfaceMgr = ServiceLocator<SurfaceManager>::Set(nullptr);
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(nullptr);
|
||||
infoMgr = ServiceLocator<InformationManager>::Set(nullptr);
|
||||
|
||||
//clean up the scene
|
||||
UnloadScene();
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
@@ -51,10 +51,10 @@ private:
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
SurfaceManager* surfaceMgr = nullptr;
|
||||
UDPNetworkUtility* netUtil = nullptr;
|
||||
InformationManager* infoMgr = nullptr;
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#define SPLASHSCREEN_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
@@ -44,8 +44,8 @@ protected:
|
||||
void LoadResources();
|
||||
|
||||
bool loaded = false;
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
Image logo;
|
||||
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
* distribution.
|
||||
*/
|
||||
#include "network_queue.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
@@ -37,7 +37,7 @@ static std::deque<Packet> queue;
|
||||
static bool running = false;
|
||||
|
||||
static int networkQueue(void*) {
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
while(running) {
|
||||
SDL_SemWait(lock);
|
||||
while(netUtil->Receive()) {
|
||||
@@ -102,7 +102,7 @@ Packet popNetworkPacket() {
|
||||
}
|
||||
|
||||
void flushNetworkQueue() {
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
SDL_SemWait(lock);
|
||||
while(netUtil->Receive());
|
||||
queue.clear();
|
||||
|
||||
@@ -19,24 +19,20 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SERVICELOCATOR_HPP_
|
||||
#define SERVICELOCATOR_HPP_
|
||||
#ifndef SINGLETON_HPP_
|
||||
#define SINGLETON_HPP_
|
||||
|
||||
template<typename T>
|
||||
class ServiceLocator {
|
||||
class Singleton {
|
||||
public:
|
||||
static T* Set(T* t) {
|
||||
delete service;
|
||||
return service = t;
|
||||
}
|
||||
static T* Get() {
|
||||
return service;
|
||||
return &instance;
|
||||
}
|
||||
private:
|
||||
static T* service;
|
||||
static T instance;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T* ServiceLocator<T>::service = nullptr;
|
||||
T Singleton<T>::instance;
|
||||
|
||||
#endif
|
||||
@@ -44,14 +44,13 @@ ServerApplication::~ServerApplication() {
|
||||
* operations.
|
||||
* Important things to note:
|
||||
* The APIs are initiated here.
|
||||
* The global objects are created here.
|
||||
* The global objects are initialized here.
|
||||
* The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it.
|
||||
*/
|
||||
|
||||
void ServerApplication::Init() {
|
||||
//load the config file
|
||||
try {
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(new ConfigUtility());
|
||||
configUtil->Load("rsc/config.cfg");
|
||||
}
|
||||
catch(std::runtime_error& e) {
|
||||
@@ -70,10 +69,7 @@ void ServerApplication::Init() {
|
||||
throw(runtime_error("Failed to initialize SDL_net"));
|
||||
}
|
||||
|
||||
//instanciate the remaining services
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(new UDPNetworkUtility());
|
||||
|
||||
//initiate the remaining services
|
||||
//initiate the remaining singletons
|
||||
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet));
|
||||
|
||||
//create the threads
|
||||
@@ -107,13 +103,9 @@ void ServerApplication::Quit() {
|
||||
//close the threads
|
||||
endQueueThread();
|
||||
|
||||
//clean up the services
|
||||
//clean up the singletons
|
||||
netUtil->Close();
|
||||
|
||||
//delete the services
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(nullptr);
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(nullptr);
|
||||
|
||||
//deinitialize the APIs
|
||||
SDLNet_Quit();
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#include "utilities.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "network_queue.hpp"
|
||||
|
||||
#include "config_Utility.hpp"
|
||||
@@ -79,8 +79,8 @@ private:
|
||||
void HandleDisconnection(Disconnect&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
UDPNetworkUtility* netUtil = nullptr;
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
|
||||
//members
|
||||
Clock::time_point lastTick = Clock::now();
|
||||
|
||||
+1
-5
@@ -1,5 +1 @@
|
||||
#include "service_locator.hpp"
|
||||
|
||||
int FooBar() {
|
||||
return *ServiceLocator<int>::Get();
|
||||
}
|
||||
#include "foobar.hpp"
|
||||
|
||||
Reference in New Issue
Block a user