helics  3.3.0
GlobalFederateId.hpp
1 /*
2 Copyright (c) 2017-2022,
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 {
23 constexpr IdentifierBaseType gGlobalBrokerIdShift{0x7000'0000};
24 
28  public:
29  using BaseType = IdentifierBaseType;
31  constexpr GlobalBrokerId() = default;
32 
33  constexpr explicit GlobalBrokerId(BaseType val) noexcept: gid(val) {}
35  constexpr BaseType baseValue() const { return gid; }
37  constexpr bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
39  constexpr bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
41  constexpr bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
42 
43  constexpr bool isFederate() const
44  {
45  return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
46  }
47  constexpr bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
48  constexpr bool isValid() const
49  {
50  return (gid != invalid_global_broker_id && gid != detail::gInvalidInterfaceHandle);
51  }
52  BaseType localIndex() const
53  {
54  return (gid >= gGlobalBrokerIdShift) ? (gid - gGlobalBrokerIdShift) : gid;
55  }
56 
57  private:
58  BaseType gid = invalid_global_broker_id;
59  friend class GlobalFederateId; // for allowing comparison operators to work well
60  static constexpr BaseType invalid_global_broker_id{-2'010'000'000};
61 };
62 
64 constexpr GlobalBrokerId parent_broker_id{0};
66 constexpr GlobalBrokerId gRootBrokerID{1};
67 
70 std::ostream& operator<<(std::ostream& os, GlobalBrokerId id);
72 class GlobalFederateId {
73  public:
74  using BaseType = IdentifierBaseType;
76  constexpr GlobalFederateId() = default;
77 
78  constexpr explicit GlobalFederateId(BaseType val) noexcept: gid(val) {}
80  constexpr GlobalFederateId(GlobalBrokerId id) noexcept: gid(id.gid) {} // NOLINT
81 
82  constexpr operator GlobalBrokerId() const noexcept { return GlobalBrokerId{gid}; } // NOLINT
84  constexpr BaseType baseValue() const { return gid; }
86  constexpr bool operator==(GlobalFederateId id) const noexcept { return (gid == id.gid); }
88  constexpr bool operator!=(GlobalFederateId id) const noexcept { return (gid != id.gid); }
90  constexpr bool operator<(GlobalFederateId id) const noexcept { return (gid < id.gid); }
92  constexpr bool operator>(GlobalFederateId id) const noexcept { return (gid > id.gid); }
94  constexpr bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
96  constexpr bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
98  constexpr bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
100  constexpr bool operator>(GlobalBrokerId id) const noexcept { return (gid > id.gid); }
102  constexpr bool isFederate() const
103  {
104  return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
105  }
107  constexpr bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
109  constexpr bool isValid() const
110  {
111  return (gid != invalid_global_fed_id && gid != detail::gInvalidInterfaceHandle);
112  }
115  constexpr BaseType localIndex() const
116  {
117  return (gid >= gGlobalFederateIdShift) ? (gid - gGlobalFederateIdShift) : gid;
118  }
119 
121  BaseType* getBaseTypePointer() { return &gid; }
122 
123  private:
124  static constexpr BaseType invalid_global_fed_id{-2'010'000'000};
125  BaseType gid{invalid_global_fed_id};
126 };
128 constexpr GlobalFederateId gDirectCoreId{-235262};
129 
132  GlobalBrokerId::BaseType index)
133 {
134  // allow 3 special federates per subbroker/core + 3 special ones for the root broker
135  return (broker != gRootBrokerID) ?
137  3 * (broker.baseValue() - gGlobalBrokerIdShift + 2) + index)) :
139 }
141 std::ostream& operator<<(std::ostream& os, GlobalFederateId id);
142 
145  public:
148 
149  constexpr GlobalHandle() = default;
151  constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand): fed_id(fed), handle(hand) {}
154  explicit operator uint64_t() const
155  {
156  auto key = static_cast<uint64_t>(fed_id.baseValue()) << 32U;
157  key += static_cast<uint64_t>(handle.baseValue()) & (0x0000'0000'FFFF'FFFF);
158  return key;
159  }
161  constexpr bool operator==(GlobalHandle id) const noexcept
162  {
163  return ((fed_id == id.fed_id) && (handle == id.handle));
164  }
166  constexpr bool operator!=(GlobalHandle id) const noexcept
167  {
168  return ((fed_id != id.fed_id) || (handle != id.handle));
169  }
171  constexpr bool operator<(GlobalHandle id) const noexcept
172  {
173  return (fed_id < id.fed_id) ? true : ((fed_id != id.fed_id) ? false : (handle < id.handle));
174  }
176  constexpr bool isValid() const { return fed_id.isValid() && handle.isValid(); }
177 };
178 
181 std::ostream& operator<<(std::ostream& os, GlobalHandle id);
182 
184 class route_id {
185  public:
186  using BaseType = IdentifierBaseType;
188  constexpr route_id() = default;
190  constexpr explicit route_id(BaseType val) noexcept: rid(val) {}
192  constexpr BaseType baseValue() const { return rid; }
194  constexpr bool operator==(route_id id) const noexcept { return (rid == id.rid); }
196  constexpr bool operator!=(route_id id) const noexcept { return (rid != id.rid); }
198  constexpr bool operator<(route_id id) const noexcept { return (rid < id.rid); }
200  constexpr bool isValid() const { return (rid != invalid_route_id); }
201 
202  private:
203  static constexpr BaseType invalid_route_id{-1'295'148'000};
204  BaseType rid{invalid_route_id};
205 };
206 
207 constexpr route_id parent_route_id{0};
208 constexpr route_id control_route{-1};
209 
210 constexpr route_id generateRouteId(int32_t route_type_code, int32_t index)
211 {
212  return route_id(route_type_code * 256 * 256 * 256 + index);
213 }
214 
215 constexpr int32_t getRouteTypeCode(route_id rid)
216 {
217  return (rid.baseValue() >> 24);
218 }
219 
220 constexpr int32_t normal_route_code{0};
221 constexpr int32_t json_route_code{10};
224 std::ostream& operator<<(std::ostream& os, route_id id);
225 
226 } // namespace helics
227 
228 namespace std {
231 template<>
232 struct hash<helics::GlobalFederateId> {
234  using result_type = std::size_t;
235 
236  result_type operator()(argument_type const& key) const noexcept
237  {
238  return std::hash<helics::GlobalFederateId::BaseType>{}(key.baseValue());
239  }
240 };
241 
244 template<>
245 struct hash<helics::GlobalBrokerId> {
247  using result_type = std::size_t;
248 
249  result_type operator()(argument_type const& key) const noexcept
250  {
251  return std::hash<helics::GlobalBrokerId::BaseType>{}(key.baseValue());
252  }
253 };
254 
257 template<>
258 struct hash<helics::route_id> {
260  using result_type = std::size_t;
261 
262  result_type operator()(argument_type const& key) const noexcept
263  {
264  return std::hash<helics::route_id::BaseType>{}(key.baseValue());
265  }
266 };
267 
271 template<>
272 struct hash<helics::GlobalHandle> {
274  using result_type = std::size_t;
275 
276  result_type operator()(argument_type const& key) const noexcept
277  {
278  return std::hash<uint64_t>{}(static_cast<uint64_t>(key));
279  }
280 };
281 
282 } // namespace std
helics::gDirectCoreId
constexpr GlobalFederateId gDirectCoreId
Definition: GlobalFederateId.hpp:128
helics::GlobalFederateId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:84
helics::operator<<
std::ostream & operator<<(std::ostream &os, const ActionMessage &command)
Definition: ActionMessage.cpp:919
std::hash< helics::GlobalBrokerId >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:247
helics::GlobalBrokerId::GlobalBrokerId
constexpr GlobalBrokerId()=default
helics::gGlobalBrokerIdShift
constexpr IdentifierBaseType gGlobalBrokerIdShift
Definition: GlobalFederateId.hpp:23
helics::GlobalHandle::GlobalHandle
constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand)
Definition: GlobalFederateId.hpp:151
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
std::hash< helics::GlobalHandle >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:276
helics::GlobalBrokerId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:35
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:72
std::hash< helics::route_id >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:260
helics::GlobalHandle::handle
InterfaceHandle handle
the interface handle component
Definition: GlobalFederateId.hpp:147
std::hash< helics::GlobalBrokerId >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:249
helics::gRootBrokerID
constexpr GlobalBrokerId gRootBrokerID
Definition: GlobalFederateId.hpp:66
helics::route_id
Definition: GlobalFederateId.hpp:184
std::hash< helics::GlobalHandle >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:274
helics::IdentifierBaseType
int32_t IdentifierBaseType
Definition: LocalFederateId.hpp:15
std::hash< helics::route_id >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:262
std::hash< helics::GlobalFederateId >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:234
helics::GlobalBrokerId
Definition: GlobalFederateId.hpp:27
helics::GlobalBrokerId::operator<
constexpr bool operator<(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:41
helics::InterfaceHandle::baseValue
constexpr BaseType baseValue() const
Definition: LocalFederateId.hpp:73
helics::getSpecialFederateId
constexpr GlobalFederateId getSpecialFederateId(GlobalBrokerId broker, GlobalBrokerId::BaseType index)
Definition: GlobalFederateId.hpp:131
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::GlobalBrokerId::operator!=
constexpr bool operator!=(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:39
helics::GlobalHandle::GlobalHandle
constexpr GlobalHandle()=default
helics::gGlobalFederateIdShift
constexpr IdentifierBaseType gGlobalFederateIdShift
Definition: GlobalFederateId.hpp:21
helics::GlobalHandle
Definition: GlobalFederateId.hpp:144
helics::GlobalBrokerId::operator==
constexpr bool operator==(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:37
helics::GlobalHandle::fed_id
GlobalFederateId fed_id
the federate id component
Definition: GlobalFederateId.hpp:146
std::hash< helics::GlobalFederateId >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:236