1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
9 Base class for applications. See also RegisteredApplication.
19 virtual ~Starter() { }
21 virtual Application *create_app(int, char **) = 0;
28 static Starter *starter_;
29 static Application *app_;
30 static const char *argv0_;
31 static std::string name_;
34 Application(const Application &);
35 Application &operator=(const Application &);
37 Application(const std::string & = std::string());
39 virtual ~Application() { }
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 * =0, void (*)(void *) = 0);
49 static void *get_data() { return data_; }
50 static const char *get_argv0() { return argv0_; }
51 static const std::string &get_name() { return name_; }
54 /** Default main loop. Calls tick() repeatedly until exit() is called. A
55 custom main loop should monitor the done member variable and return
59 /** Sets the specified signal to be delivered to the sighandler member
61 void catch_signal(int);
63 /** Causes the application to exit gracefully with the given exit code. */
66 virtual void tick() { }
67 virtual void sighandler(int) { }
70 /** Static wrapper function to call a member function of the Application
72 static void sighandler_(int);
77 Registers the class to be used for program startup. The main application class
78 should be derived from this.
81 class RegisteredApplication: public Application
84 class Starter: public Application::Starter
87 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
90 static Starter starter_;
93 RegisteredApplication(const std::string &n = std::string()):
95 { (void)starter_; } // Force the starter into existence
99 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;