]> git.tdb.fi Git - libs/core.git/blob - source/core/application.cpp
Move Application member documentation to the header
[libs/core.git] / source / core / application.cpp
1 #include <signal.h>
2 #include <msp/io/print.h>
3 #include "application.h"
4 #include "getopt.h"
5
6 using namespace std;
7
8 namespace Msp {
9
10 Application *Application::app_ = 0;
11 Application::Starter *Application::starter_ = 0;
12 void *Application::data_ = 0;
13
14 Application::Application():
15         exit_code(0)
16 { }
17
18 int Application::run(int argc, char **argv, void *data)
19 {
20         static bool called = false;
21         if(called)
22         {
23                 IO::cerr.write("Trying to call Application::run_app twice!\n");
24                 return 125;
25         }
26         called = true;
27
28         if(!starter_)
29         {
30                 IO::cerr.write("Trying to run with no RegisteredApplication class!\n");
31                 return 126;
32         }
33
34         data_ = data;
35
36         try
37         {
38                 try
39                 {
40                         app_ = starter_->create_app(argc, argv);
41                 }
42                 catch(const usage_error &e)
43                 {
44                         IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
45                         return 1;
46                 }
47
48                 int result = app_->main();
49                 Application *a = app_;
50                 app_ = 0;
51                 delete a;
52                 return result;
53         }
54         catch(const exception &e)
55         {
56                 delete app_;
57
58                 display_exception(e);
59
60                 return 124;
61         }
62 }
63
64 int Application::main()
65 {
66         done = false;
67         while(!done)
68                 tick();
69
70         return exit_code;
71 }
72
73 void Application::catch_signal(int s)
74 {
75         signal(s, &sighandler_);
76 }
77
78 void Application::exit(int c)
79 {
80         done = true;
81         exit_code = c;
82 }
83
84 void Application::sighandler_(int s)
85 {
86         app_->sighandler(s);
87 }
88
89
90 Application::Starter::Starter()
91 {
92         if(starter_)
93                 throw logic_error("Can't create more than one Starter instance");
94
95         starter_ = this;
96 }
97
98 } // namespace Msp