]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
Merge branch 'strings-master'
[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                 virtual void usage(const char *, const char *, bool) = 0;
29         };
30
31         bool done;
32         int exit_code;
33
34 private:
35         static Starter *starter_;
36         static Application *app_;
37         static void *data_;
38
39         Application(const Application &);
40         Application &operator=(const Application &);
41 protected:
42         Application();
43 public:
44         virtual ~Application() { }
45
46         static int run(int, char **, void * =0);
47         static void usage(const char *, const char *, bool);
48         static void *get_data() { return data_; }
49
50 protected:
51         virtual int main();
52         void catch_signal(int);
53         void exit(int);
54         virtual void tick() { }
55         virtual void sighandler(int) { }
56 private:
57         static void sighandler_(int);
58 };
59
60
61 template<typename T>
62 class RegisteredApplication: public Application
63 {
64 private:
65         class Starter: public Application::Starter
66         {
67         public:
68                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
69                 void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); }
70         };
71
72         static Starter starter_;
73
74 protected:
75         // Force the starter into existence
76         RegisteredApplication() { (void)starter_; }
77 };
78
79 template<typename T>
80 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;
81
82 } // namespace Msp
83
84 #endif