]> git.tdb.fi Git - libs/core.git/blob - source/application.h
Win32 tweaks
[libs/core.git] / source / application.h
1 /*
2 This file is part of libmspframework
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 #ifdef WIN32
10 #include "win32signum.h"
11 #endif
12
13 #include <pthread.h>
14 #include "event.h"
15 #include "poller.h"
16
17 namespace Msp {
18
19 /**
20 Base class for applications.  Inherit the main class from this and add a static
21 member of type RegApp<MainClass>.
22 */
23 class Application
24 {
25 public:
26         Poller::Slot &add_pollable(Pollable *, short);
27         EventManager::Event &create_event();
28         virtual ~Application();
29
30         static int run(int, char **);
31         static void usage(const char *, bool);
32 protected:
33         enum TickMode
34         {
35                 NONE,       /// No ticks
36                 AFTER_POLL, /// One tick after each poll
37                 IDLE        /// Constant torrent of ticks
38         };
39         
40         class RegBase
41         {
42         public:
43                 virtual Application *create_app(int, char **)=0;
44                 virtual void usage(const char *, bool)=0;
45                 virtual ~RegBase() { }
46         protected:
47                 RegBase();
48         };
49
50         template<typename T>
51         class RegApp: public RegBase
52         {
53         public:
54                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
55                 void usage(const char *a, bool b) { T::usage(a,b); }
56         };
57
58         bool done;
59         int  exit_code;
60
61         Application(): exit_code(0), tick_mode_(IDLE), poller_(0), ev_mgr_(0), main_tid(pthread_self()) { }
62         virtual int main();
63         void catch_signal(int);
64         void set_tick_mode(TickMode);
65         void exit(int);
66         virtual void tick() { }
67         virtual void sighandler(int) { }
68 private:
69         TickMode     tick_mode_;
70         Poller       *poller_;
71         EventManager *ev_mgr_;
72         pthread_t    main_tid;
73
74         Application(const Application &);
75         Application &operator=(const Application &);
76
77         static RegBase *reg_app_;
78         static Application *app_;
79
80         static void sighandler_(int);
81         static void sigalrm_(int) { }
82 };
83
84 } // namespace Msp
85
86 #endif