]> git.tdb.fi Git - libs/core.git/blob - source/core/application.h
Add move semantics to Variant
[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 #include "noncopyable.h"
7
8 namespace Msp {
9
10 /**
11 Base class for applications.  See also RegisteredApplication.
12 */
13 class Application: private NonCopyable
14 {
15 protected:
16         class Starter
17         {
18         protected:
19                 Starter();
20         public:
21                 virtual ~Starter() = default;
22
23                 virtual Application *create_app(int, char **) = 0;
24         };
25
26         bool done = false;
27         int exit_code = 0;
28
29 private:
30         static Starter *_starter;
31         static Application *_app;
32         static const char *_argv0;
33         static std::string _name;
34         static void *_data;
35
36 protected:
37         Application(const std::string & = std::string());
38 public:
39         virtual ~Application() = default;
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 * = nullptr, void (*)(void *) = nullptr);
48
49         /** Sets application startup info, including argv[0] value and platform-
50         specific data.
51
52         This function can only be called once, and is normally called by
53         Application::run(). */
54         static void set_startup_info(const char *, void *);
55
56         static void *get_data() { return _data; }
57         static const char *get_argv0() { return _argv0; }
58         static const std::string &get_name() { return _name; }
59
60 protected:
61         /** Default main loop.  Calls tick() repeatedly until exit() is called.  A
62         custom main loop should monitor the done member variable and return
63         exit_code. */
64         virtual int main();
65
66         /** Sets the specified signal to be delivered to the sighandler member
67         function. */
68         void catch_signal(int);
69
70         /** Causes the application to exit gracefully with the given exit code. */
71         void exit(int);
72
73         virtual void tick() { }
74         virtual void sighandler(int) { }
75
76 private:
77         /** Static wrapper function to call a member function of the Application
78         instance. */
79         static void _sighandler(int);
80 };
81
82
83 /**
84 Registers the class to be used for program startup.  The main application class
85 should be derived from this.
86 */
87 template<typename T>
88 class RegisteredApplication: public Application
89 {
90 private:
91         class Starter: public Application::Starter
92         {
93         public:
94                 Application *create_app(int argc, char **argv) { return new T(argc, argv); }
95         };
96
97         static Starter _starter;
98
99 protected:
100         RegisteredApplication(const std::string &n = std::string()):
101                 Application(n)
102         { (void)_starter; }  // Force the starter into existence
103 };
104
105 template<typename T>
106 typename RegisteredApplication<T>::Starter RegisteredApplication<T>::_starter;
107
108 } // namespace Msp
109
110 #endif