Bring aboard an abstract class for configuration options.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 13 Sep 2011 01:24:53 +0000 (22:24 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 13 Sep 2011 01:24:53 +0000 (22:24 -0300)
- The subclasses shall reduce the size and complexity from
  ConfigurationOptions class, spliting its functionality among many classes

src/config/option/configurationoption.cpp [new file with mode: 0644]
src/config/option/configurationoption.h [new file with mode: 0644]

diff --git a/src/config/option/configurationoption.cpp b/src/config/option/configurationoption.cpp
new file mode 100644 (file)
index 0000000..da9bfbf
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/option/configurationoption.h"
+
+using namespace std;
+using boost::program_options::option_description;
+using boost::program_options::option_description;
+using boost::program_options::value_semantic;
+using boost::shared_ptr;
+
+//-----------------------------------------------------------------------------
+// ConfigurationOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Parameterized constructor.
+ *
+ * @param command_string Option name.
+ * @param command_description Option description.
+ */
+ConfigurationOption::ConfigurationOption(
+        const char *command_string,
+        const char *command_description
+) :
+    option( command_string, NULL, command_description )
+{
+}
+
+/**
+ * @brief Parameterized constructor.
+ *
+ * @param command_string Option name.
+ * @param semantic Specifies how the option's value is to be parsed and converted into C++ types.
+ * @param command_description Option description.
+ */
+ConfigurationOption::ConfigurationOption(
+        const char *command_string,
+        const value_semantic *semantic,
+        const char *command_description
+) :
+    option( command_string, semantic, command_description )
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+ConfigurationOption::~ConfigurationOption()
+{
+}
+
+/**
+ * @return The underlining @c boost::program_options::option_description object.
+ */
+shared_ptr<option_description> ConfigurationOption::get_option_description()
+{
+    return shared_ptr<option_description>( &option );
+}
+
+/**
+ * @return A string representing the command name.
+ */
+string ConfigurationOption::get_command_string() const
+{
+    return option.long_name();
+}
+
+/**
+ * @return A string representing the command description.
+ */
+string ConfigurationOption::get_command_description() const
+{
+    return option.description();
+}
diff --git a/src/config/option/configurationoption.h b/src/config/option/configurationoption.h
new file mode 100644 (file)
index 0000000..e7552e5
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef CONFIGURATION_OPTION_H
+#define CONFIGURATION_OPTION_H
+
+#include <string>
+#include <vector>
+
+#include <boost/program_options.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "config/configuration.h"
+
+//-----------------------------------------------------------------------------
+// ConfigurationOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Abstract class for configuration option.
+ */
+class ConfigurationOption
+{
+public:
+    ConfigurationOption(
+            const char *command_string,
+            const char *command_description
+    );
+    ConfigurationOption(
+            const char *command_string,
+            const boost::program_options::value_semantic *semantic,
+            const char *command_description
+    );
+    virtual ~ConfigurationOption();
+
+    boost::shared_ptr< boost::program_options::option_description > get_option_description();
+    std::string get_command_string() const;
+    std::string get_command_description() const;
+
+    virtual bool validate(
+            const boost::program_options::variables_map &vm
+    ) const = 0;
+
+    virtual bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    ) const = 0;
+
+private:
+    boost::program_options::option_description option;
+
+};
+
+//-----------------------------------------------------------------------------
+// ConfigurationOptionItem
+//-----------------------------------------------------------------------------
+
+typedef boost::shared_ptr< ConfigurationOption > ConfigurationOptionItem;
+
+//-----------------------------------------------------------------------------
+// ConfigurationOptionList
+//-----------------------------------------------------------------------------
+
+typedef std::vector< ConfigurationOptionItem > ConfigurationOptionList;
+
+#endif // CONFIGURATION_OPTION_H