]> git.tdb.fi Git - libs/core.git/blobdiff - source/application.h
Add files
[libs/core.git] / source / application.h
diff --git a/source/application.h b/source/application.h
new file mode 100644 (file)
index 0000000..4296b6f
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+This file is part of libmspframework
+Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+#ifndef MSP_FRAMEWORK_APPLICATION_H_
+#define MSP_FRAMEWORK_APPLICATION_H_
+
+#include <pthread.h>
+#include "event.h"
+#include "poller.h"
+
+namespace Msp {
+
+/**
+Base class for applications.  Inherit the main class from this and add a static
+member of type RegApp<MainClass>.
+*/
+class Application
+{
+public:
+       Poller::Slot &add_pollable(Pollable *, short);
+       EventManager::Event &create_event();
+       virtual ~Application();
+
+       static int run(int, char **);
+       static void usage(const char *, bool);
+protected:
+       enum TickMode
+       {
+               NONE,       /// No ticks
+               AFTER_POLL, /// One tick after each poll
+               IDLE        /// Constant torrent of ticks
+       };
+       
+       class RegBase
+       {
+       public:
+               virtual Application *create_app(int, char **)=0;
+               virtual void usage(const char *, bool)=0;
+               virtual ~RegBase() { }
+       protected:
+               RegBase();
+       };
+
+       template<typename T>
+       class RegApp: public RegBase
+       {
+       public:
+               Application *create_app(int argc, char **argv) { return new T(argc, argv); }
+               void usage(const char *a, bool b) { T::usage(a,b); }
+       };
+
+       bool done;
+       int  exit_code;
+
+       Application(): exit_code(0), tick_mode_(IDLE), poller_(0), ev_mgr_(0), main_tid(pthread_self()) { }
+       virtual int main();
+       void catch_signal(int);
+       void set_tick_mode(TickMode);
+       void exit(int);
+       virtual void tick() { }
+       virtual void sighandler(int) { }
+private:
+       TickMode     tick_mode_;
+       Poller       *poller_;
+       EventManager *ev_mgr_;
+       pthread_t    main_tid;
+
+       Application(const Application &);
+       Application &operator=(const Application &);
+
+       static RegBase *reg_app_;
+       static Application *app_;
+
+       static void sighandler_(int);
+       static void sigalrm_(int) { }
+};
+
+} // namespace Msp
+
+#endif