3 This file is part of libmspcore
4 Copyright © 2006-2008, 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include "../debug/demangle.h"
12 #include "application.h"
19 Application *Application::app_ = 0;
20 Application::Starter *Application::starter_ = 0;
21 void *Application::data_ = 0;
23 Application::Application():
28 Constructs an instance of the registered application class and runs it. If the
29 application throws a usage_error, a help message is printed. The GetOpt class
30 will throw such exceptions automatically in error conditions.
32 This function can only be called once. The global main() function provided by
33 the library normally does it automatically at program startup.
35 int Application::run(int argc, char **argv, void *data)
37 static bool called = false;
40 cerr<<"Trying to call Application::run_app twice!\n";
47 cerr<<"Trying to run with no RegisteredApplication class!\n";
57 app_ = starter_->create_app(argc, argv);
59 catch(const usage_error &e)
66 int result = app_->main();
67 Application *a = app_;
72 catch(const exception &e)
77 string msg = Debug::demangle(typeid(e).name())+":\n"+e.what();
78 MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR);
80 cerr<<"An uncaught exception occurred.\n";
81 cerr<<" type: "<<Debug::demangle(typeid(e).name())<<'\n';
82 cerr<<" what(): "<<e.what()<<'\n';
90 Default main loop. Calls tick() repeatedly until exit() is called. A custom
91 main loop should monitor the done member variable and return exit_code.
93 int Application::main()
103 Sets the specified signal to be delivered to the sighandler member function.
105 void Application::catch_signal(int s)
107 signal(s, &sighandler_);
111 Causes the application to exit gracefully with the given exit code.
113 void Application::exit(int c)
120 Static wrapper function to call a member function of the Application instance.
122 void Application::sighandler_(int s)
128 Application::Starter::Starter()
131 throw logic_error("Can't create more than one Starter instance");