FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
SimIdGenerator Class Reference

Thread-safe Meyers singleton for generating unique object IDs. More...

#include "sim_id.h"

Public Member Functions

SimId generateId (ObjectType type)
 Generate a unique SimId for a given object type.
 
SimId generateDebugId ()
 Generate a debug SimId.
 

Static Public Member Functions

static SimIdGeneratorinstance ()
 Get the singleton instance of SimIdGenerator.
 
static ObjectType getType (const SimId id)
 Extract object type from SimId.
 
static uint64_t getCounter (const SimId id)
 Extract counter from SimId.
 

Detailed Description

Thread-safe Meyers singleton for generating unique object IDs.

Definition at line 41 of file sim_id.h.

Member Function Documentation

◆ generateDebugId()

SimId SimIdGenerator::generateDebugId ( )

Generate a debug SimId.

Returns
A unique SimId with ObjectType::Debug.

Definition at line 84 of file sim_id.h.

85 {
86 // Increment 48-bit counter
87 uint64_t count = _counter.fetch_add(1, std::memory_order_relaxed);
88 constexpr uint64_t MAX_COUNTER_VALUE = 0x0000FFFFFFFFFFFF;
89 if (count >= MAX_COUNTER_VALUE)
90 {
91 throw std::overflow_error("FERS object ID counter has overflowed the 48-bit space.");
92 }
93
94 // The mask is technically redundant due to the check above
95 count &= MAX_COUNTER_VALUE;
96
97 // Shift debug type into upper 16 bits
98 constexpr uint64_t type_bits = static_cast<uint64_t>(ObjectType::Debug) << 48;
99 return type_bits | count;
100 }

References Debug.

◆ generateId()

SimId SimIdGenerator::generateId ( ObjectType  type)

Generate a unique SimId for a given object type.

Parameters
typeThe ObjectType for which to generate the ID.
Returns
A unique SimId.
Exceptions
std::overflow_errorif the 48-bit counter overflows.

Definition at line 60 of file sim_id.h.

61 {
62 assert(type != ObjectType::Debug && "generateId called with reserved ObjectType::Debug");
63
64 // Increment 48-bit counter
65 uint64_t count = _counter.fetch_add(1, std::memory_order_relaxed);
66 constexpr uint64_t MAX_COUNTER_VALUE = 0x0000FFFFFFFFFFFF;
67 if (count >= MAX_COUNTER_VALUE)
68 {
69 throw std::overflow_error("FERS object ID counter has overflowed the 48-bit space.");
70 }
71
72 // The mask is technically redundant due to the check above
73 count &= MAX_COUNTER_VALUE;
74
75 // Shift type into upper 16 bits
76 const uint64_t type_bits = static_cast<uint64_t>(type) << 48;
77 return type_bits | count;
78 }

References Debug.

Referenced by serial::xml_parser_utils::assign_id_from_attribute().

+ Here is the caller graph for this function:

◆ getCounter()

static uint64_t SimIdGenerator::getCounter ( const SimId  id)
static

Extract counter from SimId.

Definition at line 110 of file sim_id.h.

110{ return id & 0x0000FFFFFFFFFFFF; }

◆ getType()

static ObjectType SimIdGenerator::getType ( const SimId  id)
static

Extract object type from SimId.

Definition at line 105 of file sim_id.h.

105{ return static_cast<ObjectType>(id >> 48); }
ObjectType
Categorizes objects for ID generation.
Definition sim_id.h:25

◆ instance()

static SimIdGenerator & SimIdGenerator::instance ( )
static

Get the singleton instance of SimIdGenerator.

Returns
Reference to the SimIdGenerator instance.

Definition at line 48 of file sim_id.h.

49 {
51 return instance;
52 }
Thread-safe Meyers singleton for generating unique object IDs.
Definition sim_id.h:42
static SimIdGenerator & instance()
Get the singleton instance of SimIdGenerator.
Definition sim_id.h:48

References instance().

Referenced by serial::xml_parser_utils::assign_id_from_attribute(), and instance().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: