1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
6 #include "mspcore_api.h"
7 #include "noncopyable.h"
12 Base class for applications. See also RegisteredApplication.
14 class MSPCORE_API Application: private NonCopyable
17 class MSPCORE_API Starter
22 virtual ~Starter() = default;
24 virtual Application *create_app(int, char **) = 0;
31 static Starter *_starter;
32 static Application *_app;
33 static const char *_argv0;
34 static std::string _name;
38 Application(const std::string & = std::string());
40 virtual ~Application() = default;
42 /** Constructs an instance of the registered application class and runs it.
43 If the application throws a usage_error, a help message is printed. The
44 GetOpt class will throw such exceptions automatically in error conditions.
46 This function can only be called once. The global main() function provided
47 by the library normally does it automatically at program startup. */
48 static int run(int, char **, void * = nullptr, void (*)(void *) = nullptr);
50 /** Sets application startup info, including argv[0] value and platform-
53 This function can only be called once, and is normally called by
54 Application::run(). */
55 static void set_startup_info(const char *, void *);
57 static void *get_data();
58 static const char *get_argv0();
59 static const std::string &get_name();
62 /** Default main loop. Calls tick() repeatedly until exit() is called. A
63 custom main loop should monitor the done member variable and return
67 /** Sets the specified signal to be delivered to the sighandler member
69 void catch_signal(int);
71 /** Causes the application to exit gracefully with the given exit code. */
74 virtual void tick() { }
75 virtual void sighandler(int) { }
78 /** Static wrapper function to call a member function of the Application
80 static void _sighandler(int);
85 Registers the class to be used for program startup. The main application class
86 should be derived from this.
89 class RegisteredApplication: public Application
92 class Starter: public Application::Starter
95 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
98 static Starter _starter;
101 RegisteredApplication(const std::string &n = std::string()):
103 { (void)_starter; } // Force the starter into existence
107 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::_starter;