Kea 2.7.5
ha_relationship_mapper.h
Go to the documentation of this file.
1// Copyright (C) 2023-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 HA_RELATIONSHIP_MAPPER_H
8#define HA_RELATIONSHIP_MAPPER_H
9
10#include <config.h>
11
13#include <boost/shared_ptr.hpp>
14#include <unordered_map>
15#include <vector>
16
17namespace isc {
18namespace ha {
19
32template<typename MappedType>
34public:
35
37 typedef boost::shared_ptr<MappedType> MappedTypePtr;
38
43 void map(const std::string& key, MappedTypePtr obj) {
44 if (mapping_.count(key) > 0) {
45 isc_throw(InvalidOperation, "a relationship '" << key << "' already exists");
46 }
47 mapping_[key] = obj;
48
49 auto found = false;
50 for (auto const& o : vector_) {
51 if (o == obj) {
52 found = true;
53 break;
54 }
55 }
56 if (!found) {
57 vector_.push_back(obj);
58 }
59 }
60
65 MappedTypePtr get(const std::string& key) const {
66 auto obj = mapping_.find(key);
67 if (obj == mapping_.end()) {
68 return (MappedTypePtr());
69 }
70 return (obj->second);
71 }
72
78 if (vector_.empty()) {
79 isc_throw(InvalidOperation, "expected one relationship to be configured");
80 }
81 return (vector_[0]);
82 }
83
87 const std::vector<MappedTypePtr>& getAll() const {
88 return (vector_);
89 }
90
95 bool hasMultiple() const {
96 return (vector_.size() > 1);
97 }
98
99private:
100
102 std::unordered_map<std::string, MappedTypePtr> mapping_;
103
105 std::vector<MappedTypePtr> vector_;
106};
107
108} // end of namespace isc::ha
109} // end of namespace isc
110
111#endif // HA_RELATIONSHIP_MAPPER_H
A generic exception that is thrown if a function is called in a prohibited way.
Holds associations between objects and HA relationships.
void map(const std::string &key, MappedTypePtr obj)
Associates a key with the object.
const std::vector< MappedTypePtr > & getAll() const
Returns all mapped objects.
boost::shared_ptr< MappedType > MappedTypePtr
A pointer to the held object type.
MappedTypePtr get(const std::string &key) const
Retrieves mapped object by a key (e.g., partner name).
bool hasMultiple() const
Checks if the mapper has multiple objects.
MappedTypePtr get() const
Returns the default mapped object.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.