Browse Source

[2292] Get rid of the const_cast

It was needed when extracting data from a domain tree chain. The chain
now can hold mutable pointers too, so we use that (and some amount of
template bureaucracy) to avoid the cast.

While the interface changed (on the core find function, it is not
possible to pass const node chain and have a mutable node get out), it
doesn't seem to influence the current code. Also, it is a private
interface anyway, so it should be safe.
Michal 'vorner' Vaner 12 years ago
parent
commit
5c74085e2f
2 changed files with 28 additions and 20 deletions
  1. 27 19
      src/lib/datasrc/memory/domaintree.h
  2. 1 1
      src/lib/datasrc/memory/zone_finder.cc

+ 27 - 19
src/lib/datasrc/memory/domaintree.h

@@ -1085,9 +1085,10 @@ public:
     /// Acts as described in the \ref find section.
     /// Acts as described in the \ref find section.
     Result find(const isc::dns::Name& name,
     Result find(const isc::dns::Name& name,
                 DomainTreeNode<T>** node) const {
                 DomainTreeNode<T>** node) const {
-        DomainTreeNodeChain<T> node_path;
+        DomainTreeNodeChain<T, DomainTreeNode<T> > node_path;
         const isc::dns::LabelSequence ls(name);
         const isc::dns::LabelSequence ls(name);
-        return (find<void*>(ls, node, node_path, NULL, NULL));
+        return (find<void*, DomainTreeNode<T> >(ls, node, node_path, NULL,
+                                                NULL));
     }
     }
 
 
     /// \brief Simple find returning immutable node.
     /// \brief Simple find returning immutable node.
@@ -1097,9 +1098,11 @@ public:
     Result find(const isc::dns::Name& name,
     Result find(const isc::dns::Name& name,
                 const DomainTreeNode<T>** node) const {
                 const DomainTreeNode<T>** node) const {
         DomainTreeNodeChain<T> node_path;
         DomainTreeNodeChain<T> node_path;
-        DomainTreeNode<T> *target_node = NULL;
+        const DomainTreeNode<T> *target_node = NULL;
         const isc::dns::LabelSequence ls(name);
         const isc::dns::LabelSequence ls(name);
-        Result ret = (find<void*>(ls, &target_node, node_path, NULL, NULL));
+        Result ret = (find<void*, const DomainTreeNode<T> >(ls, &target_node,
+                                                            node_path, NULL,
+                                                            NULL));
         if (ret != NOTFOUND) {
         if (ret != NOTFOUND) {
             *node = target_node;
             *node = target_node;
         }
         }
@@ -1113,7 +1116,8 @@ public:
                 DomainTreeNodeChain<T>& node_path) const
                 DomainTreeNodeChain<T>& node_path) const
     {
     {
         const isc::dns::LabelSequence ls(name);
         const isc::dns::LabelSequence ls(name);
-        return (find<void*>(ls, node, node_path, NULL, NULL));
+        return (find<void*, const DomainTreeNode<T> >(ls, node, node_path,
+                                                      NULL, NULL));
     }
     }
 
 
     /// \brief Simple find returning immutable node, with node_path tracking
     /// \brief Simple find returning immutable node, with node_path tracking
