application.h

00001 /*
00002  * application.h
00003  * This file is part of dbPager Classes Library (DCL)
00004  *
00005  * Copyright (c) 2008 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * DCL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * DCL is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with DCL; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
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 &param,
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 &param,
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 &param);
00161         void print_cmdline_parameters_help();
00162 };
00163 
00164 } // namespace
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 /*_APPLICATION_H_*/

 
Support This Project
SourceForge.net Logo