From 85a0abf9ed82f3b2a80d98aefc3bb8b45ff0a2e7 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Tue, 28 Jul 2009 11:48:25 +0200 Subject: [PATCH] Introduced shared_ptr semantic. Introduced const & semantic. --- src/config.cpp | 30 ++++++++++++++++++------------ src/config.h | 15 +++++++++------ src/dhs.cpp | 5 +++-- src/dhs.h | 4 ++-- src/main.cpp | 7 +++++-- src/ods.cpp | 5 +++-- src/ods.h | 4 ++-- src/service.cpp | 6 ++++++ src/service.h | 2 +- src/updater.cpp | 13 ++++++------- src/updater.h | 10 ++++++---- 11 files changed, 61 insertions(+), 40 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index cd71bcd..71742c9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -10,12 +10,12 @@ #include "config.h" /** - * Default Constructor. Available command line arguments with their default values are defined here. + * Default Constructor. Available command line and config file options with their default values are defined here. */ Config::Config() - : Daemon_mode(true) + : Daemon_mode(false) , Loglevel(0) - , Syslog(true) + , Syslog(false) { // Define valid command line parameters Opt_desc_cmd = new po::options_description("Command line options"); @@ -56,6 +56,8 @@ Config::~Config() delete Opt_desc_cmd; delete Opt_desc_conf_main; delete Opt_desc_conf_service; + + cout << "Config destructor!!!" << endl; } @@ -65,7 +67,7 @@ Config::~Config() * @param argv[] Pointer to command line argument array given to main. * @return 0 if all is fine, */ -int Config::parse_cmd_line(int argc, char *argv[]) +const int Config::parse_cmd_line(int argc, char *argv[]) { try { @@ -114,18 +116,22 @@ int Config::parse_cmd_line(int argc, char *argv[]) * @param password * @return A pointer to the created Service object. */ -Service * Config::create_service(string protocol,string host, string login, string password) +ServicePtr Config::create_service(const string &protocol,const string &host, const string &login, const string &password) { - Service * service; if(protocol == "dhs") { - service = new DHS(host,login,password); + ServicePtr service_dhs(new DHS(host,login,password)); + return service_dhs; } else if(protocol == "ods") { - service = new ODS(host,login,password); + ServicePtr service_ods(new ODS(host,login,password)); + return service_ods; + } + else + { + // TODO: error handling for unknown protocol } - return service; } @@ -134,7 +140,7 @@ Service * Config::create_service(string protocol,string host, string login, stri * @param config_path The path to the config directory. * @return 0 if all is fine. */ -int Config::load_config_from_files(string config_path) +const int Config::load_config_from_files(const string& config_path) { fs::path full_config_path = fs::system_complete(fs::path(config_path)); @@ -237,7 +243,7 @@ int Config::load_config_from_files(string config_path) * Getter method for Service list member. * @return Pointer to a list of Service's. */ -list Config::get_services() +list Config::get_services() { return this->Services; } @@ -257,7 +263,7 @@ void Config::print_usage() * Prints out the programm name and the given version string on stdout. * @param version Version string. */ -void Config::print_version(string version) +void Config::print_version(const string& version) { cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl; } diff --git a/src/config.h b/src/config.h index 481abc0..08f9723 100644 --- a/src/config.h +++ b/src/config.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,8 @@ namespace fs = boost::filesystem; using namespace std; +typedef boost::shared_ptr ServicePtr; + class Config { private: @@ -36,28 +39,28 @@ private: po::options_description *Opt_desc_conf_main; po::options_description *Opt_desc_conf_service; - list Services; + list Services; bool Daemon_mode; string Logfile; int Loglevel; bool Syslog; - Service * create_service(string,string,string,string); + ServicePtr create_service(const string&,const string&,const string&,const string&); public: Config(); ~Config(); - int parse_cmd_line(int, char **); + const int parse_cmd_line(int, char **); - int load_config_from_files(string); + const int load_config_from_files(const string&); void print_usage(); - void print_version(string); + void print_version(const string&); - list get_services(); + list get_services(); }; #endif diff --git a/src/dhs.cpp b/src/dhs.cpp index 82d94e9..10672ba 100644 --- a/src/dhs.cpp +++ b/src/dhs.cpp @@ -24,7 +24,7 @@ DHS::DHS() * @param _login The login name. * @param _password The corresponding password. */ -DHS::DHS(string _hostname, string _login, string _password) +DHS::DHS(const string& _hostname, const string& _login, const string& _password) { Hostname = _hostname; Login = _login; @@ -37,6 +37,7 @@ DHS::DHS(string _hostname, string _login, string _password) */ DHS::~DHS() { + cout << "DHS destructor!!!" << endl; } @@ -44,7 +45,7 @@ DHS::~DHS() * Service update method which should implement the corresponding service protocol. * @param ip The new ip to set for the hostname. */ -void DHS::update(string ip) +void DHS::update(const string& ip) { cout << "Running Update for Service DHS" << endl; cout << "Hostname: " << Hostname << endl; diff --git a/src/dhs.h b/src/dhs.h index 41f5639..ae684f5 100644 --- a/src/dhs.h +++ b/src/dhs.h @@ -25,11 +25,11 @@ private: public: DHS(); - DHS(string,string,string); + DHS(const string&, const string&, const string&); ~DHS(); - void update(string); + void update(const string&); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 462ffc4..80218b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,9 @@ using namespace std; +typedef boost::shared_ptr ConfigPtr; +typedef boost::shared_ptr UpdaterPtr; + /** * @brief The main part. * @param argc Number of arguments @@ -40,7 +43,7 @@ using namespace std; int main(int argc, char *argv[]) { // Initialize Config class and get the command line parameters - Config * config = new Config; + ConfigPtr config(new Config); int ret_val = config->parse_cmd_line(argc,argv); if(ret_val == 1) { @@ -69,7 +72,7 @@ int main(int argc, char *argv[]) } // initialize Updater - Updater * updater = new Updater(config); + UpdaterPtr updater(new Updater(config)); updater->update_services(); return 0; diff --git a/src/ods.cpp b/src/ods.cpp index 7a83309..a0ffa29 100644 --- a/src/ods.cpp +++ b/src/ods.cpp @@ -23,7 +23,7 @@ ODS::ODS() * @param _login The login name. * @param _password The corresponding password. */ -ODS::ODS(string _hostname, string _login, string _password) +ODS::ODS(const string& _hostname, const string& _login, const string& _password) { Hostname = _hostname; Login = _login; @@ -36,6 +36,7 @@ ODS::ODS(string _hostname, string _login, string _password) */ ODS::~ODS() { + cout << "ODS destructor!!!" << endl; } @@ -43,7 +44,7 @@ ODS::~ODS() * Service update method which should implement the corresponding service protocol. * @param ip The new ip to set for the hostname. */ -void ODS::update(string ip) +void ODS::update(const string& ip) { cout << "Running Update for Service ODS" << endl; cout << "Hostname: " << Hostname << endl; diff --git a/src/ods.h b/src/ods.h index d9a3324..25c925e 100644 --- a/src/ods.h +++ b/src/ods.h @@ -25,11 +25,11 @@ private: public: ODS(); - ODS(string,string,string); + ODS(const string&, const string&, const string&); ~ODS(); - void update(string); + void update(const string&); }; #endif diff --git a/src/service.cpp b/src/service.cpp index b290006..d2ca5ba 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -9,10 +9,16 @@ #include "service.h" +/** + * Default Constructor + */ Service::Service() { } +/** + * Default Destructor + */ Service::~Service() { } diff --git a/src/service.h b/src/service.h index 07ba931..044887b 100644 --- a/src/service.h +++ b/src/service.h @@ -21,7 +21,7 @@ public: virtual ~Service(); - virtual void update(string)=0; + virtual void update(const string&)=0; }; diff --git a/src/updater.cpp b/src/updater.cpp index 5bd8fc7..35811e6 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -16,7 +16,6 @@ * Default constructor which initializes the member Conf. */ Updater::Updater() - : Conf(NULL) { } @@ -25,7 +24,7 @@ Updater::Updater() * Constructor. * @param _conf A pointer to a Config object. */ -Updater::Updater(Config* _conf) +Updater::Updater(ConfigPtr _conf) { Conf = _conf; } @@ -36,7 +35,7 @@ Updater::Updater(Config* _conf) */ Updater::~Updater() { - Conf = NULL; + cout << "Updater destructor!!!" << endl; } @@ -44,7 +43,7 @@ Updater::~Updater() * Setter for member Conf. * @param _conf */ -void Updater::set_config(Config* _conf) +void Updater::set_config(ConfigPtr _conf) { Conf = _conf; } @@ -54,7 +53,7 @@ void Updater::set_config(Config* _conf) * Getter for member Conf. * @return Conf. */ -Config* Updater::get_config() +ConfigPtr Updater::get_config() { return Conf; } @@ -65,11 +64,11 @@ Config* Updater::get_config() */ void Updater::update_services() { - list services = this->Conf->get_services(); + list services = this->Conf->get_services(); string ip = "192.168.1.1"; - BOOST_FOREACH( Service * service, services ) + BOOST_FOREACH( ServicePtr service, services ) { service->update(ip); } diff --git a/src/updater.h b/src/updater.h index dd8c154..edb6548 100644 --- a/src/updater.h +++ b/src/updater.h @@ -12,21 +12,23 @@ #include "config.h" +typedef boost::shared_ptr ConfigPtr; + class Updater { private: - Config* Conf; + ConfigPtr Conf; public: Updater(); - Updater(Config*); + Updater(ConfigPtr); ~Updater(); - void set_config(Config*); + void set_config(ConfigPtr); - Config* get_config(); + ConfigPtr get_config(); void update_services(); }; -- 1.7.1