application.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _APPLICATION_H_
00023 #define _APPLICATION_H_
00024
00025 #include <string>
00026 #include <map>
00027
00028 #include <dcl/delegate.h>
00029 #include <dcl/cmdline_parameters.h>
00030 #include <dcl/singleton.h>
00031
00032 namespace dbp {
00033
00035
00039 class application_int {
00040 public:
00042 typedef delegate1<cmdline_parameter&, bool> cmdline_parameter_handler;
00044 typedef delegate0<int> on_execute_handler;
00046 typedef delegate1<const std::exception&, void> on_exception_handler;
00048 virtual ~application_int() { };
00050
00057 virtual void set_name(const std::string &name) = 0;
00059
00063 virtual void set_description(const std::string &description) = 0;
00065
00078 virtual void register_cmdline_parameter(const cmdline_parameter ¶m,
00079 cmdline_parameter_handler handler) = 0;
00081
00090 virtual void on_execute(on_execute_handler handler) = 0;
00092 virtual void on_exception(on_exception_handler handler) = 0;
00094 virtual int run(int argc, char *argv[]) = 0;
00095 };
00096
00098
00105 class application: public application_int,
00106 public singleton<application> {
00107 friend class singleton<application>;
00108 public:
00109 virtual void set_name(const std::string &app_name);
00110 virtual void set_description(const std::string &description);
00111 virtual void register_cmdline_parameter(const cmdline_parameter ¶m,
00112 application_int::cmdline_parameter_handler handler);
00114
00123 virtual void on_execute(on_execute_handler handler);
00125 virtual void on_exception(on_exception_handler handler);
00127 virtual int run(int argc, char *argv[]);
00129
00132 const std::string& get_name() const {
00133 return _app_name;
00134 };
00136
00139 const std::string& get_path() const {
00140 return _app_path;
00141 };
00143
00146 const std::string& get_description() const {
00147 return _app_description;
00148 };
00149 protected:
00150 application();
00151 private:
00152 std::string _app_path, _app_name, _app_description;
00153 cmdline_parameters _params;
00154 typedef std::map<char, cmdline_parameter_handler>
00155 cmdline_parameter_handlers;
00156 cmdline_parameter_handlers _handlers;
00157 on_execute_handler execute_handler;
00158 on_exception_handler exception_handler;
00159 bool init(int argc, char *argv[]);
00160 bool cmdline_help_handler(cmdline_parameter ¶m);
00161 void print_cmdline_parameters_help();
00162 };
00163
00164 }
00165
00166 #undef IMPLEMENT_APP
00167 #define IMPLEMENT_APP(app) \
00168 int main(int argc, char *argv[]) { \
00169 return app.run(argc, argv); \
00170 };
00171
00172 #endif