Kea  2.3.3-git
classify.cc
Go to the documentation of this file.
1 // Copyright (C) 2014-2022 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/strutil.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 
20 namespace isc {
21 namespace dhcp {
22 
23 using namespace isc::data;
24 
25 ClientClasses::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 
39 void
40 ClientClasses::erase(const ClientClass& class_name) {
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 
48 bool
50  auto const& idx = container_.get<ClassNameTag>();
51  return (idx.count(x) != 0);
52 }
53 
54 std::string
55 ClientClasses::toText(const std::string& separator) const {
56  std::stringstream s;
57  for (const_iterator class_it = cbegin(); class_it != cend(); ++class_it) {
58  if (class_it != cbegin()) {
59  s << separator;
60  }
61  s << *class_it;
62  }
63  return (s.str());
64 }
65 
69  for (ClientClass c : container_) {
70  result->add(Element::create(c));
71  }
72  return (result);
73 }
74 
75 } // end of namespace isc::dhcp
76 } // end of namespace isc
std::string toText(const std::string &separator=", ") const
Returns all class names as text.
Definition: classify.cc:55
isc::data::ElementPtr toElement() const
Returns all class names as an ElementPtr of type ListElement.
Definition: classify.cc:67
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.cc:49
Defines elements for storing the names of client classes.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
const_iterator cbegin() const
Iterators to the first element.
Definition: classify.h:152
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:286
ClientClassContainer::const_iterator const_iterator
Type of iterators.
Definition: classify.h:112
Defines the logger used by the top-level component of kea-lfc.
ClientClasses()
Default constructor.
Definition: classify.h:116
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:241
void erase(const ClientClass &class_name)
Erase element by name.
Definition: classify.cc:40
const_iterator cend() const
Iterators to the past the end element.
Definition: classify.h:165
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
void insert(const ClientClass &class_name)
Insert an element.
Definition: classify.h:128
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
Tag for the name index.
Definition: classify.h:48