@@ -1123,9 +1127,11 @@ public:
     Result find(const isc::dns::Name& name, const DomainTreeNode<T>** node,
     Result find(const isc::dns::Name& name, const DomainTreeNode<T>** node,
                 DomainTreeNodeChain<T>& node_path) const
                 DomainTreeNodeChain<T>& node_path) const
     {
     {
-        DomainTreeNode<T> *target_node = NULL;
+        const DomainTreeNode<T> *target_node = NULL;
         const isc::dns::LabelSequence ls(name);
         const isc::dns::LabelSequence ls(name);
-        Result ret = (find<void*>(ls, &target_node, node_path, NULL, NULL));
+        Result ret = (find<void*, const DomainTreeNode<T> >(ls, &target_node,
+                                                            node_path, NULL,
+                                                            NULL));
         if (ret != NOTFOUND) {
         if (ret != NOTFOUND) {
             *node = target_node;
             *node = target_node;
         }
         }
@@ -1143,7 +1149,7 @@ public:
                 bool (*callback)(const DomainTreeNode<T>&, CBARG),
                 bool (*callback)(const DomainTreeNode<T>&, CBARG),
                 CBARG callback_arg) const
                 CBARG callback_arg) const
     {
     {
-        DomainTreeNode<T>* target_node = NULL;
+        const DomainTreeNode<T>* target_node = NULL;
         const isc::dns::LabelSequence ls(name);
         const isc::dns::LabelSequence ls(name);
         Result ret = find(ls, &target_node, node_path, callback,
         Result ret = find(ls, &target_node, node_path, callback,
                           callback_arg);
                           callback_arg);
@@ -1227,10 +1233,10 @@ public:
     ///
     ///
     /// \return As in the description, but in case of callback returning
     /// \return As in the description, but in case of callback returning
     ///     \c true, it returns immediately with the current node.
     ///     \c true, it returns immediately with the current node.
-    template <typename CBARG>
+    template <typename CBARG, typename NodeType>
     Result find(const isc::dns::LabelSequence& target_labels_orig,
     Result find(const isc::dns::LabelSequence& target_labels_orig,
-                DomainTreeNode<T>** node,
-                DomainTreeNodeChain<T>& node_path,
+                NodeType** node,
+                DomainTreeNodeChain<T, NodeType>& node_path,
                 bool (*callback)(const DomainTreeNode<T>&, CBARG),
                 bool (*callback)(const DomainTreeNode<T>&, CBARG),
                 CBARG callback_arg) const;
                 CBARG callback_arg) const;
 
 
@@ -1245,9 +1251,11 @@ public:
                 bool (*callback)(const DomainTreeNode<T>&, CBARG),
                 bool (*callback)(const DomainTreeNode<T>&, CBARG),
                 CBARG callback_arg) const
                 CBARG callback_arg) const
     {
     {
-        DomainTreeNode<T>* target_node = NULL;
-        Result ret = find(target_labels, &target_node, node_path,
-                          callback, callback_arg);
+        const DomainTreeNode<T>* target_node = NULL;
+        Result ret = find<CBARG, const DomainTreeNode<T> >(target_labels,
+                                                           &target_node,
+                                                           node_path, callback,
+                                                           callback_arg);
         if (ret != NOTFOUND) {
         if (ret != NOTFOUND) {
             *node = target_node;
             *node = target_node;
         }
         }
@@ -1512,11 +1520,11 @@ DomainTree<T>::deleteHelper(util::MemorySegment& mem_sgmt,
 }
 }
 
 
 template <typename T>
 template <typename T>
-template <typename CBARG>
+template <typename CBARG, typename NodeType>
 typename DomainTree<T>::Result
 typename DomainTree<T>::Result
 DomainTree<T>::find(const isc::dns::LabelSequence& target_labels_orig,
 DomainTree<T>::find(const isc::dns::LabelSequence& target_labels_orig,
-                    DomainTreeNode<T>** target,
-                    DomainTreeNodeChain<T>& node_path,
+                    NodeType** target,
+                    DomainTreeNodeChain<T, NodeType>& node_path,
                     bool (*callback)(const DomainTreeNode<T>&, CBARG),
                     bool (*callback)(const DomainTreeNode<T>&, CBARG),
                     CBARG callback_arg) const
                     CBARG callback_arg) const
 {
 {
@@ -1526,11 +1534,11 @@ DomainTree<T>::find(const isc::dns::LabelSequence& target_labels_orig,
                   " and label sequence");
                   " and label sequence");
     }
     }
 
 
-    DomainTreeNode<T>* node;
+    NodeType* node;
 
 
     if (!node_path.isEmpty()) {
     if (!node_path.isEmpty()) {
         // Get the top node in the node chain
         // Get the top node in the node chain
-        node = const_cast<DomainTreeNode<T>*>(node_path.top());
+        node = node_path.top();
         // Start searching from its down pointer
         // Start searching from its down pointer
         node = node->getDown();
         node = node->getDown();
     } else {
     } else {

+ 1 - 1
src/lib/datasrc/memory/zone_finder.cc

@@ -417,7 +417,7 @@ FindNodeResult findNode(const ZoneData& zone_data,
                         ZoneFinder::FindOptions options,
                         ZoneFinder::FindOptions options,
                         bool out_of_zone_ok = false)
                         bool out_of_zone_ok = false)
 {
 {
-    ZoneNode* node = NULL;
+    const ZoneNode* node = NULL;
     FindState state((options & ZoneFinder::FIND_GLUE_OK) != 0);
     FindState state((options & ZoneFinder::FIND_GLUE_OK) != 0);
 
 
     const ZoneTree& tree(zone_data.getZoneTree());
     const ZoneTree& tree(zone_data.getZoneTree());