Verified Commit da1cc865 authored by Andrei Pavel's avatar Andrei Pavel 🐧
Browse files

[#562] add an ordered_unique index to ClientClassContainer

parent 9187be72
......@@ -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
......
// 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
......@@ -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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment