]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#562] add an ordered_unique index to ClientClassContainer
authorAndrei Pavel <andrei@isc.org>
Fri, 6 May 2022 05:56:19 +0000 (08:56 +0300)
committerAndrei Pavel <andrei@isc.org>
Fri, 20 May 2022 17:38:56 +0000 (20:38 +0300)
src/lib/dhcp/classify.h
src/lib/dhcp/tests/pkt4_unittest.cc
src/lib/dhcp/tests/pkt6_unittest.cc

index d304dee6cc30d9c701ffeb0e3b09be2854cff7d9..4f3a0c140179b5b0997fef2362eba7fa8480d559 100644 (file)
@@ -7,10 +7,12 @@
 #ifndef CLASSIFY_H
 #define CLASSIFY_H
 
+#include <boost/multi_index_container.hpp>
 #include <boost/multi_index/hashed_index.hpp>
 #include <boost/multi_index/identity.hpp>
+#include <boost/multi_index/ordered_index.hpp>
 #include <boost/multi_index/sequenced_index.hpp>
-#include <boost/multi_index_container.hpp>
+
 #include <string>
 
 /// @file   classify.h
@@ -55,6 +57,10 @@ namespace dhcp {
             boost::multi_index::hashed_unique<
                 boost::multi_index::tag<ClassNameTag>,
                 boost::multi_index::identity<ClientClass>
+            >,
+            // Third index orders lexicographically.
+            boost::multi_index::ordered_unique<
+                boost::multi_index::identity<ClientClass>
             >
         >
     > ClientClassContainer;
@@ -131,6 +137,14 @@ namespace dhcp {
         }
         /// @}
 
+        /// @brief Returns an index that allows iteration through a sorted set
+        ///        of all the client classes.
+        ///
+        /// @return the index iterable through range-based for looping
+        ClientClassContainer::nth_index<2>::type const& sorted() const {
+            return container_.get<2>();
+        }
+
         /// @brief returns if class x belongs to the defined classes
         ///
         /// @param x client class to be checked
index d01849891eabb1c75da68324baa32ef43bbd5d39..7029abfbd25a17c6e37f9fd4f1dd1384b90dfcf3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -1386,4 +1386,28 @@ TEST_F(Pkt4Test, testSkipThisOptionError) {
     EXPECT_EQ("def", opstr->getValue());
 }
 
+// Check that sorted() allows sorted iteration.
+TEST_F(Pkt4Test, sortedClientClasses) {
+    // Add a bunch of classes to a packet.
+    Pkt4 packet(DHCPDISCOVER, 1234);
+    packet.addClass("foo");
+    packet.addClass("bar");
+    packet.addClass("2");
+    packet.addClass("10");
+
+    // Check that the usual iteration yields items in order of insertion.
+    std::string client_classes;
+    for (ClientClass const& c : packet.getClasses()) {
+        client_classes += c + ',';
+    }
+    EXPECT_EQ(client_classes, "ALL,foo,bar,2,10,");
+
+    // Check sorted() yields sorted items.
+    std::string sorted_client_classes;
+    for (ClientClass const& c : packet.getClasses().sorted()) {
+        sorted_client_classes += c + ',';
+    }
+    EXPECT_EQ(sorted_client_classes, "10,2,ALL,bar,foo,");
+}
+
 } // end of anonymous namespace
index c4878f15951f7662689bc47abc1e14b309fff4ab..9afd8f26719e779be64e26c98821d9e23a406798 100644 (file)
@@ -2115,4 +2115,28 @@ TEST_F(Pkt6Test, relayDataOption) {
     EXPECT_EQ(orig_data, clone_data);
 }
 
+// Check that sorted() allows sorted iteration.
+TEST_F(Pkt6Test, sortedClientClasses) {
+    // Add a bunch of classes to a packet.
+    Pkt6 packet(DHCPV6_SOLICIT, 1234);
+    packet.addClass("foo");
+    packet.addClass("bar");
+    packet.addClass("2");
+    packet.addClass("10");
+
+    // Check that the usual iteration yields items in order of insertion.
+    std::string client_classes;
+    for (ClientClass const& c : packet.getClasses()) {
+        client_classes += c + ',';
+    }
+    EXPECT_EQ(client_classes, "ALL,foo,bar,2,10,");
+
+    // Check sorted() yields sorted items.
+    std::string sorted_client_classes;
+    for (ClientClass const& c : packet.getClasses().sorted()) {
+        sorted_client_classes += c + ',';
+    }
+    EXPECT_EQ(sorted_client_classes, "10,2,ALL,bar,foo,");
 }
+
+}  // namespace