Kea 2.7.5
classify.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/data.h>
10#include <dhcp/classify.h>
11#include <util/str.h>
12
13#include <boost/algorithm/string/classification.hpp>
14#include <boost/algorithm/string/constants.hpp>
15#include <boost/algorithm/string/split.hpp>
16
17#include <sstream>
18#include <vector>
19
20namespace isc {
21namespace dhcp {
22
23using namespace isc::data;
24
25ClientClasses::ClientClasses(const std::string& class_names)
26 : container_() {
27 std::vector<std::string> split_text;
28 boost::split(split_text, class_names, boost::is_any_of(","),
29 boost::algorithm::token_compress_off);
30 for (size_t i = 0; i < split_text.size(); ++i) {
31 std::string trimmed = util::str::trim(split_text[i]);
32 // Ignore empty class names.
33 if (!trimmed.empty()) {
34 insert(ClientClass(trimmed));
35 }
36 }
37}
38
40 for (auto const& cclass : other) {
41 insert(cclass);
42 }
43}
44
45void
47 auto& idx = container_.get<ClassNameTag>();
48 auto it = idx.find(class_name);
49 if (it != idx.end()) {
50 static_cast<void>(idx.erase(it));
51 }
52}
53
54bool
56 auto const& idx = container_.get<ClassNameTag>();
57 return (idx.count(x) != 0);
58}
59
60std::string
61ClientClasses::toText(const std::string& separator) const {
62 std::stringstream s;
63 bool first = true;
64 for (auto const& class_it : *this) {
65 if (!first) {
66 s << separator;
67 } else {
68 first = false;
69 }
70 s << class_it;
71 }
72 return (s.str());
73}
74
78 for (auto const& c : container_) {
79 result->add(Element::create(c));
80 }
81 return (result);
82}
83
84void
86 if (cc_list) {
87 clear();
88 if (cc_list->getType() != Element::list) {
89 isc_throw(BadValue, "not a List element");
90 }
91
92 for (auto i = 0; i < cc_list->size(); ++i) {
93 auto cclass = cc_list->get(i);
94 if (cclass->getType() != Element::string) {
95 isc_throw(BadValue, "elements of list must be valid strings");
96 }
97
98 static_cast<void>(insert(cclass->stringValue()));
99 }
100 }
101}
102
103bool
105 return ((size() == other.size()) && std::equal(cbegin(), cend(), other.cbegin()));
106}
107
110 clear();
111 for (auto const& cclass : other) {
112 insert(cclass);
113 }
114
115 return (*this);
116}
117
118} // end of namespace isc::dhcp
119} // end of namespace isc
Defines elements for storing the names of client classes.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
Container for storing client class names.
Definition classify.h:108
bool equals(const ClientClasses &other) const
Compares two ClientClasses container for equality.
Definition classify.cc:104
void clear()
Clears containers.
Definition classify.h:210
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition classify.cc:55
void insert(const ClientClass &class_name)
Insert an element.
Definition classify.h:155
void erase(const ClientClass &class_name)
Erase element by name.
Definition classify.cc:46
ClientClasses & operator=(const ClientClasses &other)
Assigns the contents of on container to another.
Definition classify.cc:109
isc::data::ElementPtr toElement() const
Returns all class names as an ElementPtr of type ListElement.
Definition classify.cc:76
void fromElement(isc::data::ConstElementPtr list)
Sets contents from a ListElement.
Definition classify.cc:85
ClientClasses()
Default constructor.
Definition classify.h:116
std::string toText(const std::string &separator=", ") const
Returns all class names as text.
Definition classify.cc:61
const_iterator cbegin() const
Iterators to the first element.
Definition classify.h:179
const_iterator cend() const
Iterators to the past the end element.
Definition classify.h:192
size_t size() const
Returns the number of classes.
Definition classify.h:173
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
std::string ClientClass
Defines a single class name.
Definition classify.h:42
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
Defines the logger used by the top-level component of kea-lfc.
Tag for the name index.
Definition classify.h:48