FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
sim_id.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2026-present FERS Contributors (see AUTHORS.md).
4//
5// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
6
7#pragma once
8
9#include <atomic>
10#include <cassert>
11#include <cstdint>
12#include <stdexcept>
13
14/**
15 * @brief 64-bit Unique Simulation ID.
16 * Structure: [16-bit Type][48-bit Counter]
17 */
18using SimId = uint64_t;
19
20/**
21 * @enum ObjectType
22 * @brief Categorizes objects for ID generation.
23 */
24enum class ObjectType : uint16_t
25{
26 Unknown = 0,
27 Platform = 1,
28 Transmitter = 2,
29 Receiver = 3,
30 Target = 4,
31 Antenna = 5,
32 Waveform = 6,
33 Timing = 7,
34 Debug = 0xFFFF
35};
36
37/**
38 * @class SimIdGenerator
39 * @brief Thread-safe Meyers singleton for generating unique object IDs.
40 */
42{
43public:
44 /**
45 * @brief Get the singleton instance of SimIdGenerator.
46 * @return Reference to the SimIdGenerator instance.
47 */
49 {
51 return instance;
52 }
53
54 /**
55 * @brief Generate a unique SimId for a given object type.
56 * @param type The ObjectType for which to generate the ID.
57 * @return A unique SimId.
58 * @throws std::overflow_error if the 48-bit counter overflows.
59 */
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 }
79
80 /**
81 * @brief Generate a debug SimId.
82 * @return A unique SimId with ObjectType::Debug.
83 */
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 }
101
102 /**
103 * @brief Extract object type from SimId
104 */
105 static ObjectType getType(const SimId id) { return static_cast<ObjectType>(id >> 48); }
106
107 /**
108 * @brief Extract counter from SimId
109 */
110 static uint64_t getCounter(const SimId id) { return id & 0x0000FFFFFFFFFFFF; }
111
112private:
113 SimIdGenerator() = default; ///< Private constructor for singleton
114 std::atomic<uint64_t> _counter{1}; ///< 48-bit counter for unique IDs
115};
Thread-safe Meyers singleton for generating unique object IDs.
Definition sim_id.h:42
SimId generateId(ObjectType type)
Generate a unique SimId for a given object type.
Definition sim_id.h:60
static SimIdGenerator & instance()
Get the singleton instance of SimIdGenerator.
Definition sim_id.h:48
static ObjectType getType(const SimId id)
Extract object type from SimId.
Definition sim_id.h:105
SimId generateDebugId()
Generate a debug SimId.
Definition sim_id.h:84
static uint64_t getCounter(const SimId id)
Extract counter from SimId.
Definition sim_id.h:110
uint64_t SimId
64-bit Unique Simulation ID.
Definition sim_id.h:18
ObjectType
Categorizes objects for ID generation.
Definition sim_id.h:25