3 This file is part of libmspcore
4 Copyright © 2006-2008, 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include "../debug/backtrace.h"
12 #include "../debug/demangle.h"
13 #include "../time/units.h"
14 #include "../time/utils.h"
15 #include "application.h"
22 Application::Application():
27 Constructs an instance of the registered application class and runs it. If the
28 application throws a UsageError, the static usage() function is called.
30 This function can only be called once. The global main() function provided by
31 the library normally does it automatically at program startup.
33 int Application::run(int argc, char **argv, void *data)
35 static bool called=false;
38 cerr<<"Trying to call Application::run_app twice!\n";
45 cerr<<"Trying to run with no RegisteredApplication class!\n";
55 app_=starter_->create_app(argc, argv);
57 catch(const UsageError &e)
59 starter_->usage(e.what(), argv[0], e.get_brief());
63 int result=app_->main();
69 catch(const exception &e)
74 string msg=Debug::demangle(typeid(e).name())+":\n"+e.what();
75 MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR);
77 cerr<<"An uncaught exception occurred.\n";
78 cerr<<" type: "<<Debug::demangle(typeid(e).name())<<'\n';
79 cerr<<" what(): "<<e.what()<<'\n';
81 const Exception *exc=dynamic_cast<const Exception *>(&e);
82 if(exc && !exc->get_backtrace().get_frames().empty())
84 cerr<<" backtrace:\n";
85 const list<Debug::Backtrace::StackFrame> &frames=exc->get_backtrace().get_frames();
86 for(list<Debug::Backtrace::StackFrame>::const_iterator i=frames.begin(); i!=frames.end(); ++i)
96 Prints a message describing the usage of the application. The default version
97 will blame the programmer for being lazy.
99 @param reason Why the function was called
100 @param argv0 The value of argv[0], to be used in the message
101 @param brief Whether to print a brief or long usage message
103 void Application::usage(const char *reason, const char *, bool)
106 cerr<<"UsageError: "<<reason<<'\n';
107 cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
111 Default main loop. Calls tick() repeatedly until exit() is called. A custom
112 main loop should monitor the done member variable and return exit_code.
114 int Application::main()
124 Sets the specified signal to be delivered to the sighandler member function.
126 void Application::catch_signal(int s)
128 signal(s, &sighandler_);
132 Causes the application to exit gracefully with the given exit code.
134 void Application::exit(int c)
141 Static wrapper function to call a member function of the Application instance.
143 void Application::sighandler_(int s)
149 Application::Starter::Starter()
152 throw InvalidState("Can't create more than one Starter instance");
157 Application *Application::app_=0;
158 Application::RegBase *Application::reg_app_=0;
159 void *Application::data_=0;