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
22 pthread_kill(main_tid, SIGALRM);
26 EventManager::Event &Application::create_event()
29 ev_mgr_=new EventManager(*this);
31 return ev_mgr_->create_event();
34 Application::~Application()
43 Constructs an instance of the registered application class and runs it. If the
44 application throws a UsageError, the static usage() function is called.
46 This function can only be called once. The global main() function provided by
47 the library normally does it automatically at program startup.
49 int Application::run(int argc, char **argv)
51 static bool called=false;
54 cerr<<"Trying to call Application::run_app twice!\n";
61 cerr<<"Trying to run with no application class registered!\n";
65 signal(SIGALRM, &sigalrm_);
69 app_=reg_app_->create_app(argc, argv);
71 catch(const UsageError &e)
73 reg_app_->usage(argv[0], e.get_brief());
77 int result=app_->main();
83 Prints a message describing the usage of the application. The default version
84 will blame the programmer for being lazy.
86 @param argv0 The value of argv[0], to be used in the message
87 @param brief Whether to print a brief or long usage message
89 void Application::usage(const char *, bool)
91 cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
95 Default main loop. Calls tick() periodically if do_ticks is true, otherwise
96 just sleeps. A custom main loop should monitor the done member variable and
99 int Application::main()
117 timespec ts={1000,0};
129 Sets the specified signal to be delivered to the sighandler member function.
131 void Application::catch_signal(int s)
133 signal(s, &sighandler_);
136 void Application::set_tick_mode(TickMode t)
139 pthread_kill(main_tid, SIGALRM);
143 Causes the application to exit gracefully with the given exit code.
145 void Application::exit(int c)
149 pthread_kill(main_tid, SIGALRM);
152 void Application::sighandler_(int s)
157 Application::RegBase::RegBase()
161 cerr<<"Warning: registering the application twice\n";
168 Application *Application::app_=0;
169 Application::RegBase *Application::reg_app_=0;