]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
6b3ec1aa65bdd98b2877440a3f7a2f6e668d191a
[libs/core.git] / source / core / application.h
1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
3
4 #include <stdexcept>
5
6 namespace Msp {
7
8 /**
9 Base class for applications.  See also RegisteredApplication.
10 */
11 class Application
12 {
13 protected:
14         class Starter
15         {
16         protected:
17                 Starter();
18         public:
19                 virtual ~Starter() { }
20
21                 virtual Application *create_app(int, char **) = 0;
22         };
23
24         bool done;
25         int exit_code;
26
27 private:
28         static Starter *starter_;
29         static Application *app_;
30         static const char *argv0_;
31         static std::string name_;
32         static void *data_;
33
34         Application(const Application &);
35         Application &operator=(const Application &);
36 protected:
37         Application(const std::string & = std::string());
38 public:
39         virtual ~Application() { }
40
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.
44
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);
48
49         /** Sets application startup info, including argv[0] value and platform-
50         specific data.
51
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 *);
55
56         static void *get_data() { return data_; }
57         static const char *get_argv0() { return argv0_; }
58         static const std::string &get_name() { return name_; }
59
60 protected:
61         /** Default main loop.  Calls tick() repeatedly until exit() is called.  A
62         custom main loop should monitor the done member variable and return
63         exit_code. */
64         virtual int main();
65
66         /** Sets the specified signal to be delivered to the sighandler member
67         function. */
68         void catch_signal(int);
69
70         /** Causes the application to exit gracefully with the given exit code. */
71         void exit(int);
72
73         virtual void tick() { }
74         virtual void sighandler(int) { }
75
76 private:
77         /** Static wrapper function to call a member function of the Application
78         instance. */
79         static void sighandler_(int);
80 };
81
82
83 /**
84 Registers the class to be used for program startup.  The main application class
85 should be derived from this.
86 */
87 template<typename T>
88 class RegisteredApplication: public Application
89 {
90 private:
91         class Starter: public Application::Starter
92         {
93         public:
94                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
95         };
96
97         static Starter starter_;
98
99 protected:
100         RegisteredApplication(const std::string &n = std::string()):
101                 Application(n)
102         { (void)starter_; }  // Force the starter into existence
103 };
104
105 template<typename T>
106 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;
107
108 } // namespace Msp
109
110 #endif