]> git.tdb.fi Git - libs/core.git/blob - source/core/application.cpp
Separate event-related stuff from Base
[libs/core.git] / source / core / application.cpp
1 #ifdef WIN32
2 #include <windows.h>
3 #endif
4 #include <signal.h>
5 #include <iostream>
6 #include <typeinfo>
7 #include <msp/debug/demangle.h>
8 #include "application.h"
9 #include "getopt.h"
10
11 using namespace std;
12
13 namespace Msp {
14
15 Application *Application::app_ = 0;
16 Application::Starter *Application::starter_ = 0;
17 void *Application::data_ = 0;
18
19 Application::Application():
20         exit_code(0)
21 { }
22
23 /**
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.
27
28 This function can only be called once.  The global main() function provided by
29 the library normally does it automatically at program startup.
30 */
31 int Application::run(int argc, char **argv, void *data)
32 {
33         static bool called = false;
34         if(called)
35         {
36                 cerr<<"Trying to call Application::run_app twice!\n";
37                 return 125;
38         }
39         called = true;
40
41         if(!starter_)
42         {
43                 cerr<<"Trying to run with no RegisteredApplication class!\n";
44                 return 126;
45         }
46
47         data_ = data;
48
49         try
50         {
51                 try
52                 {
53                         app_ = starter_->create_app(argc, argv);
54                 }
55                 catch(const usage_error &e)
56                 {
57                         cerr<<e.what()<<'\n';
58                         cerr<<e.help()<<'\n';
59                         return 1;
60                 }
61
62                 int result = app_->main();
63                 Application *a = app_;
64                 app_ = 0;
65                 delete a;
66                 return result;
67         }
68         catch(const exception &e)
69         {
70                 delete app_;
71
72 #ifdef WIN32
73                 string msg = Debug::demangle(typeid(e).name())+":\n"+e.what();
74                 MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR);
75 #else
76                 cerr<<"An uncaught exception occurred.\n";
77                 cerr<<"  type:   "<<Debug::demangle(typeid(e).name())<<'\n';
78                 cerr<<"  what(): "<<e.what()<<'\n';
79 #endif
80
81                 return 124;
82         }
83 }
84
85 /**
86 Default main loop.  Calls tick() repeatedly until exit() is called.  A custom
87 main loop should monitor the done member variable and return exit_code.
88 */
89 int Application::main()
90 {
91         done = false;
92         while(!done)
93                 tick();
94
95         return exit_code;
96 }
97
98 /**
99 Sets the specified signal to be delivered to the sighandler member function.
100 */
101 void Application::catch_signal(int s)
102 {
103         signal(s, &sighandler_);
104 }
105
106 /**
107 Causes the application to exit gracefully with the given exit code.
108 */
109 void Application::exit(int c)
110 {
111         done = true;
112         exit_code = c;
113 }
114
115 /**
116 Static wrapper function to call a member function of the Application instance.
117 */
118 void Application::sighandler_(int s)
119 {
120         app_->sighandler(s);
121 }
122
123
124 Application::Starter::Starter()
125 {
126         if(starter_)
127                 throw logic_error("Can't create more than one Starter instance");
128
129         starter_ = this;
130 }
131
132 } // namespace Msp