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