]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
Make sure all files have the correct header
[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 protected:
21         enum LoopMode
22         {
23                 NONE,       /// No main loop - main() will just return
24                 SLEEP,      /// Only sleep in the main loop - useful for threaded servers
25                 TICK_SLEEP, /// Call tick every iteration, with a short sleep in between
26                 TICK_YIELD, /// Call tick every iteration, with sched_yield in between
27                 TICK_BUSY   /// Call tick every iteration
28         };
29
30         class RegBase
31         {
32         public:
33                 virtual Application *create_app(int, char **)=0;
34                 virtual void usage(const char *, const char *, bool)=0;
35                 virtual ~RegBase() { }
36         protected:
37                 RegBase();
38         };
39
40         template<typename T>
41         class RegApp: public RegBase
42         {
43         public:
44                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
45                 void usage(const char *r, const char *a, bool b) { T::usage(r, a, b); }
46         };
47
48         bool done;
49         int exit_code;
50
51 private:
52         LoopMode loop_mode_;
53         Semaphore sleep_sem_;
54
55         static RegBase *reg_app_;
56         static Application *app_;
57         static void *data_;
58
59 protected:
60         Application();
61 public:
62         virtual ~Application() { }
63
64         static int run(int, char **, void * =0);
65         static void usage(const char *, const char *, bool);
66         static void *get_data() { return data_; }
67
68 protected:
69         virtual int main();
70         void catch_signal(int);
71         void set_loop_mode(LoopMode);
72         void induce_tick();
73         void exit(int);
74         virtual void tick() { }
75         virtual void sighandler(int) { }
76 private:
77         static void sighandler_(int);
78
79         Application(const Application &);
80         Application &operator=(const Application &);
81 };
82
83 } // namespace Msp
84
85 #endif