Kea 2.7.6
option_space_container.h
Go to the documentation of this file.
1// Copyright (C) 2013-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#ifndef OPTION_SPACE_CONTAINER_H
8#define OPTION_SPACE_CONTAINER_H
9
11#include <list>
12#include <string>
13
14namespace isc {
15namespace dhcp {
16
19
31template<typename ContainerType, typename ItemType, typename Selector>
33public:
34
36 typedef boost::shared_ptr<ContainerType> ItemsContainerPtr;
37
41 bool empty() const {
42 return (option_space_map_.empty());
43 }
44
49 void addItem(const ItemType& item, const Selector& option_space) {
50 ItemsContainerPtr items = getItems(option_space);
51 // Assume that the push_back() can't fail even when the
52 // ContainerType is a multi index container, i.e., assume
53 // there is no unique index which can raise a conflict.
54 static_cast<void>(items->push_back(item));
55 option_space_map_[option_space] = items;
56 }
57
67 ItemsContainerPtr getItems(const Selector& option_space) const {
68 const typename OptionSpaceMap::const_iterator& items =
69 option_space_map_.find(option_space);
70 if (items == option_space_map_.end()) {
71 return (ItemsContainerPtr(new ContainerType()));
72 }
73 return (items->second);
74 }
75
83 std::list<Selector> getOptionSpaceNames() const {
84 std::list<Selector> names;
85 for (typename OptionSpaceMap::const_iterator space =
86 option_space_map_.begin();
87 space != option_space_map_.end(); ++space) {
88 names.push_back(space->first);
89 }
90 return (names);
91 }
92
94 void clearItems() {
95 option_space_map_.clear();
96 }
97
112 uint64_t deleteItems(const uint64_t id) {
113 uint64_t num_deleted = 0;
114 for (auto const& space : option_space_map_) {
115 auto container = space.second;
116 auto& index = container->template get<OptionIdIndexTag>();
117 num_deleted += index.erase(id);
118 }
119
120 return (num_deleted);
121 }
122
132 bool equals(const OptionSpaceContainer& other) const {
133 for (typename OptionSpaceMap::const_iterator it =
134 option_space_map_.begin(); it != option_space_map_.end();
135 ++it) {
136
137 typename OptionSpaceMap::const_iterator other_it =
138 other.option_space_map_.find(it->first);
139 if (other_it == other.option_space_map_.end()) {
140 return (false);
141 }
142
143 // If containers have different sizes it is an indication that
144 // they are unequal.
145 if (it->second->size() != other_it->second->size()) {
146 return (false);
147 }
148
149 // If they have the same sizes, we have to compare each element.
150 for (typename ContainerType::const_iterator items_it =
151 it->second->begin();
152 items_it != it->second->end(); ++items_it) {
153
154 bool match_found = false;
155 for (typename ContainerType::const_iterator other_items_it =
156 other_it->second->begin();
157 other_items_it != other_it->second->end();
158 ++other_items_it) {
159 if (items_it->equals(*other_items_it)) {
160 match_found = true;
161 break;
162 }
163 }
164
165 if (!match_found) {
166 return (false);
167 }
168 }
169 }
170 return (true);
171 }
172
173private:
174
176 typedef std::map<Selector, ItemsContainerPtr> OptionSpaceMap;
177 OptionSpaceMap option_space_map_;
178};
179
180
181} // end of isc::dhcp namespace
182} // end of isc namespace
183
184#endif // OPTION_SPACE_CONTAINER_H
Simple container for option spaces holding various items.
boost::shared_ptr< ContainerType > ItemsContainerPtr
Pointer to the container.
uint64_t deleteItems(const uint64_t id)
Remove all options or option definitions with a given database identifier.
void addItem(const ItemType &item, const Selector &option_space)
Adds a new item to the option_space.
bool empty() const
Indicates the container is empty.
void clearItems()
Remove all items from the container.
std::list< Selector > getOptionSpaceNames() const
Get a list of existing option spaces.
ItemsContainerPtr getItems(const Selector &option_space) const
Get all items for the particular option space.
bool equals(const OptionSpaceContainer &other) const
Check if two containers are equal.
Defines the logger used by the top-level component of kea-lfc.
A tag for accessing DHCP options and definitions by id.