]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
4ae8c5614250819ec6aa28ed83c7cc6461f7ee69
[libs/core.git] / source / core / application.h
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006-2008, 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_CORE_APPLICATION_H_
9 #define MSP_CORE_APPLICATION_H_
10
11 namespace Msp {
12
13 /**
14 Base class for applications.  Inherit the main class from this and add a static
15 member of type RegApp<MainClass>.
16 */
17 class Application
18 {
19 protected:
20         class Starter
21         {
22         protected:
23                 Starter();
24         public:
25                 virtual ~Starter() { }
26
27                 virtual Application *create_app(int, char **) = 0;
28         };
29
30         bool done;
31         int exit_code;
32
33 private:
34         static Starter *starter_;
35         static Application *app_;
36         static void *data_;
37
38         Application(const Application &);
39         Application &operator=(const Application &);
40 protected:
41         Application();
42 public:
43         virtual ~Application() { }
44
45         static int run(int, char **, void * =0);
46         static void *get_data() { return data_; }
47
48 protected:
49         virtual int main();
50         void catch_signal(int);
51         void exit(int);
52         virtual void tick() { }
53         virtual void sighandler(int) { }
54 private:
55         static void sighandler_(int);
56 };
57
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