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