From: Mikko Rasa Date: Sun, 19 Oct 2014 22:24:43 +0000 (+0300) Subject: Store argv0 and application name in the Application class X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=231482c618cb2b30e1f7194d8d1d3ea30bc94512 Store argv0 and application name in the Application class It can get quite annoying having to pass them around to be fed to the standard path retrieval functions. --- diff --git a/source/core/application.cpp b/source/core/application.cpp index 3a5561e..f4887e7 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "application.h" #include "getopt.h" @@ -12,13 +13,20 @@ namespace Msp { Application *Application::app_ = 0; Application::Starter *Application::starter_ = 0; +const char *Application::argv0_ = 0; +string Application::name_; void *Application::data_ = 0; -Application::Application(): +Application::Application(const string &n): exit_code(0) { if(app_) throw logic_error("instance already exists"); + + if(!n.empty()) + name_ = n; + else + name_ = FS::basename(argv0_); } int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *)) @@ -29,6 +37,7 @@ int Application::run(int argc, char **argv, void *data, void (*created_callback) return 126; } + argv0_ = argv[0]; data_ = data; try diff --git a/source/core/application.h b/source/core/application.h index af18cea..35ab873 100644 --- a/source/core/application.h +++ b/source/core/application.h @@ -27,12 +27,14 @@ protected: private: static Starter *starter_; static Application *app_; + static const char *argv0_; + static std::string name_; static void *data_; Application(const Application &); Application &operator=(const Application &); protected: - Application(); + Application(const std::string & = std::string()); public: virtual ~Application() { } @@ -45,6 +47,8 @@ public: static int run(int, char **, void * =0, void (*)(void *) = 0); static void *get_data() { return data_; } + static const char *get_argv0() { return argv0_; } + static const std::string &get_name() { return name_; } protected: /** Default main loop. Calls tick() repeatedly until exit() is called. A @@ -86,8 +90,9 @@ private: static Starter starter_; protected: - // Force the starter into existence - RegisteredApplication() { (void)starter_; } + RegisteredApplication(const std::string &n = std::string()): + Application(n) + { (void)starter_; } // Force the starter into existence }; template