Introduced shared_ptr semantic. Introduced const & semantic.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 28 Jul 2009 09:48:25 +0000 (11:48 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 28 Jul 2009 09:48:25 +0000 (11:48 +0200)
src/config.cpp
src/config.h
src/dhs.cpp
src/dhs.h
src/main.cpp
src/ods.cpp
src/ods.h
src/service.cpp
src/service.h
src/updater.cpp
src/updater.h

index cd71bcd..71742c9 100644 (file)
 #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<Service*> Config::get_services()
+list<ServicePtr> 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;
 }
index 481abc0..08f9723 100644 (file)
@@ -14,6 +14,7 @@
 #include <boost/foreach.hpp>
 #include <boost/filesystem.hpp>
 #include <boost/regex.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include <string>
 #include <iostream>
@@ -29,6 +30,8 @@ namespace fs = boost::filesystem;
 
 using namespace std;
 
+typedef boost::shared_ptr<Service> ServicePtr;
+
 class Config
 {
 private:
@@ -36,28 +39,28 @@ private:
     po::options_description *Opt_desc_conf_main;
     po::options_description *Opt_desc_conf_service;
 
-    list<Service*> Services;
+    list<ServicePtr> 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<Service*> get_services();
+    list<ServicePtr> get_services();
 };
 
 #endif
index 82d94e9..10672ba 100644 (file)
@@ -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;
index 41f5639..ae684f5 100644 (file)
--- 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
index 462ffc4..80218b9 100644 (file)
@@ -31,6 +31,9 @@
 
 using namespace std;
 
+typedef boost::shared_ptr<Config> ConfigPtr;
+typedef boost::shared_ptr<Updater> 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;
index 7a83309..a0ffa29 100644 (file)
@@ -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;
index d9a3324..25c925e 100644 (file)
--- 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
index b290006..d2ca5ba 100644 (file)
@@ -9,10 +9,16 @@
 
 #include "service.h"
 
+/**
+ * Default Constructor
+ */
 Service::Service()
 {
 }
 
+/**
+ * Default Destructor
+ */
 Service::~Service()
 {
 }
index 07ba931..044887b 100644 (file)
@@ -21,7 +21,7 @@ public:
 
     virtual ~Service();
 
-    virtual void update(string)=0;
+    virtual void update(const string&)=0;
 
 };
 
index 5bd8fc7..35811e6 100644 (file)
@@ -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<Service*> services = this->Conf->get_services();
+    list<ServicePtr> services = this->Conf->get_services();
 
     string ip = "192.168.1.1";
 
-    BOOST_FOREACH( Service * service, services )
+    BOOST_FOREACH( ServicePtr service, services )
     {
         service->update(ip);
     }
index dd8c154..edb6548 100644 (file)
 
 #include "config.h"
 
+typedef boost::shared_ptr<Config> 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();
 };