6 #include <msp/debug/demangle.h>
7 #include <msp/io/print.h>
8 #include "application.h"
15 Application *Application::app_ = 0;
16 Application::Starter *Application::starter_ = 0;
17 void *Application::data_ = 0;
19 Application::Application():
24 Constructs an instance of the registered application class and runs it. If the
25 application throws a usage_error, a help message is printed. The GetOpt class
26 will throw such exceptions automatically in error conditions.
28 This function can only be called once. The global main() function provided by
29 the library normally does it automatically at program startup.
31 int Application::run(int argc, char **argv, void *data)
33 static bool called = false;
36 IO::cerr.write("Trying to call Application::run_app twice!\n");
43 IO::cerr.write("Trying to run with no RegisteredApplication class!\n");
53 app_ = starter_->create_app(argc, argv);
55 catch(const usage_error &e)
57 IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
61 int result = app_->main();
62 Application *a = app_;
67 catch(const exception &e)
72 string msg = Debug::demangle(typeid(e).name())+":\n"+e.what();
73 MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR);
75 IO::print(IO::cerr, "An uncaught exception occurred.\n");
76 IO::print(IO::cerr, " type: %s\n", Debug::demangle(typeid(e).name()));
77 IO::print(IO::cerr, " what(): %s\n", e.what());
85 Default main loop. Calls tick() repeatedly until exit() is called. A custom
86 main loop should monitor the done member variable and return exit_code.
88 int Application::main()
98 Sets the specified signal to be delivered to the sighandler member function.
100 void Application::catch_signal(int s)
102 signal(s, &sighandler_);
106 Causes the application to exit gracefully with the given exit code.
108 void Application::exit(int c)
115 Static wrapper function to call a member function of the Application instance.
117 void Application::sighandler_(int s)
123 Application::Starter::Starter()
126 throw logic_error("Can't create more than one Starter instance");