helics  3.5.2
GlobalFederateId.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 
8 #pragma once
9 #include "LocalFederateId.hpp"
10 
11 namespace helics {
27 
31  public:
32  using BaseType = IdentifierBaseType;
34  constexpr GlobalBrokerId() = default;
35 
36  constexpr explicit GlobalBrokerId(BaseType val) noexcept: gid(val) {}
38  constexpr BaseType baseValue() const { return gid; }
40  constexpr bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
42  constexpr bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
44  constexpr bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
45 
46  constexpr bool isFederate() const
47  {
48  return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
49  }
50  constexpr bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
51  constexpr bool isValid() const
52  {
53  return (gid != invalid_global_broker_id && gid != detail::gInvalidInterfaceHandle);
54  }
55  BaseType localIndex() const
56  {
57  return (gid >= gGlobalBrokerIdShift) ? (gid - gGlobalBrokerIdShift) : gid;
58  }
59 
60  private:
61  BaseType gid = invalid_global_broker_id;
62  friend class GlobalFederateId; // for allowing comparison operators to work well
63  static constexpr BaseType invalid_global_broker_id{-2'010'000'000};
64 };
65 
70 
73 std::ostream& operator<<(std::ostream& os, GlobalBrokerId id);
76  public:
77  using BaseType = IdentifierBaseType;
79  constexpr GlobalFederateId() = default;
80 
81  constexpr explicit GlobalFederateId(BaseType val) noexcept: gid(val) {}
83  constexpr GlobalFederateId(GlobalBrokerId id) noexcept: gid(id.gid) {} // NOLINT
84 
85  constexpr operator GlobalBrokerId() const noexcept { return GlobalBrokerId{gid}; } // NOLINT
87  constexpr BaseType baseValue() const { return gid; }
89  constexpr bool operator==(GlobalFederateId id) const noexcept { return (gid == id.gid); }
91  constexpr bool operator!=(GlobalFederateId id) const noexcept { return (gid != id.gid); }
93  constexpr bool operator<(GlobalFederateId id) const noexcept { return (gid < id.gid); }
95  constexpr bool operator>(GlobalFederateId id) const noexcept { return (gid > id.gid); }
97  constexpr bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
99  constexpr bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
101  constexpr bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
103  constexpr bool operator>(GlobalBrokerId id) const noexcept { return (gid > id.gid); }
105  constexpr bool isFederate() const
106  {
107  return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
108  }
110  constexpr bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
112  constexpr bool isValid() const
113  {
114  return (gid != invalid_global_fed_id && gid != detail::gInvalidInterfaceHandle);
115  }
118  constexpr BaseType localIndex() const
119  {
120  return (gid >= gGlobalFederateIdShift) ? (gid - gGlobalFederateIdShift) : gid;
121  }
122 
124  BaseType* getBaseTypePointer() { return &gid; }
125 
126  private:
127  static constexpr BaseType invalid_global_fed_id{-2'010'000'000};
128  BaseType gid{invalid_global_fed_id};
129 };
131 constexpr GlobalFederateId gDirectCoreId{-235262};
132 
135  GlobalBrokerId::BaseType index)
136 {
137  // allow 3 special federates per subbroker/core + 3 special ones for the root broker
138  return (broker != gRootBrokerID) ?
140  3 * (broker.baseValue() - gGlobalBrokerIdShift + 2) + index)) :
142 }
144 std::ostream& operator<<(std::ostream& os, GlobalFederateId id);
145 
148  public:
152  constexpr GlobalHandle() = default;
154  constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand): fed_id(fed), handle(hand) {}
157  explicit operator uint64_t() const
158  {
159  auto key = static_cast<uint64_t>(fed_id.baseValue()) << 32U;
160  key += static_cast<uint64_t>(handle.baseValue()) & (0x0000'0000'FFFF'FFFF);
161  return key;
162  }
164  constexpr bool operator==(GlobalHandle id) const noexcept
165  {
166  return ((fed_id == id.fed_id) && (handle == id.handle));
167  }
169  constexpr bool operator!=(GlobalHandle id) const noexcept
170  {
171  return ((fed_id != id.fed_id) || (handle != id.handle));
172  }
174  constexpr bool operator<(GlobalHandle id) const noexcept
175  {
176  return (fed_id < id.fed_id) ? true : ((fed_id != id.fed_id) ? false : (handle < id.handle));
177  }
179  constexpr bool isValid() const { return fed_id.isValid() && handle.isValid(); }
180 };
181 
184 std::ostream& operator<<(std::ostream& os, GlobalHandle id);
185 
187 class route_id {
188  public:
189  using BaseType = IdentifierBaseType;
191  constexpr route_id() = default;
193  constexpr explicit route_id(BaseType val) noexcept: rid(val) {}
195  constexpr BaseType baseValue() const { return rid; }
197  constexpr bool operator==(route_id id) const noexcept { return (rid == id.rid); }
199  constexpr bool operator!=(route_id id) const noexcept { return (rid != id.rid); }
201  constexpr bool operator<(route_id id) const noexcept { return (rid < id.rid); }
203  constexpr bool isValid() const { return (rid != invalid_route_id); }
204 
205  private:
206  static constexpr BaseType invalid_route_id{-1'295'148'000};
207  BaseType rid{invalid_route_id};
208 };
209 
210 constexpr route_id parent_route_id{0};
211 constexpr route_id control_route{-1};
212 
213 constexpr route_id generateRouteId(int32_t route_type_code, int32_t index)
214 {
215  return route_id(route_type_code * 256 * 256 * 256 + index);
216 }
217 
218 constexpr int32_t getRouteTypeCode(route_id rid)
219 {
220  return (rid.baseValue() >> 24);
221 }
222 
223 constexpr int32_t normal_route_code{0};
224 constexpr int32_t json_route_code{10};
227 std::ostream& operator<<(std::ostream& os, route_id id);
228 
229 } // namespace helics
230 
231 namespace std {
234 template<>
235 struct hash<helics::GlobalFederateId> {
237  using result_type = std::size_t;
239  result_type operator()(argument_type const& key) const noexcept
240  {
241  return std::hash<helics::GlobalFederateId::BaseType>{}(key.baseValue());
242  }
243 };
244 
247 template<>
248 struct hash<helics::GlobalBrokerId> {
250  using result_type = std::size_t;
252  result_type operator()(argument_type const& key) const noexcept
253  {
254  return std::hash<helics::GlobalBrokerId::BaseType>{}(key.baseValue());
255  }
256 };
257 
260 template<>
261 struct hash<helics::route_id> {
263  using result_type = std::size_t;
265  result_type operator()(argument_type const& key) const noexcept
266  {
267  return std::hash<helics::route_id::BaseType>{}(key.baseValue());
268  }
269 };
270 
274 template<>
275 struct hash<helics::GlobalHandle> {
277  using result_type = std::size_t;
279  result_type operator()(argument_type const& key) const noexcept
280  {
281  return std::hash<uint64_t>{}(static_cast<uint64_t>(key));
282  }
283 };
284 
285 } // namespace std
Definition: GlobalFederateId.hpp:30
constexpr bool operator==(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:40
constexpr bool operator<(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:44
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:38
constexpr bool operator!=(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:42
constexpr GlobalBrokerId()=default
Definition: GlobalFederateId.hpp:75
constexpr bool isFederate() const
Definition: GlobalFederateId.hpp:105
constexpr bool operator>(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:95
constexpr bool operator<(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:101
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:112
constexpr GlobalFederateId(GlobalBrokerId id) noexcept
Definition: GlobalFederateId.hpp:83
constexpr bool operator==(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:89
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:87
constexpr bool operator!=(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:91
constexpr bool operator>(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:103
constexpr BaseType localIndex() const
Definition: GlobalFederateId.hpp:118
BaseType * getBaseTypePointer()
Definition: GlobalFederateId.hpp:124
constexpr bool operator==(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:97
constexpr bool operator<(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:93
constexpr GlobalFederateId()=default
constexpr bool operator!=(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:99
constexpr bool isBroker() const
Definition: GlobalFederateId.hpp:110
Definition: GlobalFederateId.hpp:147
constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand)
Definition: GlobalFederateId.hpp:154
GlobalFederateId fed_id
the federate id component
Definition: GlobalFederateId.hpp:149
InterfaceHandle handle
the interface handle component
Definition: GlobalFederateId.hpp:150
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:179
constexpr bool operator<(GlobalHandle id) const noexcept
Definition: GlobalFederateId.hpp:174
constexpr bool operator==(GlobalHandle id) const noexcept
Definition: GlobalFederateId.hpp:164
constexpr bool operator!=(GlobalHandle id) const noexcept
Definition: GlobalFederateId.hpp:169
constexpr GlobalHandle()=default
Definition: LocalFederateId.hpp:65
constexpr BaseType baseValue() const
Definition: LocalFederateId.hpp:73
Definition: GlobalFederateId.hpp:187
constexpr route_id(BaseType val) noexcept
Definition: GlobalFederateId.hpp:193
constexpr bool operator==(route_id id) const noexcept
Definition: GlobalFederateId.hpp:197
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:195
constexpr route_id()=default
constexpr bool operator<(route_id id) const noexcept
Definition: GlobalFederateId.hpp:201
constexpr bool operator!=(route_id id) const noexcept
Definition: GlobalFederateId.hpp:199
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:203
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
constexpr IdentifierBaseType gGlobalPriorityBlockSize
Definition: GlobalFederateId.hpp:26
constexpr GlobalFederateId getSpecialFederateId(GlobalBrokerId broker, GlobalBrokerId::BaseType index)
Definition: GlobalFederateId.hpp:134
std::ostream & operator<<(std::ostream &out, const ActionMessage &command)
Definition: ActionMessage.cpp:918
constexpr GlobalBrokerId gRootBrokerID
Definition: GlobalFederateId.hpp:69
constexpr GlobalFederateId gDirectCoreId
Definition: GlobalFederateId.hpp:131
constexpr IdentifierBaseType gGlobalBrokerIdShift
Definition: GlobalFederateId.hpp:24
constexpr GlobalBrokerId parent_broker_id
Definition: GlobalFederateId.hpp:67
int32_t IdentifierBaseType
Definition: LocalFederateId.hpp:15
constexpr IdentifierBaseType gGlobalFederateIdShift
Definition: GlobalFederateId.hpp:22
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:250
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:252
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:239
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:237
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:279
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:277
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:263
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:265