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