helics  3.5.2
HandleManager.hpp
1 /*
2 Copyright (c) 2017-2024,
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 
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;
51  InterfaceType what,
52  std::string_view key,
53  std::string_view type,
54  std::string_view units);
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;
77  const BasicHandleInfo* getHandleInfo(InterfaceHandle handle) const;
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;
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
Definition: BasicHandleInfo.hpp:20
Definition: GlobalFederateId.hpp:75
Definition: GlobalFederateId.hpp:147
Definition: HandleManager.hpp:25
BasicHandleInfo * findHandle(GlobalHandle id)
Definition: HandleManager.cpp:148
void addHandleAtIndex(const BasicHandleInfo &otherHandle, int32_t index)
Definition: HandleManager.cpp:97
const std::unordered_map< std::string_view, std::vector< std::string_view > > & getAliases() const
Definition: HandleManager.hpp:115
void addAlias(std::string_view interfaceName, std::string_view alias)
Definition: HandleManager.cpp:400
void removeHandle(GlobalHandle handle)
Definition: HandleManager.cpp:47
BasicHandleInfo & addHandle(GlobalFederateId fed_id, InterfaceType what, std::string_view key, std::string_view type, std::string_view units)
Definition: HandleManager.cpp:13
void removeFederateHandles(GlobalFederateId fedToRemove)
Definition: HandleManager.cpp:88
BasicHandleInfo * getHandleInfo(int32_t index)
Definition: HandleManager.cpp:112
BasicHandleInfo * getInterfaceHandle(std::string_view name, InterfaceType type)
Definition: HandleManager.cpp:297
Definition: LocalFederateId.hpp:65
Definition: LocalFederateId.hpp:22
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
InterfaceType
Definition: CoreTypes.hpp:111