Kea 2.5.8
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
39void
41 auto& idx = container_.get<ClassNameTag>();
42 auto it = idx.find(class_name);
43 if (it != idx.end()) {
44 static_cast<void>(idx.erase(it));
45 }
46}
47
48bool
50 auto const& idx = container_.get<ClassNameTag>();
51 return (idx.count(x) != 0);
52}
53
54std::string
55ClientClasses::toText(const std::string& separator) const {
56 std::stringstream s;
57 bool first = true;
58 for (auto const& class_it : *this) {
59 if (!first) {
60 s << separator;
61 } else {
62 first = false;
63 }
64 s << class_it;
65 }
66 return (s.str());
67}
68
72 for (auto const& c : container_) {
73 result->add(Element::create(c));
74 }
75 return (result);
76}
77
78} // end of namespace isc::dhcp
79} // end of namespace isc
Defines elements for storing the names of client classes.
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
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.cc:49
void insert(const ClientClass &class_name)
Insert an element.
Definition: classify.h:128
void erase(const ClientClass &class_name)
Erase element by name.
Definition: classify.cc:40
isc::data::ElementPtr toElement() const
Returns all class names as an ElementPtr of type ListElement.
Definition: classify.cc:70
ClientClasses()
Default constructor.
Definition: classify.h:116
std::string toText(const std::string &separator=", ") const
Returns all class names as text.
Definition: classify.cc:55
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