Kea  2.1.7-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 #include <dhcp/classify.h>
9 #include <util/strutil.h>
10 #include <boost/algorithm/string/classification.hpp>
11 #include <boost/algorithm/string/constants.hpp>
12 #include <boost/algorithm/string/split.hpp>
13 #include <sstream>
14 #include <vector>
15 
16 namespace isc {
17 namespace dhcp {
18 
19 ClientClasses::ClientClasses(const std::string& class_names)
20  : container_() {
21  std::vector<std::string> split_text;
22  boost::split(split_text, class_names, boost::is_any_of(","),
23  boost::algorithm::token_compress_off);
24  for (size_t i = 0; i < split_text.size(); ++i) {
25  std::string trimmed = util::str::trim(split_text[i]);
26  // Ignore empty class names.
27  if (!trimmed.empty()) {
28  insert(ClientClass(trimmed));
29  }
30  }
31 }
32 
33 void
34 ClientClasses::erase(const ClientClass& class_name) {
35  auto& idx = container_.get<ClassNameTag>();
36  auto it = idx.find(class_name);
37  if (it != idx.end()) {
38  static_cast<void>(idx.erase(it));
39  }
40 }
41 
42 bool
44  auto const& idx = container_.get<ClassNameTag>();
45  return (idx.count(x) != 0);
46 }
47 
48 std::string
49 ClientClasses::toText(const std::string& separator) const {
50  std::stringstream s;
51  for (const_iterator class_it = cbegin(); class_it != cend(); ++class_it) {
52  if (class_it != cbegin()) {
53  s << separator;
54  }
55  s << *class_it;
56  }
57  return (s.str());
58 }
59 
60 } // end of namespace isc::dhcp
61 } // end of namespace isc
std::string toText(const std::string &separator=", ") const
Returns all class names as text.
Definition: classify.cc:49
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.cc:43
Defines elements for storing the names of client classes.
const_iterator cbegin() const
Iterators to the first element.
Definition: classify.h:112
ClientClassContainer::const_iterator const_iterator
Type of iterators.
Definition: classify.h:72
Defines the logger used by the top-level component of kea-lfc.
ClientClasses()
Default constructor.
Definition: classify.h:76
void erase(const ClientClass &class_name)
Erase element by name.
Definition: classify.cc:34
const_iterator cend() const
Iterators to the past the end element.
Definition: classify.h:125
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:88
std::string ClientClass
Defines a single class name.
Definition: classify.h:40
Tag for the name index.
Definition: classify.h:46