Kea: ha_relationship_mapper.h Source File

| Kea 3.0.2-git

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
12#include <exceptions/exceptions.h>
13#include <boost/shared_ptr.hpp>
14#include <unordered_map>
15#include <vector>
16
17namespace isc {
18namespace ha {
19
32template<typename MappedType>
33 class HARelationshipMapper {
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
77 MappedTypePtr get() const {
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
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition exceptions/exceptions.h:143
isc::ha::HARelationshipMapper
Holds associations between objects and HA relationships.
Definition ha_relationship_mapper.h:33
isc::ha::HARelationshipMapper::map
void map(const std::string &key, MappedTypePtr obj)
Associates a key with the object.
Definition ha_relationship_mapper.h:43
isc::ha::HARelationshipMapper::getAll
const std::vector< MappedTypePtr > & getAll() const
Returns all mapped objects.
Definition ha_relationship_mapper.h:87
isc::ha::HARelationshipMapper< HAConfig >::MappedTypePtr
boost::shared_ptr< HAConfig > MappedTypePtr
Definition ha_relationship_mapper.h:37
isc::ha::HARelationshipMapper::get
MappedTypePtr get(const std::string &key) const
Retrieves mapped object by a key (e.g., partner name).
Definition ha_relationship_mapper.h:65
isc::ha::HARelationshipMapper::hasMultiple
bool hasMultiple() const
Checks if the mapper has multiple objects.
Definition ha_relationship_mapper.h:95
isc::ha::HARelationshipMapper::get
MappedTypePtr get() const
Returns the default mapped object.
Definition ha_relationship_mapper.h:77
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::ha
Definition command_creator.cc:20
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148