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() { }
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_;
36 Application(const Application &);
37 Application &operator=(const Application &);
39 Application(const std::string & = std::string());
41 virtual ~Application() { }
43 /** Constructs an instance of the registered application class and runs it.
44 If the application throws a usage_error, a help message is printed. The
45 GetOpt class will throw such exceptions automatically in error conditions.
47 This function can only be called once. The global main() function provided
48 by the library normally does it automatically at program startup. */
49 static int run(int, char **, void * =0, void (*)(void *) = 0);
51 /** Sets application startup info, including argv[0] value and platform-
54 This function can only be called once, and is normally called by
55 Application::run(). */
56 static void set_startup_info(const char *, void *);
58 static void *get_data() { return data_; }
59 static const char *get_argv0() { return argv0_; }
60 static const std::string &get_name() { return name_; }
63 /** Default main loop. Calls tick() repeatedly until exit() is called. A
64 custom main loop should monitor the done member variable and return
68 /** Sets the specified signal to be delivered to the sighandler member
70 void catch_signal(int);
72 /** Causes the application to exit gracefully with the given exit code. */
75 virtual void tick() { }
76 virtual void sighandler(int) { }
79 /** Static wrapper function to call a member function of the Application
81 static void sighandler_(int);
86 Registers the class to be used for program startup. The main application class
87 should be derived from this.
90 class RegisteredApplication: public Application
93 class Starter: public Application::Starter
96 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
99 static Starter starter_;
102 RegisteredApplication(const std::string &n = std::string()):
104 { (void)starter_; } // Force the starter into existence
108 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;