diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2015-04-03 00:48:56 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2015-04-03 01:07:01 +0200 |
commit | 82cc9c0cf2cdb8de17233a2ea943a5247d5da305 (patch) | |
tree | af341aed11c15ac3ab54de6c62e56251c859d76f | |
parent | 263ef7f8c7085b90f8d21e3dfc2598ed7340e73d (diff) | |
download | usdx-82cc9c0cf2cdb8de17233a2ea943a5247d5da305.tar.gz usdx-82cc9c0cf2cdb8de17233a2ea943a5247d5da305.tar.xz usdx-82cc9c0cf2cdb8de17233a2ea943a5247d5da305.zip |
menu/application: Remove singleton.
The singleton pattern should not be used in most cases, because it
behaves like global variables.
-rw-r--r-- | src/main.cpp | 4 | ||||
-rw-r--r-- | src/menu/application.cpp | 37 | ||||
-rw-r--r-- | src/menu/application.hpp | 13 | ||||
-rw-r--r-- | src/menu/mouse_manager.cpp | 7 | ||||
-rw-r--r-- | src/menu/mouse_manager.hpp | 5 | ||||
-rw-r--r-- | test/menu/application.cpp | 27 |
6 files changed, 36 insertions, 57 deletions
diff --git a/src/main.cpp b/src/main.cpp index 9e1ba84b..c9df9373 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,8 +27,8 @@ int main (int argc, char** argv) { - usdx::Application::get_instance()->run(); - usdx::Application::free(); + usdx::Application app; + app.run(); return EXIT_SUCCESS; } diff --git a/src/menu/application.cpp b/src/menu/application.cpp index 92e6b8a8..520ea27f 100644 --- a/src/menu/application.cpp +++ b/src/menu/application.cpp @@ -41,14 +41,19 @@ namespace usdx log4cpp::Category& Application::log = log4cpp::Category::getInstance("usdx.menu.application"); - Application* Application::instance = NULL; + Config* Application::config = NULL; - Application::Application(Container* parent) - : Container(parent), config(NULL), display(NULL), fps_manager(NULL), + boost::thread::id Application::gl_thread; + + Application::Application(void) + : Container(NULL), display(NULL), fps_manager(NULL), running(false), frames_per_second(50) { log4cpp::PropertyConfigurator::configure("log4cpp.property"); - config = new Config(); + + if (config == NULL) { + config = new Config(); + } set_size(config->get_graphics_resolution()); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); @@ -67,41 +72,23 @@ namespace usdx } SDL_Quit(); - - // reset instance to be able to recreate the singleton - instance = NULL; - } - - Application* Application::get_instance(void) - { - if (! instance) { - instance = new Application(NULL); - } - - return instance; - } - - void Application::free(void) - { - delete instance; - instance = NULL; } Config* Application::get_config(void) { - return get_instance()->config; + return config; } bool Application::is_gl_thread(void) { - return boost::this_thread::get_id() == get_instance()->gl_thread; + return boost::this_thread::get_id() == gl_thread; } void Application::main_loop(SDL_Surface* display) { SDL_Event event; EventManager event_manager; - MouseManager mouse_manager(event_manager); + MouseManager mouse_manager(this, event_manager); event_thread = boost::thread(boost::bind(&EventManager::handle_events, &event_manager)); SoftwareMousePointer pointer(this, &event_manager); diff --git a/src/menu/application.hpp b/src/menu/application.hpp index a47bd42a..6d1b2b44 100644 --- a/src/menu/application.hpp +++ b/src/menu/application.hpp @@ -28,6 +28,7 @@ #include <SDL/SDL.h> #include <SDL/SDL_framerate.h> #include <boost/thread/thread.hpp> +#include <boost/noncopyable.hpp> #include <log4cpp/Category.hh> #include <list> @@ -37,13 +38,12 @@ namespace usdx { - class Application : public Container + class Application : public Container, public boost::noncopyable { private: static log4cpp::Category& log; - static Application* instance; - Config* config; + static Config* config; SDL_Surface* display; FPSmanager* fps_manager; @@ -56,20 +56,17 @@ namespace usdx * This is the main loop. */ void main_loop(SDL_Surface* display); - boost::thread::id gl_thread; + static boost::thread::id gl_thread; boost::thread event_thread; void init_fps_manager(void); protected: - Application(Container* parent); public: + Application(void); virtual ~Application(); - static Application* get_instance(void); - static void free(void); - static Config* get_config(void); static bool is_gl_thread(void); diff --git a/src/menu/mouse_manager.cpp b/src/menu/mouse_manager.cpp index 3be6cb1b..db4db7ea 100644 --- a/src/menu/mouse_manager.cpp +++ b/src/menu/mouse_manager.cpp @@ -24,6 +24,7 @@ #include <cstddef> +#include "application.hpp" #include "mouse_manager.hpp" #include "utils/rectangle.hpp" #include "container.hpp" @@ -34,8 +35,8 @@ namespace usdx log4cpp::Category& MouseManager::log = log4cpp::Category::getInstance("usdx.menu.mouse_manager"); - MouseManager::MouseManager(EventManager& event_manager) - : last_active(NULL) + MouseManager::MouseManager(Application* app, EventManager& event_manager) + : app(app), last_active(NULL) { mouse_move_connection = event_manager.mouse_move.connect( boost::bind(&MouseManager::on_mouse_move, this, _1)); @@ -75,7 +76,7 @@ namespace usdx { if (current == NULL) { // start search from top level - return Application::get_instance(); + return app; } if (!current->get_absolut_rect().is_in(p)) { diff --git a/src/menu/mouse_manager.hpp b/src/menu/mouse_manager.hpp index 90e896c5..c977e85d 100644 --- a/src/menu/mouse_manager.hpp +++ b/src/menu/mouse_manager.hpp @@ -35,11 +35,14 @@ namespace usdx { + class Application; + class MouseManager { private: static log4cpp::Category& log; + Application *app; DrawableControl *last_active; MouseEvent event; @@ -55,7 +58,7 @@ namespace usdx void on_mouse_up(uint8_t button, const Point<int>& p); public: - MouseManager(EventManager& event_manager); + MouseManager(Application* app, EventManager& event_manager); virtual ~MouseManager(); /** diff --git a/test/menu/application.cpp b/test/menu/application.cpp index 20be3542..8a14644a 100644 --- a/test/menu/application.cpp +++ b/test/menu/application.cpp @@ -41,38 +41,29 @@ namespace usdx private: static log4cpp::Category& log; public: - void setUp() - { - Application::get_instance(); - } - - void tearDown() - { - Application::free(); - } - void testQuitBeforeRun() { Timestamp t; - Application::get_instance()->quit(); - CPPUNIT_ASSERT_EQUAL(false, Application::get_instance()->is_running()); + Application app; + app.quit(); + CPPUNIT_ASSERT_EQUAL(false, app.is_running()); CPPUNIT_ASSERT_EQUAL(true, t.since(0.2)); } void testNewInstance() { int fps; + Application app; + Application app2; // change fps - fps = Application::get_instance()->get_frames_per_second(); - Application::get_instance()->set_frames_per_second(fps + 23); + fps = app.get_frames_per_second(); + app.set_frames_per_second(fps + 23); - // reset instance - Application::free(); - // check if fps changed back - CPPUNIT_ASSERT_EQUAL(fps, Application::get_instance()->get_frames_per_second()); + // check if other instance is independent + CPPUNIT_ASSERT_EQUAL(fps, app2.get_frames_per_second()); } }; |