3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include "../time/units.h"
10 #include "../time/utils.h"
11 #include "application.h"
19 Constructs an instance of the registered application class and runs it. If the
20 application throws a UsageError, the static usage() function is called.
22 This function can only be called once. The global main() function provided by
23 the library normally does it automatically at program startup.
25 int Application::run(int argc, char **argv)
27 static bool called=false;
30 cerr<<"Trying to call Application::run_app twice!\n";
37 cerr<<"Trying to run with no application class registered!\n";
45 app_=reg_app_->create_app(argc, argv);
47 catch(const UsageError &e)
49 reg_app_->usage(e.what(), argv[0], e.get_brief());
53 int result=app_->main();
57 catch(const exception &e)
59 cerr<<"An uncaught exception occurred.\n";
60 cerr<<" type: "<<typeid(e).name()<<'\n';
61 cerr<<" what(): "<<e.what()<<'\n';
68 Prints a message describing the usage of the application. The default version
69 will blame the programmer for being lazy.
71 @param reason Why the function was called
72 @param argv0 The value of argv[0], to be used in the message
73 @param brief Whether to print a brief or long usage message
75 void Application::usage(const char *reason, const char *, bool)
78 cerr<<"UsageError: "<<reason<<'\n';
79 cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
82 Application::Application():
84 loop_mode_(TICK_SLEEP)
88 Default main loop. Behavior depends on loop_mode_. A custom main loop should
89 monitor the done member variable and return exit_code.
91 int Application::main()
105 else if(loop_mode_==TICK_SLEEP)
108 Time::sleep(Time::msec);
110 else if(loop_mode_==TICK_YIELD)
125 Sets the specified signal to be delivered to the sighandler member function.
127 void Application::catch_signal(int s)
129 signal(s, &sighandler_);
133 Changes the main loop mode.
135 void Application::set_loop_mode(LoopMode l)
137 LoopMode old_mode=loop_mode_;
144 Causes the tick() function to be executed once if loop mode is SLEEP. Has no
145 effect with other loop modes.
147 void Application::induce_tick()
149 if(loop_mode_==SLEEP)
154 Causes the application to exit gracefully with the given exit code.
156 void Application::exit(int c)
160 if(loop_mode_==SLEEP)
165 Static wrapper function to call a member function of the Application instance.
167 void Application::sighandler_(int s)
172 Application::RegBase::RegBase()
176 cerr<<"Warning: registering the application twice\n";
183 Application *Application::app_=0;
184 Application::RegBase *Application::reg_app_=0;