1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
6 #include "noncopyable.h"
11 Base class for applications. See also RegisteredApplication.
13 class Application: private NonCopyable
21 virtual ~Starter() = default;
23 virtual Application *create_app(int, char **) = 0;
30 static Starter *_starter;
31 static Application *_app;
32 static const char *_argv0;
33 static std::string _name;
37 Application(const std::string & = std::string());
39 virtual ~Application() = default;
41 /** Constructs an instance of the registered application class and runs it.
42 If the application throws a usage_error, a help message is printed. The
43 GetOpt class will throw such exceptions automatically in error conditions.
45 This function can only be called once. The global main() function provided
46 by the library normally does it automatically at program startup. */
47 static int run(int, char **, void * = nullptr, void (*)(void *) = nullptr);
49 /** Sets application startup info, including argv[0] value and platform-
52 This function can only be called once, and is normally called by
53 Application::run(). */
54 static void set_startup_info(const char *, void *);
56 static void *get_data() { return _data; }
57 static const char *get_argv0() { return _argv0; }
58 static const std::string &get_name() { return _name; }
61 /** Default main loop. Calls tick() repeatedly until exit() is called. A
62 custom main loop should monitor the done member variable and return
66 /** Sets the specified signal to be delivered to the sighandler member
68 void catch_signal(int);
70 /** Causes the application to exit gracefully with the given exit code. */
73 virtual void tick() { }
74 virtual void sighandler(int) { }
77 /** Static wrapper function to call a member function of the Application
79 static void _sighandler(int);
84 Registers the class to be used for program startup. The main application class
85 should be derived from this.
88 class RegisteredApplication: public Application
91 class Starter: public Application::Starter
94 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
97 static Starter _starter;
100 RegisteredApplication(const std::string &n = std::string()):
102 { (void)_starter; } // Force the starter into existence
106 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::_starter;