]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#490,!284] Use inheritance for all optional parameters in Network.
authorMarcin Siodelski <marcin@isc.org>
Tue, 26 Mar 2019 10:19:58 +0000 (11:19 +0100)
committerMarcin Siodelski <marcin@isc.org>
Wed, 27 Mar 2019 19:44:25 +0000 (20:44 +0100)
src/lib/dhcpsrv/network.cc
src/lib/dhcpsrv/network.h
src/lib/dhcpsrv/tests/network_unittest.cc
src/lib/util/optional.h

index 2c13a4779c3eae16d0623d3b8bdd7d9d63e68f16..59f599bf921ab4e6f1faf4e4cf52027c90586774 100644 (file)
@@ -107,8 +107,8 @@ Network::toElement() const {
     contextToElement(map);
 
     // Set interface
-    if (!getIface().unspecified()) {
-        map->set("interface", Element::create(getIface().get()));
+    if (!iface_name_.unspecified()) {
+        map->set("interface", Element::create(iface_name_.get()));
     }
 
     ElementPtr relay_map = Element::createMap();
@@ -122,8 +122,8 @@ Network::toElement() const {
     map->set("relay", relay_map);
 
     // Set client-class
-    if (!getClientClass().unspecified()) {
-        map->set("client-class", Element::create(getClientClass().get()));
+    if (!client_class_.unspecified()) {
+        map->set("client-class", Element::create(client_class_.get()));
     }
 
     // Set require-client-classes
@@ -140,22 +140,21 @@ Network::toElement() const {
     // T1, T2, and Valid are optional for SharedNetworks, and
     // T1 and T2 are optional for Subnet4 thus we will only
     // output them if they are marked as specified.
-    if (!getT1().unspecified()) {
+    if (!t1_.unspecified()) {
         map->set("renew-timer",
-                 Element::create(static_cast<long long>(getT1().get())));
+                 Element::create(static_cast<long long>(t1_.get())));
     }
 
     // Set rebind-timer
-    if (!getT2().unspecified()) {
+    if (!t2_.unspecified()) {
         map->set("rebind-timer",
-                 Element::create(static_cast<long long>(getT2().get())));
+                 Element::create(static_cast<long long>(t2_.get())));
     }
 
     // Set valid-lifetime
-    if (!getValid().unspecified()) {
+    if (!valid_.unspecified()) {
         map->set("valid-lifetime",
-                 Element::create(static_cast<long long>
-                                 (getValid().get())));
+                 Element::create(static_cast<long long>(valid_.get())));
     }
 
     // Set reservation mode
@@ -186,20 +185,17 @@ Network::toElement() const {
     ConstCfgOptionPtr opts = getCfgOption();
     map->set("option-data", opts->toElement());
 
-    // Output calcualte-tee-times and percentages if calculation is enabled.
-    auto calc_tee_times = getCalculateTeeTimes();
-    if (!calc_tee_times.unspecified()) {
-        map->set("calculate-tee-times", Element::create(calc_tee_times));
+    // Output calculate-tee-times and percentages if calculation is enabled.
+    if (!calculate_tee_times_.unspecified()) {
+        map->set("calculate-tee-times", Element::create(calculate_tee_times_));
     }
 
-    auto t1_percent = getT1Percent();
-    if (!t1_percent.unspecified()) {
-        map->set("t1-percent", Element::create(getT1Percent()));
+    if (!t1_percent_.unspecified()) {
+        map->set("t1-percent", Element::create(t1_percent_));
     }
 
-    auto t2_percent = getT2Percent();
-    if (!t2_percent.unspecified()) {
-        map->set("t2-percent", Element::create(getT2Percent()));
+    if (!t2_percent_.unspecified()) {
+        map->set("t2-percent", Element::create(t2_percent_));
     }
 
     return (map);
@@ -214,38 +210,23 @@ Network4::setSiaddr(const Optional<IOAddress>& siaddr) {
     siaddr_ = siaddr;
 }
 
-const Optional<IOAddress>&
-Network4::getSiaddr() const {
-    return (siaddr_);
-}
-
 void
 Network4::setSname(const Optional<std::string>& sname) {
     sname_ = sname;
 }
 
-const Optional<std::string>&
-Network4::getSname() const {
-    return (sname_);
-}
-
 void
 Network4::setFilename(const Optional<std::string>& filename) {
     filename_ = filename;
 }
 
-const Optional<std::string>&
-Network4::getFilename() const {
-    return (filename_);
-}
-
 ElementPtr
 Network4::toElement() const {
     ElementPtr map = Network::toElement();
 
     // Set match-client-id
-    if (!getMatchClientId().unspecified()) {
-        map->set("match-client-id", Element::create(getMatchClientId().get()));
+    if (!match_client_id_.unspecified()) {
+        map->set("match-client-id", Element::create(match_client_id_.get()));
     }
 
     // Set authoritative
@@ -254,18 +235,18 @@ Network4::toElement() const {
     }
 
     // Set next-server
-    if (!getSiaddr().unspecified()) {
-        map->set("next-server", Element::create(getSiaddr().get().toText()));
+    if (!siaddr_.unspecified()) {
+        map->set("next-server", Element::create(siaddr_.get().toText()));
     }
 
     // Set server-hostname
-    if (!getSname().unspecified()) {
-        map->set("server-hostname", Element::create(getSname().get()));
+    if (!sname_.unspecified()) {
+        map->set("server-hostname", Element::create(sname_.get()));
     }
 
     // Set boot-file-name
-    if (!getFilename().unspecified()) {
-        map->set("boot-file-name",Element::create(getFilename().get()));
+    if (!filename_.unspecified()) {
+        map->set("boot-file-name",Element::create(filename_.get()));
     }
 
     return (map);
index f3d3d9f05944f9a6d5abcb5830098cfe9903b0dd..583960745c9f05d06461d158215c867fa07d4373 100644 (file)
@@ -311,13 +311,13 @@ public:
     /// returned it is valid.
     ///
     /// @return client class @ref client_class_
-    const util::Optional<ClientClass>& getClientClass() const {
-        return (client_class_);
+    util::Optional<ClientClass> getClientClass() const {
+        return (getProperty<Network>(&Network::getClientClass, client_class_));
     }
 
     /// @brief Return valid-lifetime for addresses in that prefix
     Triplet<uint32_t> getValid() const {
-        return (valid_);
+        return (getProperty<Network>(&Network::getValid, valid_, "valid-lifetime"));
     }
 
     /// @brief Sets new valid lifetime for a network.
@@ -329,7 +329,7 @@ public:
 
     /// @brief Returns T1 (renew timer), expressed in seconds
     Triplet<uint32_t> getT1() const {
-        return (t1_);
+        return (getProperty<Network>(&Network::getT1, t1_, "renew-timer"));
     }
 
     /// @brief Sets new renew timer for a network.
@@ -341,7 +341,7 @@ public:
 
     /// @brief Returns T2 (rebind timer), expressed in seconds
     Triplet<uint32_t> getT2() const {
-        return (t2_);
+        return (getProperty<Network>(&Network::getT2, t2_, "rebind-timer"));
     }
 
     /// @brief Sets new rebind timer for a network.
@@ -388,7 +388,9 @@ public:
 
     /// @brief Returns whether or not T1/T2 calculation is enabled.
     util::Optional<bool> getCalculateTeeTimes() const {
-        return (calculate_tee_times_);
+        return (getProperty<Network>(&Network::getCalculateTeeTimes,
+                                     calculate_tee_times_,
+                                     "calculate-tee-times"));
     }
 
     /// @brief Sets whether or not T1/T2 calculation is enabled.
@@ -400,7 +402,9 @@ public:
 
     /// @brief Returns percentage to use when calculating the T1 (renew timer).
     util::Optional<double> getT1Percent() const {
-        return (t1_percent_);
+        return (getProperty<Network>(&Network::getT1Percent,
+                                     t1_percent_,
+                                     "t1-percent"));
     }
 
     /// @brief Sets new precentage for calculating T1 (renew timer).
@@ -412,7 +416,9 @@ public:
 
     /// @brief Returns percentage to use when calculating the T2 (rebind timer).
     util::Optional<double> getT2Percent() const {
-        return (t2_percent_);
+        return (getProperty<Network>(&Network::getT2Percent,
+                                     t2_percent_,
+                                     "t2-percent"));
     }
 
     /// @brief Sets new precentage for calculating T2 (rebind timer).
@@ -430,9 +436,10 @@ public:
 protected:
 
     template<typename BaseType, typename ReturnType>
-    util::Optional<ReturnType>
-    getProperty(util::Optional<ReturnType>(BaseType::*MethodPointer)() const,
-                util::Optional<ReturnType> property, const std::string& global_name) const {
+    ReturnType
+    getProperty(ReturnType(BaseType::*MethodPointer)() const,
+                ReturnType property,
+                const std::string& global_name = "") const {
         if (property.unspecified()) {
             auto parent = boost::dynamic_pointer_cast<BaseType>(parent_network_.lock());
             if (parent) {
@@ -443,12 +450,12 @@ protected:
                 }
             }
 
-            if (fetch_globals_fn_) {
+            if (!global_name.empty() && fetch_globals_fn_) {
                 data::ConstElementPtr globals = fetch_globals_fn_();
                 if (globals && (globals->getType() == data::Element::map)) {
                     data::ConstElementPtr global_param = globals->get(global_name);
                     if (global_param) {
-                        return (data::ElementExtractor<ReturnType>()(global_param));
+                        return (data::ElementExtractor<typename ReturnType::ValueType>()(global_param));
                     }
                 }
             }
@@ -532,7 +539,9 @@ public:
     ///
     /// @return true if client identifiers should be used, false otherwise.
     util::Optional<bool> getMatchClientId() const {
-        return (match_client_id_);
+        return (getProperty<Network4>(&Network4::getMatchClientId,
+                                      match_client_id_,
+                                      "match-client-id"));
     }
 
     /// @brief Sets the flag indicating if the client identifier should be
@@ -573,7 +582,9 @@ public:
     /// @brief Returns siaddr for this network.
     ///
     /// @return siaddr value
-    const util::Optional<asiolink::IOAddress>& getSiaddr() const;
+    util::Optional<asiolink::IOAddress> getSiaddr() const {
+        return (siaddr_);
+    }
 
     /// @brief Sets server hostname for the network.
     ///
@@ -583,7 +594,9 @@ public:
     /// @brief Returns server hostname for this network.
     ///
     /// @return server hostname value
-    const util::Optional<std::string>& getSname() const;
+    util::Optional<std::string> getSname() const {
+        return (sname_);
+    }
 
     /// @brief Sets boot file name for the network.
     ///
@@ -593,7 +606,9 @@ public:
     /// @brief Returns boot file name for this subnet
     ///
     /// @return boot file name value
-    const util::Optional<std::string>& getFilename() const;
+    util::Optional<std::string> getFilename() const {
+        return (filename_);
+    }
 
     /// @brief Unparses network object.
     ///
index 996a9ee7b6180a02f752576e7db1cada8bbcce29..a9d9a609f388254bd2c57ac59befe287ebe0fb4a 100644 (file)
@@ -42,6 +42,10 @@ public:
         return (globals_);
     }
 
+    template<typename>
+    void testNetworkInheritance() {
+    }
+
     ElementPtr globals_;
 };
 
index d75787a74b600e5c4d803d49c93489ada1f47e02..5d1cc98e0dfded4e504d14e687cbcbda70e420fb 100644 (file)
@@ -36,6 +36,9 @@ template<typename T>
 class Optional {
 public:
 
+    /// @brief Type of the encapsulated value.
+    typedef T ValueType;
+
     /// @brief Assigns a new value value and marks it "specified".
     ///
     /// @tparam A Type of the value to be assigned. Typically this is @c T, but