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