Browse Source

[2355]Created new template class ValueParser<> which implements
a simple-type parser. Replaces individual class definitions
for BooleanParser, Uint32Parser, and StringParser. Base class
uses new ValueStore<> storage template class.

Thomas Markwalder 12 years ago
parent
commit
f18722f2e3
2 changed files with 63 additions and 192 deletions
  1. 3 68
      src/lib/dhcpsrv/dhcp_parsers.cc
  2. 60 124
      src/lib/dhcpsrv/dhcp_parsers.h

+ 3 - 68
src/lib/dhcpsrv/dhcp_parsers.cc

@@ -75,23 +75,7 @@ void DebugParser::commit() {
 
 // **************************** BooleanParser  *************************
 
-BooleanParser::BooleanParser(const std::string& param_name, 
-                            BooleanStoragePtr storage)
-    : storage_(storage), param_name_(param_name), value_(false) {
-    // Empty parameter name is invalid.
-    if (param_name_.empty()) {
-        isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
-        << "empty parameter name provided");
-    }
-
-    // NUll storage is invalid.
-    if (!storage_) {
-        isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
-            << "storage may not be NULL");
-    }
-}
-
-void BooleanParser::build(ConstElementPtr value) {
+template<> void ValueParser<bool>::build(isc::data::ConstElementPtr value) {
     // The Config Manager checks if user specified a
     // valid value for a boolean parameter: True or False.
     // We should have a boolean Element, use value directly
@@ -103,30 +87,9 @@ void BooleanParser::build(ConstElementPtr value) {
     }
 }
 
-void BooleanParser::commit() {
-    // If a given parameter already exists in the storage we override
-    // its value. If it doesn't we insert a new element.
-    storage_->setParam(param_name_, value_);
-}
-
 // **************************** Uin32Parser  *************************
 
-Uint32Parser::Uint32Parser(const std::string& param_name, Uint32StoragePtr storage)
-    : storage_(storage), param_name_(param_name) {
-    // Empty parameter name is invalid.
-    if (param_name_.empty()) {
-        isc_throw(DhcpConfigError, "parser logic error:"
-            << "empty parameter name provided");
-    }
-
-    // NULL storage is invalid.
-    if (!storage_) {
-        isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
-            << "storage may not be NULL");
-    }
-}
-
-void Uint32Parser::build(ConstElementPtr value) {
+template<> void ValueParser<uint32_t>::build(ConstElementPtr value) {
     int64_t check;
     string x = value->str();
     try {
@@ -148,41 +111,13 @@ void Uint32Parser::build(ConstElementPtr value) {
     value_ = static_cast<uint32_t>(check);
 }
 
-void Uint32Parser::commit() {
-    // If a given parameter already exists in the storage we override
-    // its value. If it doesn't we insert a new element.
-    storage_->setParam(param_name_, value_);
-}
-
 // **************************** StringParser  *************************
 
-StringParser::StringParser(const std::string& param_name, 
-                           StringStoragePtr storage)
-    :storage_(storage), param_name_(param_name) {
-    // Empty parameter name is invalid.
-    if (param_name_.empty()) {
-        isc_throw(DhcpConfigError, "parser logic error:"
-                  << "empty parameter name provided");
-    }
-
-    // NULL storage is invalid.
-    if (!storage_) {
-        isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
-            << "storage may not be NULL");
-    }
-}
-
-void StringParser::build(ConstElementPtr value) {
+template <> void ValueParser<std::string>::build(ConstElementPtr value) {
     value_ = value->str();
     boost::erase_all(value_, "\"");
 }
 
-void StringParser::commit() {
-    // If a given parameter already exists in the storage we override
-    // its value. If it doesn't we insert a new element.
-    storage_->setParam(param_name_, value_);
-}
-
 // ******************** InterfaceListConfigParser *************************
 
 InterfaceListConfigParser::InterfaceListConfigParser(const std::string& 

+ 60 - 124
src/lib/dhcpsrv/dhcp_parsers.h

@@ -80,55 +80,16 @@ public:
     Option::Universe universe_;
 };
 
-// Pointers to various parser objects.
+/// @brief Pointer to various parser context.
 typedef boost::shared_ptr<ParserContext> ParserContextPtr;
 
-//brief a dummy configuration parser
-///
-/// It is a debugging parser. It does not configure anything,
-/// will accept any configuration and will just print it out
-/// on commit. Useful for debugging existing configurations and
-/// adding new ones.
-class DebugParser : public DhcpConfigParser {
-public:
-
-    /// @brief Constructor
-    ///
-    /// See @ref DhcpConfigParser class for details.
-    ///
-    /// @param param_name name of the parsed parameter
-    DebugParser(const std::string& param_name);
-
-    /// @brief builds parameter value
-    ///
-    /// See @ref DhcpConfigParser class for details.
-    ///
-    /// @param new_config pointer to the new configuration
-    virtual void build(isc::data::ConstElementPtr new_config);
-
-    /// @brief pretends to apply the configuration
-    ///
-    /// This is a method required by base class. It pretends to apply the
-    /// configuration, but in fact it only prints the parameter out.
-    ///
-    /// See @ref DhcpConfigParser class for details.
-    virtual void commit();
-
-private:
-    /// name of the parsed parameter
-    std::string param_name_;
-
-    /// pointer to the actual value of the parameter
-    isc::data::ConstElementPtr value_;
-
-};
-
-/// @brief A boolean value parser.
+/// @brief - TKM  - simple type parser template class 
 ///
 /// This parser handles configuration values of the boolean type.
 /// Parsed values are stored in a provided storage. If no storage
 /// is provided then the build function throws an exception.
-class BooleanParser : public DhcpConfigParser {
+template<typename ValueType>
+class ValueParser : public DhcpConfigParser {
 public:
 
     /// @brief Constructor.
@@ -139,116 +100,91 @@ public:
     /// @throw isc::dhcp::DhcpConfigError if a provided parameter's
     /// name is empty.
     /// @throw isc::dhcp::DhcpConfigError if storage is null.
-    BooleanParser(const std::string& param_name, BooleanStoragePtr storage);
+    ValueParser(const std::string& param_name, 
+        boost::shared_ptr<ValueStorage<ValueType> > storage)
+        : storage_(storage), param_name_(param_name), value_() {
+        // Empty parameter name is invalid.
+        if (param_name_.empty()) {
+            isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
+                << "empty parameter name provided");
+        }
+
+        // NUll storage is invalid.
+        if (!storage_) {
+            isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
+                << "storage may not be NULL");
+        }
+    }
+
 
     /// @brief Parse a boolean value.
     ///
     /// @param value a value to be parsed.
     ///
     /// @throw isc::BadValue if value is not a boolean type Element.
-    virtual void build(isc::data::ConstElementPtr value);
+    void build(isc::data::ConstElementPtr value);
 
     /// @brief Put a parsed value to the storage.
-    virtual void commit();
-
+    void commit() {
+        // If a given parameter already exists in the storage we override
+        // its value. If it doesn't we insert a new element.
+        storage_->setParam(param_name_, value_);
+    }
+    
 private:
     /// Pointer to the storage where parsed value is stored.
-    BooleanStoragePtr storage_;
+    boost::shared_ptr<ValueStorage<ValueType> > storage_;
 
     /// Name of the parameter which value is parsed with this parser.
     std::string param_name_;
+
     /// Parsed value.
-    bool value_;
+    ValueType value_;
 };
 
-/// @brief Configuration parser for uint32 parameters
-///
-/// This class is a generic parser that is able to handle any uint32 integer
-/// type. 
-/// Upon commit it stores the value in the external storage passed in
-/// during construction.  
-/// This class follows the parser interface, laid out
-/// in its base class, @ref DhcpConfigParser.
+/// @brief typedefs for simple data type parsers
+typedef ValueParser<bool> BooleanParser;
+typedef ValueParser<uint32_t> Uint32Parser;
+typedef ValueParser<std::string> StringParser;
+
+/// @brief a dummy configuration parser
 ///
-/// For overview of usability of this generic purpose parser, see
-/// @ref dhcpv4ConfigInherit page.
-class Uint32Parser : public DhcpConfigParser {
+/// It is a debugging parser. It does not configure anything,
+/// will accept any configuration and will just print it out
+/// on commit. Useful for debugging existing configurations and
+/// adding new ones.
+class DebugParser : public DhcpConfigParser {
 public:
 
-    /// @brief constructor for Uint32Parser
-    /// @param param_name name of the configuration parameter being parsed
-    /// @param storage is a pointer to the storage container where the parsed
-    /// value be stored upon commit.
-    /// @throw isc::dhcp::DhcpConfigError if a provided parameter's
-    /// name is empty.
-    /// @throw isc::dhcp::DhcpConfigError if storage is null.
-    Uint32Parser(const std::string& param_name, Uint32StoragePtr storage);
-
-    /// @brief Parses configuration configuration parameter as uint32_t.
+    /// @brief Constructor
     ///
-    /// @param value pointer to the content of parsed values
-    /// @throw BadValue if supplied value could not be base to uint32_t
-    ///        or the parameter name is empty.
-    virtual void build(isc::data::ConstElementPtr value);
-
-    /// @brief Stores the parsed uint32_t value in a storage.
-    virtual void commit();
-
-private:
-    /// pointer to the storage, where parsed value will be stored
-    Uint32StoragePtr storage_;
-
-    /// name of the parameter to be parsed
-    std::string param_name_;
-
-    /// the actual parsed value
-    uint32_t value_;
-};
-
-/// @brief Configuration parser for string parameters
-///
-/// This class is a generic parser that is able to handle any string
-/// parameter.  
-/// Upon commit it stores the value in the external storage passed in
-/// during construction.  
-///
-/// This class follows the parser interface, laid out
-/// in its base class, @ref DhcpConfigParser.
-///
-/// For overview of usability of this generic purpose parser, see
-/// @ref dhcpv4ConfigInherit page.
-class StringParser : public DhcpConfigParser {
-public:
-    /// @brief constructor for StringParser
-    /// @param param_name name of the configuration parameter being parsed
-    /// @param storage is a pointer to the storage container where the parsed
-    /// value be stored upon commit.
-    /// @throw isc::dhcp::DhcpConfigError if a provided parameter's
-    /// name is empty.
-    /// @throw isc::dhcp::DhcpConfigError if storage is null.
-    StringParser(const std::string& param_name, StringStoragePtr storage);
+    /// See @ref DhcpConfigParser class for details.
+    ///
+    /// @param param_name name of the parsed parameter
+    DebugParser(const std::string& param_name);
 
-    /// @brief parses parameter value
+    /// @brief builds parameter value
     ///
-    /// Parses configuration entry and stores it in storage. See
-    /// @ref setStorage() for details.
+    /// See @ref DhcpConfigParser class for details.
     ///
-    /// @param value pointer to the content of parsed values
-    /// @throw isc::BadValue if value is not a string type Element.
-    virtual void build(isc::data::ConstElementPtr value);
+    /// @param new_config pointer to the new configuration
+    virtual void build(isc::data::ConstElementPtr new_config);
 
-    /// @brief Stores the parsed value in a storage.
+    /// @brief pretends to apply the configuration
+    ///
+    /// This is a method required by base class. It pretends to apply the
+    /// configuration, but in fact it only prints the parameter out.
+    ///
+    /// See @ref DhcpConfigParser class for details.
     virtual void commit();
 
 private:
-    /// pointer to the storage, where parsed value will be stored
-    StringStoragePtr storage_;
-
-    /// name of the parameter to be parsed
+    /// name of the parsed parameter
     std::string param_name_;
 
-    /// the actual parsed value
-    std::string value_;
+    /// pointer to the actual value of the parameter
+    isc::data::ConstElementPtr value_;
+
 };
 
 /// @brief parser for interface list definition