# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1671277882 0 # Node ID 6ea67d0809eb44787f69d83c495d7afa1f6d08ad # Parent fd32dce7fecd72656a0ae05e9b1a6766893d87c8 Convert DW::App class into a singleton so subsequent DW::App::Init() calls will return a handle to our instance once it is already initialized. diff -r fd32dce7fecd -r 6ea67d0809eb dw.hpp --- a/dw.hpp Sat Dec 17 09:03:32 2022 +0000 +++ b/dw.hpp Sat Dec 17 11:51:22 2022 +0000 @@ -61,13 +61,21 @@ class App { +protected: + App() { } + static App *_app; public: - App() { dw_init(TRUE, 0, NULL); } - App(const char *appid) { dw_app_id_set(appid, NULL); dw_init(TRUE, 0, NULL); } - App(const char *appid, const char *appname) { dw_app_id_set(appid, appname); dw_init(TRUE, 0, NULL); } - App(int argc, char *argv[]) { dw_init(TRUE, argc, argv); } - App(int argc, char *argv[], const char *appid) { dw_app_id_set(appid, NULL); dw_init(TRUE, argc, argv); } - App(int argc, char *argv[], const char *appid, const char *appname) { dw_app_id_set(appid, appname); dw_init(TRUE, argc, argv); } + // Singletons should not be cloneable. + App(App &other) = delete; + // Singletons should not be assignable. + void operator=(const App &) = delete; + // Initialization functions for creating App + static App *Init() { if(!_app) { _app = new App; dw_init(TRUE, 0, NULL); } return _app; } + static App *Init(const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, NULL); dw_init(TRUE, 0, NULL); } return _app; } + static App *Init(const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, 0, NULL); } return _app; } + static App *Init(int argc, char *argv[]) { if(!_app) { _app = new App(); dw_init(TRUE, argc, argv); } return _app; } + static App *Init(int argc, char *argv[], const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, NULL); dw_init(TRUE, argc, argv); } return _app; } + static App *Init(int argc, char *argv[], const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, argc, argv); } return _app; } void Main() { dw_main(); } void MainIteration() { dw_main_iteration(); } @@ -75,6 +83,9 @@ void Exit(int exitcode) { dw_exit(exitcode); } }; +// Static singleton reference declared outside of the class +App* App::_app = nullptr; + #if 0 // Class that allows drawing, either to screen or picture (pixmap) class Drawable diff -r fd32dce7fecd -r 6ea67d0809eb dwtestoo.cpp --- a/dwtestoo.cpp Sat Dec 17 09:03:32 2022 +0000 +++ b/dwtestoo.cpp Sat Dec 17 11:51:22 2022 +0000 @@ -1,7 +1,5 @@ #include -DW::App *app; - class MyWindow : public DW::Window { public: @@ -10,13 +8,13 @@ SetSize(200, 200); } protected: - virtual int OnDelete() { app->MainQuit(); return FALSE; } + virtual int OnDelete() { DW::App *app = DW::App::Init(); app->MainQuit(); return FALSE; } virtual int OnConfigure(int width, int height) { return FALSE; } }; int dwmain(int argc, char* argv[]) { - app = new DW::App(argc, argv, "org.dbsoft.dwindows.dwtestoo"); + DW::App *app = DW::App::Init(argc, argv, "org.dbsoft.dwindows.dwtestoo"); MyWindow *window = new MyWindow(); window->Show();