]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
fa0c9c41bcf42a7923237b21cdcc410233599226
[libs/core.git] / source / core / application.h
1 /*
2 This file is part of libmspcore
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #ifndef MSP_FRAMEWORK_APPLICATION_H_
7 #define MSP_FRAMEWORK_APPLICATION_H_
8
9 #include "semaphore.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 public:
20         virtual ~Application() { }
21
22         static int run(int, char **);
23         static void usage(const char *, const char *, bool);
24 protected:
25         enum LoopMode
26         {
27                 NONE,       /// No main loop - main() will just return
28                 SLEEP,      /// Only sleep in the main loop - useful for servers
29                 TICK_SLEEP, /// Call tick every iteration, with a short sleep in between
30                 TICK_YIELD  /// Call tick every iteration, with sched_yield in between
31         };
32         
33         class RegBase
34         {
35         public:
36                 virtual Application *create_app(int, char **)=0;
37                 virtual void usage(const char *, const char *, bool)=0;
38                 virtual ~RegBase() { }
39         protected:
40                 RegBase();
41         };
42
43         template<typename T>
44         class RegApp: public RegBase
45         {
46         public:
47                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
48                 void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); }
49         };
50
51         bool done;
52         int  exit_code;
53
54         Application();
55         virtual int main();
56         void catch_signal(int);
57         void set_loop_mode(LoopMode);
58         void induce_tick();
59         void exit(int);
60         virtual void tick() { }
61         virtual void sighandler(int) { }
62 private:
63         LoopMode     loop_mode_;
64         Semaphore    sleep_sem_;
65
66         Application(const Application &);
67         Application &operator=(const Application &);
68
69         static RegBase     *reg_app_;
70         static Application *app_;
71
72         static void sighandler_(int);
73         static void sigalrm_(int) { }
74 };
75
76 } // namespace Msp
77
78 #endif