X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fapplication.h;fp=source%2Fapplication.h;h=4296b6f1b6811db6cf9354516297e3872c5973bb;hp=0000000000000000000000000000000000000000;hb=1013e3c216cdf8e0ecc0f3b1e8314989b5333818;hpb=5780f0826f057f99593de46a583be7c098efaf90 diff --git a/source/application.h b/source/application.h new file mode 100644 index 0000000..4296b6f --- /dev/null +++ b/source/application.h @@ -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 +#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. +*/ +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 + 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