| helics 3.6.1

HandleManager.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 #include "BasicHandleInfo.hpp"
9 #include "Core.hpp"
10 #include "helicsTime.hpp"
11
12 #include <deque>
13 #include <string>
14 #include <string_view>
15 #include <unordered_map>
16 #include <unordered_set>
17 #include <utility>
18 #include <vector>
19
20 namespace helics {
21
25 class HandleManager {
26 private:
27 using MapType = std::unordered_map<std::string_view, InterfaceHandle>;
35 std::deque<BasicHandleInfo> handles;
37 MapType publications;
39 MapType endpoints;
40 MapType inputs;
41 MapType filters;
42 std::unordered_map<std::uint64_t, int32_t> unique_ids;
44 std::unordered_map<std::string_view, std::vector<std::string_view>> aliases;
45 std::unordered_set<std::string> alias_names;
46 public:
48 HandleManager() = default;
50 BasicHandleInfo& addHandle(GlobalFederateId fed_id,
51 InterfaceType what,
52 std::string_view key,
53 std::string_view type,
54 std::string_view units);
56 BasicHandleInfo& addHandle(GlobalFederateId fed_id,
57 InterfaceHandle local_id,
58 InterfaceType what,
59 std::string_view key,
60 std::string_view type,
61 std::string_view units);
62
63 void addHandle(const BasicHandleInfo& otherHandle);
65 void addHandleAtIndex(const BasicHandleInfo& otherHandle, int32_t index);
67 void removeHandle(GlobalHandle handle);
69 void removeFederateHandles(GlobalFederateId fedToRemove);
71 BasicHandleInfo* getHandleInfo(int32_t index);
73 const BasicHandleInfo* getHandleInfo(int32_t index) const;
75 BasicHandleInfo* getHandleInfo(InterfaceHandle handle);
77 const BasicHandleInfo* getHandleInfo(InterfaceHandle handle) const;
79 BasicHandleInfo* findHandle(GlobalHandle id);
81 const BasicHandleInfo* findHandle(GlobalHandle id) const;
82
83 void setHandleOption(InterfaceHandle handle, int32_t option, int32_t val);
84
85 int32_t getHandleOption(InterfaceHandle handle, int32_t option) const;
87 BasicHandleInfo* getInterfaceHandle(std::string_view name, InterfaceType type);
89 const BasicHandleInfo* getInterfaceHandle(std::string_view name, InterfaceType type) const;
92 BasicHandleInfo* getInterfaceHandle(InterfaceHandle handle, InterfaceType type);
95 const BasicHandleInfo* getInterfaceHandle(InterfaceHandle handle, InterfaceType type) const;
96
97 LocalFederateId getLocalFedID(InterfaceHandle handle) const;
98
99 BasicHandleInfo& operator[](size_t index) { return handles[index]; }
100 const BasicHandleInfo& operator[](size_t index) const { return handles[index]; }
105 void addAlias(std::string_view interfaceName, std::string_view alias);
106 auto begin() { return handles.begin(); }
107 auto end() { return handles.end(); }
108 auto begin() const { return handles.begin(); }
109 auto end() const { return handles.end(); }
110 auto size() const { return handles.size(); }
111 /* search for handles based on a regex string and type*/
112 std::vector<GlobalHandle> regexSearch(const std::string& regexExpression,
113 InterfaceType type) const;
115 const std::unordered_map<std::string_view, std::vector<std::string_view>>& getAliases() const
116 {
117 return aliases;
118 }
119
120 private:
121 void addSearchFields(const BasicHandleInfo& handle, int32_t index);
122 std::string generateName(InterfaceType what) const;
123 // alias must be a stable string_view here
124 void addPublicationAlias(std::string_view interfaceName, std::string_view alias);
125 // alias must be a stable string_view here
126 void addInputAlias(std::string_view interfaceName, std::string_view alias);
127 // alias must be a stable string_view here
128 void addEndpointAlias(std::string_view interfaceName, std::string_view alias);
129 // alias must be a stable string_view here
130 void addFilterAlias(std::string_view interfaceName, std::string_view alias);
135 bool addAliasName(std::string_view interfaceName, std::string_view alias);
137 MapType& getMap(InterfaceType type);
138 const MapType& getMap(InterfaceType type) const;
139 };
140
141 } // namespace helics
helics::BasicHandleInfo
Definition: BasicHandleInfo.hpp:20
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:75
helics::GlobalHandle
Definition: GlobalFederateId.hpp:147
helics::HandleManager
Definition: HandleManager.hpp:25
helics::HandleManager::findHandle
BasicHandleInfo * findHandle(GlobalHandle id)
Definition: HandleManager.cpp:151
helics::HandleManager::addHandleAtIndex
void addHandleAtIndex(const BasicHandleInfo &otherHandle, int32_t index)
Definition: HandleManager.cpp:100
helics::HandleManager::getAliases
const std::unordered_map< std::string_view, std::vector< std::string_view > > & getAliases() const
Definition: HandleManager.hpp:115
helics::HandleManager::addAlias
void addAlias(std::string_view interfaceName, std::string_view alias)
Definition: HandleManager.cpp:403
helics::HandleManager::HandleManager
HandleManager()=default
helics::HandleManager::removeHandle
void removeHandle(GlobalHandle handle)
Definition: HandleManager.cpp:50
helics::HandleManager::addHandle
BasicHandleInfo & addHandle(GlobalFederateId fed_id, InterfaceType what, std::string_view key, std::string_view type, std::string_view units)
Definition: HandleManager.cpp:16
helics::HandleManager::removeFederateHandles
void removeFederateHandles(GlobalFederateId fedToRemove)
Definition: HandleManager.cpp:91
helics::HandleManager::getHandleInfo
BasicHandleInfo * getHandleInfo(int32_t index)
Definition: HandleManager.cpp:115
helics::HandleManager::getInterfaceHandle
BasicHandleInfo * getInterfaceHandle(std::string_view name, InterfaceType type)
Definition: HandleManager.cpp:300
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
helics::LocalFederateId
Definition: LocalFederateId.hpp:22
helicsTime.hpp
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::InterfaceType
InterfaceType
Definition: CoreTypes.hpp:112