helics  2.8.1
global_federate_id.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 "federate_id.hpp"
10 
11 namespace helics {
22 constexpr identififier_base_type global_broker_id_shift{0x7000'0000};
23 
27  public:
28  using base_type = identififier_base_type;
30  constexpr global_broker_id() = default;
31 
32  constexpr explicit global_broker_id(base_type val) noexcept: gid(val) {}
34  constexpr base_type baseValue() const { return gid; }
36  bool operator==(global_broker_id id) const noexcept { return (gid == id.gid); }
38  bool operator!=(global_broker_id id) const noexcept { return (gid != id.gid); }
40  bool operator<(global_broker_id id) const noexcept { return (gid < id.gid); }
41 
42  bool isFederate() const
43  {
44  return ((gid >= global_federate_id_shift) && (gid < global_broker_id_shift));
45  }
46  bool isBroker() const { return (gid >= global_broker_id_shift || gid == 1); }
47  bool isValid() const
48  {
49  return (gid != invalid_global_broker_id && gid != detail::invalid_interface_handle);
50  }
51  base_type localIndex() const { return gid - global_broker_id_shift; }
52 
53  private:
54  base_type gid = invalid_global_broker_id;
55  friend class global_federate_id; // for allowing comparison operators to work well
56  static constexpr base_type invalid_global_broker_id{-2'010'000'000};
57 };
58 
60 constexpr global_broker_id parent_broker_id{0};
62 constexpr global_broker_id root_broker_id{1};
63 
66 std::ostream& operator<<(std::ostream& os, global_broker_id id);
68 class global_federate_id {
69  public:
70  using base_type = identififier_base_type;
72  constexpr global_federate_id() = default;
73 
74  constexpr explicit global_federate_id(base_type val) noexcept: gid(val) {}
76  constexpr global_federate_id(global_broker_id id) noexcept: gid(id.gid) {} // NOLINT
77 
78  constexpr operator global_broker_id() const noexcept
79  {
80  return global_broker_id{gid};
81  } // NOLINT
83  constexpr base_type baseValue() const { return gid; }
85  bool operator==(global_federate_id id) const noexcept { return (gid == id.gid); }
87  bool operator!=(global_federate_id id) const noexcept { return (gid != id.gid); }
89  bool operator<(global_federate_id id) const noexcept { return (gid < id.gid); }
91  bool operator>(global_federate_id id) const noexcept { return (gid > id.gid); }
93  bool operator==(global_broker_id id) const noexcept { return (gid == id.gid); }
95  bool operator!=(global_broker_id id) const noexcept { return (gid != id.gid); }
97  bool operator<(global_broker_id id) const noexcept { return (gid < id.gid); }
99  bool operator>(global_broker_id id) const noexcept { return (gid > id.gid); }
101  bool isFederate() const
102  {
103  return ((gid >= global_federate_id_shift) && (gid < global_broker_id_shift));
104  }
106  bool isBroker() const { return (gid >= global_broker_id_shift || gid == 1); }
108  bool isValid() const
109  {
110  return (gid != invalid_global_fed_id && gid != detail::invalid_interface_handle);
111  }
114  constexpr base_type localIndex() const { return gid - global_federate_id_shift; }
115 
116  private:
117  static constexpr base_type invalid_global_fed_id{-2'010'000'000};
118  base_type gid{invalid_global_fed_id};
119 };
122 
125 std::ostream& operator<<(std::ostream& os, global_federate_id id);
126 
129  public:
132 
133  constexpr global_handle() = default;
136  fed_id(fed), handle(hand)
137  {
138  }
141  explicit operator uint64_t() const
142  {
143  auto key = static_cast<uint64_t>(fed_id.baseValue()) << 32U;
144  key += static_cast<uint64_t>(handle.baseValue()) & (0x0000'0000'FFFF'FFFF);
145  return key;
146  }
148  bool operator==(global_handle id) const noexcept
149  {
150  return ((fed_id == id.fed_id) && (handle == id.handle));
151  }
153  bool operator!=(global_handle id) const noexcept
154  {
155  return ((fed_id != id.fed_id) || (handle != id.handle));
156  }
158  bool operator<(global_handle id) const noexcept
159  {
160  return (fed_id < id.fed_id) ? true : ((fed_id != id.fed_id) ? false : (handle < id.handle));
161  }
163  bool isValid() const { return fed_id.isValid() && handle.isValid(); }
164 };
165 
168 std::ostream& operator<<(std::ostream& os, global_handle id);
169 
171 class route_id {
172  public:
173  using base_type = identififier_base_type;
175  constexpr route_id() = default;
177  constexpr explicit route_id(base_type val) noexcept: rid(val) {}
179  constexpr base_type baseValue() const { return rid; }
181  bool operator==(route_id id) const noexcept { return (rid == id.rid); }
183  bool operator!=(route_id id) const noexcept { return (rid != id.rid); }
185  bool operator<(route_id id) const noexcept { return (rid < id.rid); }
187  bool isValid() const { return (rid != invalid_route_id); }
188 
189  private:
190  static constexpr base_type invalid_route_id{-1'295'148'000};
191  base_type rid{invalid_route_id};
192 };
193 
194 constexpr route_id parent_route_id{0};
195 constexpr route_id control_route{-1};
196 
197 constexpr route_id generateRouteId(int32_t route_type_code, int32_t index)
198 {
199  return route_id(route_type_code * 256 * 256 * 256 + index);
200 }
201 
202 constexpr int32_t getRouteTypeCode(route_id rid)
203 {
204  return (rid.baseValue() >> 24);
205 }
206 
207 constexpr int32_t normal_route_code{0};
208 constexpr int32_t json_route_code{10};
211 std::ostream& operator<<(std::ostream& os, route_id id);
212 
213 } // namespace helics
214 
215 namespace std {
218 template<>
219 struct hash<helics::global_federate_id> {
221  using result_type = std::size_t;
222 
223  result_type operator()(argument_type const& key) const noexcept
224  {
225  return std::hash<helics::global_federate_id::base_type>{}(key.baseValue());
226  }
227 };
228 
231 template<>
232 struct hash<helics::global_broker_id> {
234  using result_type = std::size_t;
235 
236  result_type operator()(argument_type const& key) const noexcept
237  {
238  return std::hash<helics::global_broker_id::base_type>{}(key.baseValue());
239  }
240 };
241 
244 template<>
245 struct hash<helics::route_id> {
247  using result_type = std::size_t;
248 
249  result_type operator()(argument_type const& key) const noexcept
250  {
251  return std::hash<helics::route_id::base_type>{}(key.baseValue());
252  }
253 };
254 
258 template<>
259 struct hash<helics::global_handle> {
261  using result_type = std::size_t;
262 
263  result_type operator()(argument_type const& key) const noexcept
264  {
265  return std::hash<uint64_t>{}(static_cast<uint64_t>(key));
266  }
267 };
268 
269 } // namespace std
helics::global_broker_id::operator==
bool operator==(global_broker_id id) const noexcept
Definition: global_federate_id.hpp:36
helics::operator<<
std::ostream & operator<<(std::ostream &os, const ActionMessage &command)
Definition: ActionMessage.cpp:933
helics::interface_handle::baseValue
constexpr base_type baseValue() const
Definition: federate_id.hpp:73
std::hash< helics::global_broker_id >::result_type
std::size_t result_type
typedef for output result
Definition: global_federate_id.hpp:234
helics::global_broker_id_shift
constexpr identififier_base_type global_broker_id_shift
Definition: global_federate_id.hpp:22
helics::global_broker_id
Definition: global_federate_id.hpp:26
helics::global_federate_id
Definition: global_federate_id.hpp:68
std::hash< helics::route_id >::result_type
std::size_t result_type
typedef for output result
Definition: global_federate_id.hpp:247
helics::global_broker_id::baseValue
constexpr base_type baseValue() const
Definition: global_federate_id.hpp:34
helics::global_handle::fed_id
global_federate_id fed_id
the federate id component
Definition: global_federate_id.hpp:130
helics::global_handle::handle
interface_handle handle
the interface handle component
Definition: global_federate_id.hpp:131
helics::global_broker_id::global_broker_id
constexpr global_broker_id()=default
helics::route_id
Definition: global_federate_id.hpp:171
std::hash< helics::global_federate_id >::result_type
std::size_t result_type
typedef for output result
Definition: global_federate_id.hpp:221
helics::identififier_base_type
int32_t identififier_base_type
Definition: federate_id.hpp:15
helics::global_handle
Definition: global_federate_id.hpp:128
helics::global_federate_id::baseValue
constexpr base_type baseValue() const
Definition: global_federate_id.hpp:83
helics::direct_core_id
constexpr global_federate_id direct_core_id
Definition: global_federate_id.hpp:121
std::hash< helics::route_id >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: global_federate_id.hpp:249
fed
@ fed
special logging command for message coming from a fed
Definition: loggingHelper.hpp:32
helics::interface_handle
Definition: federate_id.hpp:65
helics::global_broker_id::operator!=
bool operator!=(global_broker_id id) const noexcept
Definition: global_federate_id.hpp:38
std::hash< helics::global_broker_id >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: global_federate_id.hpp:236
helics::global_broker_id::operator<
bool operator<(global_broker_id id) const noexcept
Definition: global_federate_id.hpp:40
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::global_handle::global_handle
constexpr global_handle(global_federate_id fed, interface_handle hand)
Definition: global_federate_id.hpp:135
std::hash< helics::global_federate_id >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: global_federate_id.hpp:223
std::hash< helics::global_handle >::result_type
std::size_t result_type
typedef for output result
Definition: global_federate_id.hpp:261
helics::global_handle::global_handle
constexpr global_handle()=default
std::hash< helics::global_handle >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: global_federate_id.hpp:263
helics::global_federate_id_shift
constexpr identififier_base_type global_federate_id_shift
Definition: global_federate_id.hpp:20