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 application class registered!\n";
55 app_=reg_app_->create_app(argc, argv);
57 catch(const UsageError &e)
59 reg_app_->usage(e.what(), argv[0], e.get_brief());
63 int result=app_->main();
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 cerr<<"An uncaught exception occurred.\n";
76 cerr<<" type: "<<Debug::demangle(typeid(e).name())<<'\n';
77 cerr<<" what(): "<<e.what()<<'\n';
79 const Exception *exc=dynamic_cast<const Exception *>(&e);
80 if(exc && !exc->get_backtrace().get_frames().empty())
82 cerr<<" backtrace:\n";
83 const list<Debug::Backtrace::StackFrame> &frames=exc->get_backtrace().get_frames();
84 for(list<Debug::Backtrace::StackFrame>::const_iterator i=frames.begin(); i!=frames.end(); ++i)
94 Prints a message describing the usage of the application. The default version
95 will blame the programmer for being lazy.
97 @param reason Why the function was called
98 @param argv0 The value of argv[0], to be used in the message
99 @param brief Whether to print a brief or long usage message
101 void Application::usage(const char *reason, const char *, bool)
104 cerr<<"UsageError: "<<reason<<'\n';
105 cerr<<"The programmer was lazy and didn't write a usage() function for this application.\n";
109 Default main loop. Calls tick() repeatedly until exit() is called. A custom
110 main loop should monitor the done member variable and return exit_code.
112 int Application::main()
122 Sets the specified signal to be delivered to the sighandler member function.
124 void Application::catch_signal(int s)
126 signal(s, &sighandler_);
130 Causes the application to exit gracefully with the given exit code.
132 void Application::exit(int c)
139 Static wrapper function to call a member function of the Application instance.
141 void Application::sighandler_(int s)
147 Application::RegBase::RegBase()
151 cerr<<"Warning: registering the application twice\n";
158 Application *Application::app_=0;
159 Application::RegBase *Application::reg_app_=0;
160 void *Application::data_=0;