]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
Eliminate loop mode from Application
[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 RegBase
21         {
22         public:
23                 virtual Application *create_app(int, char **)=0;
24                 virtual void usage(const char *, const char *, bool)=0;
25                 virtual ~RegBase() { }
26         protected:
27                 RegBase();
28         };
29
30         template<typename T>
31         class RegApp: public RegBase
32         {
33         public:
34                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
35                 void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); }
36         };
37
38         bool done;
39         int exit_code;
40
41 private:
42         static RegBase *reg_app_;
43         static Application *app_;
44         static void *data_;
45
46 protected:
47         Application();
48 public:
49         virtual ~Application() { }
50
51         static int run(int, char **, void * =0);
52         static void usage(const char *, const char *, bool);
53         static void *get_data() { return data_; }
54
55 protected:
56         virtual int main();
57         void catch_signal(int);
58         void exit(int);
59         virtual void tick() { }
60         virtual void sighandler(int) { }
61 private:
62         static void sighandler_(int);
63
64         Application(const Application &);
65         Application &operator=(const Application &);
66 };
67
68 } // namespace Msp
69
70 #endif