Browse Source

[2786] Use parent class functions to access and modify data in OptionCustom

Marcin Siodelski 12 years ago
parent
commit
e817fc7e97

+ 12 - 16
src/lib/dhcp/option_custom.cc

@@ -28,20 +28,20 @@ OptionCustom::OptionCustom(const OptionDefinition& def,
 }
 
 OptionCustom::OptionCustom(const OptionDefinition& def,
-                             Universe u,
-                             const OptionBuffer& data)
+                           Universe u,
+                           const OptionBuffer& data)
     : Option(u, def.getCode(), data.begin(), data.end()),
       definition_(def) {
-    createBuffers(data_);
+    createBuffers(getData());
 }
 
 OptionCustom::OptionCustom(const OptionDefinition& def,
-                             Universe u,
-                             OptionBufferConstIter first,
-                             OptionBufferConstIter last)
+                           Universe u,
+                           OptionBufferConstIter first,
+                           OptionBufferConstIter last)
     : Option(u, def.getCode(), first, last),
       definition_(def) {
-    createBuffers(data_);
+    createBuffers(getData());
 }
 
 void
@@ -507,9 +507,7 @@ OptionCustom::writeString(const std::string& text, const uint32_t index) {
 void
 OptionCustom::unpack(OptionBufferConstIter begin,
                      OptionBufferConstIter end) {
-    data_ = OptionBuffer(begin, end);
-    // Chop the buffer stored in data_ into set of sub buffers.
-    createBuffers(data_);
+    initialize(begin, end);
 }
 
 uint16_t
@@ -533,15 +531,13 @@ OptionCustom::len() {
     return (length);
 }
 
-void OptionCustom::setData(const OptionBufferConstIter first,
-                     const OptionBufferConstIter last) {
-    // We will copy entire option buffer, so we have to resize data_.
-    data_.resize(std::distance(first, last));
-    std::copy(first, last, data_.begin());
+void OptionCustom::initialize(const OptionBufferConstIter first,
+                              const OptionBufferConstIter last) {
+    setData(first, last);
 
     // Chop the data_ buffer into set of buffers that represent
     // option fields data.
-    createBuffers(data_);
+    createBuffers(getData());
 }
 
 std::string OptionCustom::toText(int indent) {

+ 7 - 2
src/lib/dhcp/option_custom.h

@@ -280,8 +280,8 @@ public:
     ///
     /// @param first iterator pointing to beginning of buffer to copy.
     /// @param last iterator pointing to end of buffer to copy.
-    void setData(const OptionBufferConstIter first,
-                 const OptionBufferConstIter last);
+    void initialize(const OptionBufferConstIter first,
+                    const OptionBufferConstIter last);
 
 private:
 
@@ -336,6 +336,11 @@ private:
     std::string dataFieldToText(const OptionDataType data_type,
                                 const uint32_t index) const;
 
+    /// Make this function private as we don't want it to be invoked
+    /// on OptionCustom object. We rather want that initialize to
+    /// be called instead.
+    using Option::setData;
+
     /// Option definition used to create an option.
     OptionDefinition definition_;
 

+ 2 - 2
src/lib/dhcp/tests/option_custom_unittest.cc

@@ -1324,7 +1324,7 @@ TEST_F(OptionCustomTest, unpack) {
 
 // The purpose of this test is to verify that new data can be set for
 // a custom option.
-TEST_F(OptionCustomTest, setData) {
+TEST_F(OptionCustomTest, initialize) {
     OptionDefinition opt_def("OPTION_FOO", 1000, "ipv6-address", true);
 
     // Initialize reference data.
@@ -1370,7 +1370,7 @@ TEST_F(OptionCustomTest, setData) {
     }
 
     // Replace the option data.
-    ASSERT_NO_THROW(option->setData(buf.begin(), buf.end()));
+    ASSERT_NO_THROW(option->initialize(buf.begin(), buf.end()));
 
     // Now we should have only 2 data fields.
     ASSERT_EQ(2, option->getDataFieldsNum());