Browse Source

[2565] Refactor code so that RRParamRegistry::textToClassCode() does not throw

Mukund Sivaraman 12 years ago
parent
commit
ed7622df90
3 changed files with 41 additions and 21 deletions
  1. 5 1
      src/lib/dns/rrclass.cc
  2. 24 12
      src/lib/dns/rrparamregistry-placeholder.cc
  3. 12 8
      src/lib/dns/rrparamregistry.h

+ 5 - 1
src/lib/dns/rrclass.cc

@@ -31,7 +31,11 @@ namespace isc {
 namespace dns {
 namespace dns {
 
 
 RRClass::RRClass(const std::string& classstr) {
 RRClass::RRClass(const std::string& classstr) {
-    classcode_ = RRParamRegistry::getRegistry().textToClassCode(classstr);
+     if (!RRParamRegistry::getRegistry().textToClassCode(classstr,
+                                                         classcode_)) {
+          isc_throw(InvalidRRClass,
+                    "Unrecognized RR parameter string: " + classstr);
+     }
 }
 }
 
 
 RRClass::RRClass(InputBuffer& buffer) {
 RRClass::RRClass(InputBuffer& buffer) {

+ 24 - 12
src/lib/dns/rrparamregistry-placeholder.cc

@@ -421,14 +421,15 @@ removeParam(uint16_t code, MC& codemap, MS& stringmap) {
     return (false);
     return (false);
 }
 }
 
 
-template <typename PT, typename MS, typename ET>
-inline uint16_t
-textToCode(const string& code_str, MS& stringmap) {
+template <typename PT, typename MS>
+inline bool
+textToCode(const string& code_str, MS& stringmap, uint16_t& class_code) {
     typename MS::const_iterator found;
     typename MS::const_iterator found;
 
 
     found = stringmap.find(code_str);
     found = stringmap.find(code_str);
     if (found != stringmap.end()) {
     if (found != stringmap.end()) {
-        return (found->second->code_);
+        class_code = found->second->code_;
+        return (true);
     }
     }
 
 
     size_t l = code_str.size();
     size_t l = code_str.size();
@@ -441,10 +442,12 @@ textToCode(const string& code_str, MS& stringmap) {
                                           l - PT::UNKNOWN_PREFIXLEN()));
                                           l - PT::UNKNOWN_PREFIXLEN()));
         iss >> dec >> code;
         iss >> dec >> code;
         if (iss.rdstate() == ios::eofbit && code <= PT::MAX_CODE) {
         if (iss.rdstate() == ios::eofbit && code <= PT::MAX_CODE) {
-            return (code);
+            class_code = code;
+            return (true);
         }
         }
     }
     }
-    isc_throw(ET, "Unrecognized RR parameter string: " + code_str);
+
+    return (false);
 }
 }
 
 
 template <typename PT, typename MC>
 template <typename PT, typename MC>
@@ -477,8 +480,15 @@ RRParamRegistry::removeType(uint16_t code) {
 
 
 uint16_t
 uint16_t
 RRParamRegistry::textToTypeCode(const string& type_string) const {
 RRParamRegistry::textToTypeCode(const string& type_string) const {
-    return (textToCode<RRTypeParam, StrRRTypeMap,
-            InvalidRRType>(type_string, impl_->str2typemap));
+    uint16_t code;
+
+    if (!textToCode<RRTypeParam, StrRRTypeMap>
+        (type_string, impl_->str2typemap, code)) {
+         isc_throw(InvalidRRType,
+                   "Unrecognized RR parameter string: " + type_string);
+    }
+
+    return (code);
 }
 }
 
 
 string
 string
@@ -499,10 +509,12 @@ RRParamRegistry::removeClass(uint16_t code) {
                                                        impl_->str2classmap));
                                                        impl_->str2classmap));
 }
 }
 
 
-uint16_t
-RRParamRegistry::textToClassCode(const string& class_string) const {
-    return (textToCode<RRClassParam, StrRRClassMap,
-            InvalidRRClass>(class_string, impl_->str2classmap));
+bool
+RRParamRegistry::textToClassCode(const string& class_string,
+                                 uint16_t& class_code) const
+{
+    return (textToCode<RRClassParam, StrRRClassMap>
+            (class_string, impl_->str2classmap, class_code));
 }
 }
 
 
 string
 string

+ 12 - 8
src/lib/dns/rrparamregistry.h

@@ -415,16 +415,20 @@ public:
     /// \brief Convert a textual representation of an RR class to the
     /// \brief Convert a textual representation of an RR class to the
     /// corresponding 16-bit integer code.
     /// corresponding 16-bit integer code.
     ///
     ///
-    /// This method searches the \c RRParamRegistry for the mapping from the
-    /// given textual representation of RR class to the corresponding integer
-    /// code.  If a mapping is found, it returns the associated class code;
-    /// otherwise, if the given string is in the form of "CLASSnnnn", it returns
-    /// the corresponding number as the class code; otherwise, it throws an
-    /// exception of class \c InvalidRRClass.
+    /// This method searches the \c RRParamRegistry for the mapping from
+    /// the given textual representation of RR class to the
+    /// corresponding integer code.  If a mapping is found, it returns
+    /// true with the associated class code in \c class_code; otherwise,
+    /// if the given string is in the form of "CLASSnnnn", it returns
+    /// true with the corresponding number as the class code in \c
+    /// class_code; otherwise, it returns false and \c class_code is
+    /// untouched.
     ///
     ///
     /// \param class_string The textual representation of the RR class.
     /// \param class_string The textual representation of the RR class.
-    /// \return The RR class code for \c class_string.
-    uint16_t textToClassCode(const std::string& class_string) const;
+    /// \param class_code Returns the RR class code in this argument.
+    /// \return true if conversion is successful, false otherwise.
+    bool textToClassCode(const std::string& class_string,
+			 uint16_t& class_code) const;
 
 
     /// \brief Convert class code into its textual representation.
     /// \brief Convert class code into its textual representation.
     ///
     ///