1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
10 Base class for applications. See also RegisteredApplication.
20 virtual ~Starter() { }
22 virtual Application *create_app(int, char **) = 0;
29 static Starter *starter_;
30 static Application *app_;
31 static const char *argv0_;
32 static std::string name_;
35 Application(const Application &);
36 Application &operator=(const Application &);
38 Application(const std::string & = std::string());
40 virtual ~Application() { }
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 * =0, void (*)(void *) = 0);
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() { return data_; }
58 static const char *get_argv0() { return argv0_; }
59 static const std::string &get_name() { return 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_;