]> git.tdb.fi Git - libs/core.git/blob - source/core/application.cpp
1fbe15452fd8a13d39c3c0939d2c09419e5b6733
[libs/core.git] / source / core / application.cpp
1 #include <cstring>
2 #include <typeinfo>
3 #include <signal.h>
4 #include <msp/debug/demangle.h>
5 #include <msp/debug/errorreporter.h>
6 #include <msp/fs/dir.h>
7 #include <msp/fs/path.h>
8 #include <msp/fs/utils.h>
9 #include <msp/io/print.h>
10 #include <msp/strings/utils.h>
11 #include "application.h"
12 #include "getopt.h"
13
14 using namespace std;
15
16 namespace Msp {
17
18 Application *Application::_app = 0;
19 Application::Starter *Application::_starter = 0;
20 const char *Application::_argv0 = 0;
21 string Application::_name;
22 void *Application::_data = 0;
23
24 Application::Application(const string &n)
25 {
26         if(_app)
27                 throw logic_error("instance already exists");
28
29         if(!n.empty())
30                 _name = n;
31         else
32                 _name = FS::basename(_argv0);
33 }
34
35 int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *))
36 {
37         if(!_starter)
38         {
39                 IO::cerr.write("Application::run called with no RegisteredApplication class!\n");
40                 return 126;
41         }
42
43         set_startup_info(argv[0], data);
44
45         try
46         {
47                 try
48                 {
49                         _app = _starter->create_app(argc, argv);
50                 }
51                 catch(const usage_error &e)
52                 {
53                         IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
54                         return 1;
55                 }
56
57                 if(created_callback)
58                         created_callback(data);
59
60                 int result = _app->main();
61                 Application *a = _app;
62                 _app = 0;
63                 delete a;
64                 return result;
65         }
66         catch(const exception &e)
67         {
68                 bool handled = false;
69                 if(const Debug::ErrorReporter *er = Debug::ErrorReporter::get_current())
70                         handled = er->report_uncaught_exception(e);
71
72                 if(!handled)
73                 {
74                         IO::print(IO::cerr, "An uncaught exception occurred.\n");
75                         IO::print(IO::cerr, "  type:   %s\n", Debug::demangle(typeid(e).name()));
76                         vector<string> lines = split(e.what(), '\n');
77                         if(lines.size()<2)
78                                 IO::print(IO::cerr, "  what(): %s\n", e.what());
79                         else
80                         {
81                                 IO::print(IO::cerr, "  what(): %s\n", lines.front());
82                                 for(auto i=lines.begin(); ++i!=lines.end(); )
83                                         IO::print(IO::cerr, "          %s\n", *i);
84                         }
85                 }
86
87                 delete _app;
88                 _app = 0;
89
90                 return 124;
91         }
92 }
93
94 void Application::set_startup_info(const char *argv0, void *data)
95 {
96         if(_argv0)
97                 throw logic_error("startup info already set");
98
99         static FS::Path exe;
100
101         if(!argv0 || !*argv0)
102         {
103 #ifdef _WIN32
104                 argv0 = "application.exe";
105 #else
106                 argv0 = "./application";
107 #endif
108         }
109
110         bool has_slash = strchr(argv0, FS::DIRSEP);
111         if(!has_slash)
112                 exe = FS::path_lookup(argv0);
113         if(exe.empty())
114                 exe = FS::realpath(argv0);
115
116         _argv0 = exe.c_str();
117         _data = data;
118 }
119
120 int Application::main()
121 {
122         done = false;
123         while(!done)
124                 tick();
125
126         return exit_code;
127 }
128
129 void Application::catch_signal(int s)
130 {
131         signal(s, &_sighandler);
132 }
133
134 void Application::exit(int c)
135 {
136         done = true;
137         exit_code = c;
138 }
139
140 void Application::_sighandler(int s)
141 {
142         _app->sighandler(s);
143 }
144
145
146 Application::Starter::Starter()
147 {
148         if(_starter)
149                 throw logic_error("Can't create more than one Starter instance");
150
151         _starter = this;
152 }
153
154 } // namespace Msp