From: Guilherme Maciel Ferreira Date: Tue, 13 Sep 2011 01:24:53 +0000 (-0300) Subject: Bring aboard an abstract class for configuration options. X-Git-Tag: v1.3~11^2~34^2~30 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=79add24b53fb820839dbd0202929411c8eea09b8;p=pingcheck Bring aboard an abstract class for configuration options. - The subclasses shall reduce the size and complexity from ConfigurationOptions class, spliting its functionality among many classes --- diff --git a/src/config/option/configurationoption.cpp b/src/config/option/configurationoption.cpp new file mode 100644 index 0000000..da9bfbf --- /dev/null +++ b/src/config/option/configurationoption.cpp @@ -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 ConfigurationOption::get_option_description() +{ + return shared_ptr( &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 index 0000000..e7552e5 --- /dev/null +++ b/src/config/option/configurationoption.h @@ -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 +#include + +#include +#include + +#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