changeset 2866:6ea67d0809eb

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.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 17 Dec 2022 11:51:22 +0000
parents fd32dce7fecd
children ada74f4d3f39
files dw.hpp dwtestoo.cpp
diffstat 2 files changed, 19 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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.hpp>
 
-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();