]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
Move most platform-specific code into overlay directories
[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 void *data_;
31
32         Application(const Application &);
33         Application &operator=(const Application &);
34 protected:
35         Application();
36 public:
37         virtual ~Application() { }
38
39         static int run(int, char **, void * =0);
40         static void *get_data() { return data_; }
41
42 protected:
43         virtual int main();
44         void catch_signal(int);
45         void exit(int);
46         virtual void tick() { }
47         virtual void sighandler(int) { }
48 private:
49         static void sighandler_(int);
50
51         static void display_exception(const std::exception &);
52 };
53
54
55 /**
56 Registers the class to be used for program startup.  The main application class
57 should be derived from this.
58 */
59 template<typename T>
60 class RegisteredApplication: public Application
61 {
62 private:
63         class Starter: public Application::Starter
64         {
65         public:
66                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
67         };
68
69         static Starter starter_;
70
71 protected:
72         // Force the starter into existence
73         RegisteredApplication() { (void)starter_; }
74 };
75
76 template<typename T>
77 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;
78
79 } // namespace Msp
80
81 #endif