#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
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;
}
/// @}
+ /// @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
-// 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
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
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