]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
35ab8734ef30fa08b7a9da21e3c854a9f30d24b1
[libs/core.git] / source / core / application.h
1 #ifndef MSP_CORE_APPLICATION_H_
2 #define MSP_CORE_APPLICATION_H_
3
4 #include <stdexcept>
5
6 namespace Msp {
7
8 /**
9 Base class for applications.  See also RegisteredApplication.
10 */
11 class Application
12 {
13 protected:
14         class Starter
15         {
16         protected:
17                 Starter();
18         public:
19                 virtual ~Starter() { }
20
21                 virtual Application *create_app(int, char **) = 0;
22         };
23
24         bool done;
25         int exit_code;
26
27 private:
28         static Starter *starter_;
29         static Application *app_;
30         static const char *argv0_;
31         static std::string name_;
32         static void *data_;
33
34         Application(const Application &);
35         Application &operator=(const Application &);
36 protected:
37         Application(const std::string & = std::string());
38 public:
39         virtual ~Application() { }
40
41         /** Constructs an instance of the registered application class and runs it.
42         If the application throws a usage_error, a help message is printed.  The
43         GetOpt class will throw such exceptions automatically in error conditions.
44
45         This function can only be called once.  The global main() function provided
46         by the library normally does it automatically at program startup. */
47         static int run(int, char **, void * =0, void (*)(void *) = 0);
48
49         static void *get_data() { return data_; }
50         static const char *get_argv0() { return argv0_; }
51         static const std::string &get_name() { return name_; }
52
53 protected:
54         /** Default main loop.  Calls tick() repeatedly until exit() is called.  A
55         custom main loop should monitor the done member variable and return
56         exit_code. */
57         virtual int main();
58
59         /** Sets the specified signal to be delivered to the sighandler member
60         function. */
61         void catch_signal(int);
62
63         /** Causes the application to exit gracefully with the given exit code. */
64         void exit(int);
65
66         virtual void tick() { }
67         virtual void sighandler(int) { }
68
69 private:
70         /** Static wrapper function to call a member function of the Application
71         instance. */
72         static void sighandler_(int);
73 };
74
75
76 /**
77 Registers the class to be used for program startup.  The main application class
78 should be derived from this.
79 */
80 template<typename T>
81 class RegisteredApplication: public Application
82 {
83 private:
84         class Starter: public Application::Starter
85         {
86         public:
87                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
88         };
89
90         static Starter starter_;
91
92 protected:
93         RegisteredApplication(const std::string &n = std::string()):
94                 Application(n)
95         { (void)starter_; }  // Force the starter into existence
96 };
97
98 template<typename T>
99 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::starter_;
100
101 } // namespace Msp
102
103 #endif