From 1876f64c4ec1bcba8de57b8b5f63e250187b4ad1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 23 May 2011 21:20:23 +0300 Subject: [PATCH] Streamline application class registration --- source/core/application.cpp | 19 +++++++---------- source/core/application.h | 42 ++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/source/core/application.cpp b/source/core/application.cpp index 17611c1..a588e9d 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -40,9 +40,9 @@ int Application::run(int argc, char **argv, void *data) } called=true; - if(!reg_app_) + if(!starter_) { - cerr<<"Trying to run with no application class registered!\n"; + cerr<<"Trying to run with no RegisteredApplication class!\n"; return 126; } @@ -52,11 +52,11 @@ int Application::run(int argc, char **argv, void *data) { try { - app_=reg_app_->create_app(argc, argv); + app_=starter_->create_app(argc, argv); } catch(const UsageError &e) { - reg_app_->usage(e.what(), argv[0], e.get_brief()); + starter_->usage(e.what(), argv[0], e.get_brief()); return 1; } @@ -144,15 +144,12 @@ void Application::sighandler_(int s) } -Application::RegBase::RegBase() +Application::Starter::Starter() { - if(reg_app_) - { - cerr<<"Warning: registering the application twice\n"; - delete reg_app_; - } + if(starter_) + throw InvalidState("Can't create more than one Starter instance"); - reg_app_=this; + starter_=this; } Application *Application::app_=0; diff --git a/source/core/application.h b/source/core/application.h index 38df732..305fbfe 100644 --- a/source/core/application.h +++ b/source/core/application.h @@ -17,29 +17,22 @@ member of type RegApp. class Application { protected: - class RegBase + class Starter { - public: - virtual Application *create_app(int, char **)=0; - virtual void usage(const char *, const char *, bool)=0; - virtual ~RegBase() { } protected: - RegBase(); - }; - - template - class RegApp: public RegBase - { + Starter(); public: - Application *create_app(int argc, char **argv) { return new T(argc, argv); } - void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); } + virtual ~Starter() { } + + virtual Application *create_app(int, char **) = 0; + virtual void usage(const char *, const char *, bool) = 0; }; bool done; int exit_code; private: - static RegBase *reg_app_; + static Starter *starter_; static Application *app_; static void *data_; @@ -60,11 +53,32 @@ protected: virtual void sighandler(int) { } private: static void sighandler_(int); +}; Application(const Application &); Application &operator=(const Application &); + +template +class RegisteredApplication: public Application +{ +private: + class Starter: public Application::Starter + { + public: + Application *create_app(int argc, char **argv) { return new T(argc, argv); } + void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); } + }; + + static Starter starter_; + +protected: + // Force the starter into existence + RegisteredApplication() { (void)starter_; } }; +template +typename RegisteredApplication::Starter RegisteredApplication::starter_; + } // namespace Msp #endif -- 2.43.0