helics  3.0.1
GlobalFederateId.hpp
1 /*
2 Copyright (c) 2017-2021,
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 {
22 constexpr IdentifierBaseType gGlobalBrokerIdShift{0x7000'0000};
23 
27  public:
28  using BaseType = IdentifierBaseType;
30  constexpr GlobalBrokerId() = default;
31 
32  constexpr explicit GlobalBrokerId(BaseType val) noexcept: gid(val) {}
34  constexpr BaseType baseValue() const { return gid; }
36  bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
38  bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
40  bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
41 
42  bool isFederate() const
43  {
44  return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
45  }
46  bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
47  bool isValid() const
48  {
49  return (gid != invalid_global_broker_id && gid != detail::gInvalidInterfaceHandle);
50  }
51  BaseType localIndex() const { return gid - gGlobalBrokerIdShift; }
52 
53  private:
54  BaseType gid = invalid_global_broker_id;
55  friend class GlobalFederateId; // for allowing comparison operators to work well
56  static constexpr BaseType invalid_global_broker_id{-2'010'000'000};
57 };
58 
60 constexpr GlobalBrokerId parent_broker_id{0};
62 constexpr GlobalBrokerId root_broker_id{1};
63 
66 std::ostream& operator<<(std::ostream& os, GlobalBrokerId id);
68 class GlobalFederateId {
69  public:
70  using BaseType = IdentifierBaseType;
72  constexpr GlobalFederateId() = default;
73 
74  constexpr explicit GlobalFederateId(BaseType val) noexcept: gid(val) {}
76  constexpr GlobalFederateId(GlobalBrokerId id) noexcept: gid(id.gid) {} // NOLINT
77 
78  constexpr operator GlobalBrokerId() const noexcept { return GlobalBrokerId{gid}; } // NOLINT
80  constexpr BaseType baseValue() const { return gid; }
82  bool operator==(GlobalFederateId id) const noexcept { return (gid == id.gid); }
84  bool operator!=(GlobalFederateId id) const noexcept { return (gid != id.gid); }
86  bool operator<(GlobalFederateId id) const noexcept { return (gid < id.gid); }
88  bool operator>(GlobalFederateId id) const noexcept { return (gid > id.gid); }
90  bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
92  bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
94  bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
96  bool operator>(GlobalBrokerId id) const noexcept { return (gid > id.gid); }
98  bool isFederate() const
99  {
100  return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
101  }
103  bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
105  bool isValid() const
106  {
107  return (gid != invalid_global_fed_id && gid != detail::gInvalidInterfaceHandle);
108  }
111  constexpr BaseType localIndex() const { return gid - gGlobalFederateIdShift; }
112 
114  BaseType* getBaseTypePointer() { return &gid; }
115 
116  private:
117  static constexpr BaseType invalid_global_fed_id{-2'010'000'000};
118  BaseType gid{invalid_global_fed_id};
119 };
121 constexpr GlobalFederateId direct_core_id{-235262};
122 
125 std::ostream& operator<<(std::ostream& os, GlobalFederateId id);
126 
129  public:
132 
133  constexpr GlobalHandle() = default;
135  constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand): fed_id(fed), handle(hand) {}
138  explicit operator uint64_t() const
139  {
140  auto key = static_cast<uint64_t>(fed_id.baseValue()) << 32U;
141  key += static_cast<uint64_t>(handle.baseValue()) & (0x0000'0000'FFFF'FFFF);
142  return key;
143  }
145  bool operator==(GlobalHandle id) const noexcept
146  {
147  return ((fed_id == id.fed_id) && (handle == id.handle));
148  }
150  bool operator!=(GlobalHandle id) const noexcept
151  {
152  return ((fed_id != id.fed_id) || (handle != id.handle));
153  }
155  bool operator<(GlobalHandle id) const noexcept
156  {
157  return (fed_id < id.fed_id) ? true : ((fed_id != id.fed_id) ? false : (handle < id.handle));
158  }
160  bool isValid() const { return fed_id.isValid() && handle.isValid(); }
161 };
162 
165 std::ostream& operator<<(std::ostream& os, GlobalHandle id);
166 
168 class route_id {
169  public:
170  using BaseType = IdentifierBaseType;
172  constexpr route_id() = default;
174  constexpr explicit route_id(BaseType val) noexcept: rid(val) {}
176  constexpr BaseType baseValue() const { return rid; }
178  bool operator==(route_id id) const noexcept { return (rid == id.rid); }
180  bool operator!=(route_id id) const noexcept { return (rid != id.rid); }
182  bool operator<(route_id id) const noexcept { return (rid < id.rid); }
184  bool isValid() const { return (rid != invalid_route_id); }
185 
186  private:
187  static constexpr BaseType invalid_route_id{-1'295'148'000};
188  BaseType rid{invalid_route_id};
189 };
190 
191 constexpr route_id parent_route_id{0};
192 constexpr route_id control_route{-1};
193 
194 constexpr route_id generateRouteId(int32_t route_type_code, int32_t index)
195 {
196  return route_id(route_type_code * 256 * 256 * 256 + index);
197 }
198 
199 constexpr int32_t getRouteTypeCode(route_id rid)
200 {
201  return (rid.baseValue() >> 24);
202 }
203 
204 constexpr int32_t normal_route_code{0};
205 constexpr int32_t json_route_code{10};
208 std::ostream& operator<<(std::ostream& os, route_id id);
209 
210 } // namespace helics
211 
212 namespace std {
215 template<>
216 struct hash<helics::GlobalFederateId> {
218  using result_type = std::size_t;
219 
220  result_type operator()(argument_type const& key) const noexcept
221  {
222  return std::hash<helics::GlobalFederateId::BaseType>{}(key.baseValue());
223  }
224 };
225 
228 template<>
229 struct hash<helics::GlobalBrokerId> {
231  using result_type = std::size_t;
232 
233  result_type operator()(argument_type const& key) const noexcept
234  {
235  return std::hash<helics::GlobalBrokerId::BaseType>{}(key.baseValue());
236  }
237 };
238 
241 template<>
242 struct hash<helics::route_id> {
244  using result_type = std::size_t;
245 
246  result_type operator()(argument_type const& key) const noexcept
247  {
248  return std::hash<helics::route_id::BaseType>{}(key.baseValue());
249  }
250 };
251 
255 template<>
256 struct hash<helics::GlobalHandle> {
258  using result_type = std::size_t;
259 
260  result_type operator()(argument_type const& key) const noexcept
261  {
262  return std::hash<uint64_t>{}(static_cast<uint64_t>(key));
263  }
264 };
265 
266 } // namespace std
helics::GlobalFederateId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:80
helics::operator<<
std::ostream & operator<<(std::ostream &os, const ActionMessage &command)
Definition: ActionMessage.cpp:913
std::hash< helics::GlobalBrokerId >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:231
helics::GlobalBrokerId::GlobalBrokerId
constexpr GlobalBrokerId()=default
helics::gGlobalBrokerIdShift
constexpr IdentifierBaseType gGlobalBrokerIdShift
Definition: GlobalFederateId.hpp:22
helics::direct_core_id
constexpr GlobalFederateId direct_core_id
Definition: GlobalFederateId.hpp:121
helics::GlobalHandle::GlobalHandle
constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand)
Definition: GlobalFederateId.hpp:135
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
std::hash< helics::GlobalHandle >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:260
helics::GlobalBrokerId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:34
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:68
std::hash< helics::route_id >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:244
helics::GlobalHandle::handle
InterfaceHandle handle
the interface handle component
Definition: GlobalFederateId.hpp:131
std::hash< helics::GlobalBrokerId >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:233
helics::route_id
Definition: GlobalFederateId.hpp:168
std::hash< helics::GlobalHandle >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:258
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:246
std::hash< helics::GlobalFederateId >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:218
helics::GlobalBrokerId::operator!=
bool operator!=(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:38
helics::GlobalBrokerId
Definition: GlobalFederateId.hpp:26
helics::GlobalBrokerId::operator<
bool operator<(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:40
helics::GlobalBrokerId::operator==
bool operator==(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:36
helics::InterfaceHandle::baseValue
constexpr BaseType baseValue() const
Definition: LocalFederateId.hpp:73
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::GlobalHandle::GlobalHandle
constexpr GlobalHandle()=default
helics::gGlobalFederateIdShift
constexpr IdentifierBaseType gGlobalFederateIdShift
Definition: GlobalFederateId.hpp:20
helics::GlobalHandle
Definition: GlobalFederateId.hpp:128
helics::GlobalHandle::fed_id
GlobalFederateId fed_id
the federate id component
Definition: GlobalFederateId.hpp:130
std::hash< helics::GlobalFederateId >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:220