3 This file is part of libmspcore
4 Copyright © 2006-2008, 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
14 #include "../debug/demangle.h"
15 #include "application.h"
22 Application *Application::app_ = 0;
23 Application::Starter *Application::starter_ = 0;
24 void *Application::data_ = 0;
26 Application::Application():
31 Constructs an instance of the registered application class and runs it. If the
32 application throws a usage_error, a help message is printed. The GetOpt class
33 will throw such exceptions automatically in error conditions.
35 This function can only be called once. The global main() function provided by
36 the library normally does it automatically at program startup.
38 int Application::run(int argc, char **argv, void *data)
40 static bool called = false;
43 cerr<<"Trying to call Application::run_app twice!\n";
50 cerr<<"Trying to run with no RegisteredApplication class!\n";
60 app_ = starter_->create_app(argc, argv);
62 catch(const usage_error &e)
69 int result = app_->main();
70 Application *a = app_;
75 catch(const exception &e)
80 string msg = Debug::demangle(typeid(e).name())+":\n"+e.what();
81 MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR);
83 cerr<<"An uncaught exception occurred.\n";
84 cerr<<" type: "<<Debug::demangle(typeid(e).name())<<'\n';
85 cerr<<" what(): "<<e.what()<<'\n';
93 Default main loop. Calls tick() repeatedly until exit() is called. A custom
94 main loop should monitor the done member variable and return exit_code.
96 int Application::main()
106 Sets the specified signal to be delivered to the sighandler member function.
108 void Application::catch_signal(int s)
110 signal(s, &sighandler_);
114 Causes the application to exit gracefully with the given exit code.
116 void Application::exit(int c)
123 Static wrapper function to call a member function of the Application instance.
125 void Application::sighandler_(int s)
131 Application::Starter::Starter()
134 throw logic_error("Can't create more than one Starter instance");