2 This file is part of libmspframework
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
8 #include "application.h"
15 Poller::Slot &Application::add_pollable(Pollable *obj, short events)
20 Poller::Slot &slot=poller_->add_pollable(obj, events);
21 // Interrupt a possible poll in progress
23 pthread_kill(main_tid, SIGALRM);
28 EventManager::Event &Application::create_event()
31 ev_mgr_=new EventManager(*this);
33 return ev_mgr_->create_event();
36 Application::~Application()
45 Constructs an instance of the registered application class and runs it. If the
46 application throws a UsageError, the static usage() function is called.
48 This function can only be called once. The global main() function provided by
49 the library normally does it automatically at program startup.
51 int Application::run(int argc, char **argv)
53 static bool called=false;
56 cerr<<"Trying to call Application::run_app twice!\n";
63 cerr<<"Trying to run with no application class registered!\n";
68 signal(SIGALRM, &sigalrm_);
73 app_=reg_app_->create_app(argc, argv);
75 catch(const UsageError &e)
77 reg_app_->usage(argv[0], e.get_brief());
81 int result=app_->main();
87 Prints a message describing the usage of the application. The default version
88 will blame the programmer for being lazy.
90 @param argv0 The value of argv[0], to be used in the message
91 @param brief Whether to print a brief or long usage message
93 void Application::usage(const char *, bool)
95 cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
98 Application::Application():
104 //XXX Figure out how to get the current thread on win32
105 ,main_tid(pthread_self())
110 Default main loop. Calls tick() periodically if do_ticks is true, otherwise
111 just sleeps. A custom main loop should monitor the done member variable and
114 int Application::main()
139 timespec ts={1000,0};
152 Sets the specified signal to be delivered to the sighandler member function.
154 void Application::catch_signal(int s)
156 signal(s, &sighandler_);
159 void Application::set_tick_mode(TickMode t)
163 pthread_kill(main_tid, SIGALRM);
168 Causes the application to exit gracefully with the given exit code.
170 void Application::exit(int c)
175 pthread_kill(main_tid, SIGALRM);
179 void Application::sighandler_(int s)
184 Application::RegBase::RegBase()
188 cerr<<"Warning: registering the application twice\n";
195 Application *Application::app_=0;
196 Application::RegBase *Application::reg_app_=0;