]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
08393b724b486077452e9ffb940bd9822fc6578c
[libs/core.git] / source / core / application.h
1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
3
4 namespace Msp {
5
6 /**
7 Base class for applications.  See also RegisteredApplication.
8 */
9 class Application
10 {
11 protected:
12         class Starter
13         {
14         protected:
15                 Starter();
16         public:
17                 virtual ~Starter() { }
18
19                 virtual Application *create_app(int, char **) = 0;
20         };
21
22         bool done;
23         int exit_code;
24
25 private:
26         static Starter *starter_;
27         static Application *app_;
28         static void *data_;
29
30         Application(const Application &);
31         Application &operator=(const Application &);
32 protected:
33         Application();
34 public:
35         virtual ~Application() { }
36
37         static int run(int, char **, void * =0);
38         static void *get_data() { return data_; }
39
40 protected:
41         virtual int main();
42         void catch_signal(int);
43         void exit(int);
44         virtual void tick() { }
45         virtual void sighandler(int) { }
46 private:
47         static void sighandler_(int);
48 };
49
50
51 /**
52 Registers the class to be used for program startup.  The main application class
53 should be derived from this.
54 */
55 template<typename T>
56 class RegisteredApplication: public Application
57 {
58 private:
59         class Starter: public Application::Starter
60         {
61         public:
62                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
63         };
64
65         static Starter starter_;
66
67 protected:
68         // Force the starter into existence
69         RegisteredApplication() { (void)starter_; }
70 };
71
72 template<typename T>
73 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;
74
75 } // namespace Msp
76
77 #endif