qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
ActorId.h
Go to the documentation of this file.
1
28
29#ifndef QB_ACTORID_H
30#define QB_ACTORID_H
31#include <algorithm>
32#include <bitset>
33#include <cstdint>
34#include <limits>
35#include <unordered_set>
36#include <vector>
37// include from qb
38#include <qb/io.h>
41#include <qb/utility/prefix.h>
42
43namespace qb {
44
51using CoreId = uint16_t;
52
59using ServiceId = uint16_t;
60
67using TypeId = uint16_t;
68
76
80constexpr size_t MaxCores = 256;
81
90private:
91 std::bitset<MaxCores> _bits;
92
93public:
98 CoreIdBitSet() = default;
99
105 explicit CoreIdBitSet(const qb::unordered_set<CoreId> &coreIds) {
106 for (const auto id : coreIds) {
107 _bits.set(id);
108 }
109 }
110
116 CoreIdBitSet(std::initializer_list<CoreId> ids) {
117 for (const auto id : ids) {
118 _bits.set(id);
119 }
120 }
121
126 [[nodiscard]] const std::bitset<MaxCores> &
127 bits() const noexcept {
128 return _bits;
129 }
130
136 [[nodiscard]] bool
137 contains(CoreId id) const noexcept {
138 return id < MaxCores && _bits.test(id);
139 }
140
145 void
146 insert(CoreId id) noexcept {
147 if (id < MaxCores) {
148 _bits.set(id);
149 }
150 }
151
156 void
157 emplace(CoreId id) noexcept {
158 insert(id);
159 }
160
165 void
166 remove(CoreId id) noexcept {
167 if (id < MaxCores) {
168 _bits.reset(id);
169 }
170 }
171
175 void
176 clear() noexcept {
177 _bits.reset();
178 }
179
184 [[nodiscard]] bool
185 empty() const noexcept {
186 return _bits.none();
187 }
188
193 [[nodiscard]] size_t
194 size() const noexcept {
195 return _bits.count();
196 }
197
202 [[nodiscard]] std::vector<CoreId>
203 to_vector() const {
204 std::vector<CoreId> result;
205 result.reserve(_bits.count());
206
207 for (size_t i = 0; i < MaxCores; ++i) {
208 if (_bits.test(i)) {
209 result.push_back(static_cast<CoreId>(i));
210 }
211 }
212
213 return result;
214 }
215
220 [[nodiscard]] qb::unordered_set<CoreId>
223
224 for (size_t i = 0; i < MaxCores; ++i) {
225 if (_bits.test(i)) {
226 result.insert(static_cast<CoreId>(i));
227 }
228 }
229
230 return result;
231 }
232
237 [[nodiscard]] qb::unordered_set<CoreId>
238 raw() const {
239 return to_unordered_set();
240 }
241
242 // Iteration support
252 class iterator {
253 private:
254 const CoreIdBitSet &_set;
255 size_t _pos;
256
257 // Advance to the next set bit
258 void
259 advance() {
260 while (_pos < MaxCores && !_set._bits.test(_pos)) {
261 ++_pos;
262 }
263 }
264
265 public:
266 using iterator_category = std::forward_iterator_tag;
267 using value_type = CoreId;
268 using difference_type = std::ptrdiff_t;
269 using pointer = const CoreId *;
270 using reference = const CoreId &;
271
277 iterator(const CoreIdBitSet &set, size_t pos)
278 : _set(set)
279 , _pos(pos) {
280 advance();
281 }
282
287 CoreId
288 operator*() const {
289 return static_cast<CoreId>(_pos);
290 }
291
296 iterator &
298 ++_pos;
299 advance();
300 return *this;
301 }
302
309 iterator tmp = *this;
310 ++(*this);
311 return tmp;
312 }
313
319 bool
320 operator==(const iterator &other) const {
321 return &_set == &other._set && _pos == other._pos;
322 }
323
329 bool
330 operator!=(const iterator &other) const {
331 return !(*this == other);
332 }
333 };
334
339 [[nodiscard]] iterator
340 begin() const {
341 return iterator(*this, 0);
342 }
343
348 [[nodiscard]] iterator
349 end() const {
350 return iterator(*this, MaxCores);
351 }
352};
353
354// Define CoreIdSet as our new efficient implementation
364
374class ActorId {
375 template <typename T>
376 friend struct std::hash;
377
378 friend class CoreInitializer;
379 friend class SharedCoreCommunication;
380 friend class VirtualCore;
381 friend class Actor;
382 friend class Service;
383
384private:
385 ServiceId _service_id;
386 CoreId _core_id;
387
388protected:
389 ActorId(ServiceId id, CoreId index) noexcept;
390
391public:
392 static constexpr uint32_t NotFound = 0;
393 static constexpr ServiceId BroadcastSid = (std::numeric_limits<ServiceId>::max)();
394
398 ActorId() noexcept;
403 ActorId(uint32_t id) noexcept;
404
409 [[nodiscard]] operator uint32_t() const noexcept;
410
415 [[nodiscard]] ServiceId sid() const noexcept;
416
421 [[nodiscard]] CoreId index() const noexcept;
422
427 [[nodiscard]] bool is_broadcast() const noexcept;
428
433 [[nodiscard]] bool is_valid() const noexcept;
434};
435
445class BroadcastId : public ActorId {
446public:
447 BroadcastId() = delete;
453 explicit BroadcastId(uint32_t const core_id) noexcept
454 : ActorId(BroadcastSid, static_cast<CoreId>(core_id)) {}
455};
456
463using ActorIdList = std::vector<ActorId>;
464
471using ActorIdSet = std::unordered_set<ActorId>;
472
480
488
496
504
512
520
528#ifdef QB_LOGGER
529qb::io::log::stream &operator<<(qb::io::log::stream &os, qb::ActorId const &id);
530#endif
531} // namespace qb
532
533namespace std {
543template <>
544struct hash<qb::ActorId> {
550 std::size_t
551 operator()(qb::ActorId const &val) const noexcept {
552 return static_cast<uint32_t>(val);
553 }
554};
555} // namespace std
556
557#endif // QB_ACTORID_H
Platform, compiler, and C++ feature detection macros for the QB Framework.
Unique identifier for actors.
Definition ActorId.h:374
CoreId index() const noexcept
Get the core identifier component of this ActorId.
ServiceId sid() const noexcept
Get the service identifier component of this ActorId.
ActorId() noexcept
bool is_valid() const noexcept
Check if this ActorId is valid (not NotFound)
bool is_broadcast() const noexcept
Check if this ActorId represents a broadcast identifier.
Specialized ActorId for broadcasting messages to all actors on a core.
Definition ActorId.h:445
Iterator for traversing set bits in a CoreIdBitSet.
Definition ActorId.h:252
iterator operator++(int)
Post-increment operator.
Definition ActorId.h:308
iterator(const CoreIdBitSet &set, size_t pos)
Constructs an iterator for a CoreIdBitSet.
Definition ActorId.h:277
CoreId operator*() const
Dereference operator.
Definition ActorId.h:288
bool operator!=(const iterator &other) const
Inequality comparison operator.
Definition ActorId.h:330
bool operator==(const iterator &other) const
Equality comparison operator.
Definition ActorId.h:320
iterator & operator++()
Pre-increment operator.
Definition ActorId.h:297
Efficient representation of a set of core IDs using a bitset.
Definition ActorId.h:89
qb::unordered_set< CoreId > to_unordered_set() const
Get an unordered_set of the core IDs.
Definition ActorId.h:221
const std::bitset< MaxCores > & bits() const noexcept
Get the raw bitset.
Definition ActorId.h:127
void remove(CoreId id) noexcept
Remove a core ID from the set.
Definition ActorId.h:166
iterator begin() const
Get an iterator to the beginning of the set.
Definition ActorId.h:340
qb::unordered_set< CoreId > raw() const
Get a reference to the raw set for internal use.
Definition ActorId.h:238
size_t size() const noexcept
Get the number of core IDs in the set.
Definition ActorId.h:194
void emplace(CoreId id) noexcept
Add a core ID to the set (emplace version)
Definition ActorId.h:157
iterator end() const
Get an iterator to the end of the set.
Definition ActorId.h:349
void clear() noexcept
Clear all core IDs from the set.
Definition ActorId.h:176
bool empty() const noexcept
Check if the set is empty.
Definition ActorId.h:185
void insert(CoreId id) noexcept
Add a core ID to the set.
Definition ActorId.h:146
bool contains(CoreId id) const noexcept
Check if a core ID is in the set.
Definition ActorId.h:137
std::vector< CoreId > to_vector() const
Convert the set to a vector of core IDs.
Definition ActorId.h:203
constexpr size_t MaxCores
Maximum number of cores supported in a system.
Definition ActorId.h:80
CoreIdBitSet()=default
Default constructor - creates an empty set.
BroadcastId(uint32_t const core_id) noexcept
Constructor for BroadcastId.
Definition ActorId.h:453
CoreIdBitSet(std::initializer_list< CoreId > ids)
Constructor from an initializer list.
Definition ActorId.h:116
std::ostream & operator<<(std::ostream &os, qb::Actor const &actor)
Stream output operator for Actor objects.
CoreIdBitSet(const qb::unordered_set< CoreId > &coreIds)
Constructor from a set of core IDs.
Definition ActorId.h:105
std::unordered_set< K, H, E, A > unordered_set
The primary unordered set implementation.
Definition unordered_set.h:81
std::vector< ActorId > ActorIdList
List of actor identifiers.
Definition ActorId.h:463
uint16_t CoreId
Type definition for core identifiers.
Definition ActorId.h:51
uint16_t TypeId
Type definition for type identifiers.
Definition ActorId.h:67
CoreId core_id
Alias for CoreId.
Definition ActorId.h:479
uint16_t ServiceId
Type definition for service identifiers.
Definition ActorId.h:59
std::unordered_set< ActorId > ActorIdSet
Set of unique actor identifiers.
Definition ActorId.h:471
CoreIdSet core_id_set
Alias for CoreIdSet.
Definition ActorId.h:527
ActorId actor_id
Alias for ActorId.
Definition ActorId.h:495
BroadcastId broadcast_id
Alias for BroadcastId.
Definition ActorId.h:503
ServiceId service_id
Alias for ServiceId.
Definition ActorId.h:487
ActorIdSet actor_is_set
Alias for ActorIdSet.
Definition ActorId.h:519
TypeId EventId
Type definition for event identifiers.
Definition ActorId.h:75
ActorIdList actor_id_list
Alias for ActorIdList.
Definition ActorId.h:511
CoreIdBitSet CoreIdSet
Efficient set implementation for storing CoreId values.
Definition ActorId.h:363
Core I/O and logging utilities for the qb framework.
Platform-specific alignment macros, cache-line definitions, and related utilities.
std::size_t operator()(qb::ActorId const &val) const noexcept
Hash function operator for ActorId.
Definition ActorId.h:551
Optimized unordered set implementations.