2 This file is part of libmspcore
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
8 #include "../time/units.h"
9 #include "../time/utils.h"
10 #include "application.h"
18 Constructs an instance of the registered application class and runs it. If the
19 application throws a UsageError, the static usage() function is called.
21 This function can only be called once. The global main() function provided by
22 the library normally does it automatically at program startup.
24 int Application::run(int argc, char **argv)
26 static bool called=false;
29 cerr<<"Trying to call Application::run_app twice!\n";
36 cerr<<"Trying to run with no application class registered!\n";
42 app_=reg_app_->create_app(argc, argv);
44 catch(const UsageError &e)
46 reg_app_->usage(e.what(), argv[0], e.get_brief());
50 int result=app_->main();
56 Prints a message describing the usage of the application. The default version
57 will blame the programmer for being lazy.
59 @param reason Why the function was called
60 @param argv0 The value of argv[0], to be used in the message
61 @param brief Whether to print a brief or long usage message
63 void Application::usage(const char *reason, const char *, bool)
66 cerr<<"UsageError: "<<reason<<'\n';
67 cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
70 Application::Application():
72 loop_mode_(TICK_SLEEP)
76 Default main loop. Behavior depends on loop_mode_. A custom main loop should
77 monitor the done member variable and return exit_code.
79 int Application::main()
93 else if(loop_mode_==TICK_SLEEP)
96 Time::sleep(Time::msec);
98 else if(loop_mode_==TICK_YIELD)
113 Sets the specified signal to be delivered to the sighandler member function.
115 void Application::catch_signal(int s)
117 signal(s, &sighandler_);
121 Changes the main loop mode.
123 void Application::set_loop_mode(LoopMode l)
125 LoopMode old_mode=loop_mode_;
132 Causes the tick() function to be executed once if loop mode is SLEEP. Has no
133 effect with other loop modes.
135 void Application::induce_tick()
137 if(loop_mode_==SLEEP)
142 Causes the application to exit gracefully with the given exit code.
144 void Application::exit(int c)
148 if(loop_mode_==SLEEP)
153 Static wrapper function to call a member function of the Application instance.
155 void Application::sighandler_(int s)
160 Application::RegBase::RegBase()
164 cerr<<"Warning: registering the application twice\n";
171 Application *Application::app_=0;
172 Application::RegBase *Application::reg_app_=0;