EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
Object.hpp
Go to the documentation of this file.
1
9
10#ifndef ECHOMAP_OBJECT_HPP
11#define ECHOMAP_OBJECT_HPP
12
13#include <format>
14#include <string>
15#include <string_view>
16#include <utility>
17
18#include "IDAllocator.hpp"
19
20namespace echomap
21{
22
35template <typename Derived> class Object
36{
37public:
44 ~Object() = default;
45
46 [[nodiscard]] id_type get_id() const noexcept
47 {
48 assert(is_valid());
49 return id;
50 }
51
52 [[nodiscard]] bool is_valid() const noexcept
53 {
55 }
56
57 void set_name(
58 const std::string_view new_name
59 )
60 {
61 assert(is_valid());
62 this->name = std::string(new_name);
63 }
64
65 [[nodiscard]] std::string_view get_name() const noexcept
66 {
67 assert(is_valid());
68 return name;
69 }
70
71 [[nodiscard]] const char* get_imgui_name() const noexcept
72 {
73 assert(is_valid());
74 return name.c_str();
75 }
76
77 [[nodiscard]] static std::string_view get_class_name() noexcept
78 {
79 return class_name;
80 }
81
82 Object(const Object&) = delete;
83 Object& operator=(const Object&) = delete;
84 Object& operator=(Object&&) = delete;
85
94 [[nodiscard]] bool operator==(
95 const Object& other
96 ) const noexcept
97 {
98 return id == other.id;
99 }
100
101protected:
105 struct CopyTag
106 {};
107
112 id(IDAllocator<Derived>::allocate()),
113 name(std::string(class_name) + ' ' + std::to_string(id))
114 {
115 }
116
122 explicit Object(
123 const std::string_view object_name
124 ) :
125 id(IDAllocator<Derived>::allocate()),
126 name(object_name.empty() ? std::string(class_name) + ' ' + std::to_string(id) : object_name)
127 {
128 }
129
130 Object(
131 Object&& other
132 ) noexcept :
134 other.id,
136 )),
139 other.copy_count,
140 0
141 )
142 ),
143 name(std::move(other.name))
144 {
145 }
146
156 [[maybe_unused]] CopyTag tag,
157 const Object& old
158 ) :
159 id(IDAllocator<Derived>::allocate()),
160 copy_count(old.copy_count + 1),
161 name(std::format(
162 "{} ({})",
163 old.get_name(),
165 ))
166 {
167 ++old.copy_count;
168 }
169
180 [[maybe_unused]] CopyTag tag,
181 const Object& old,
182 const std::string_view new_name
183 ) :
184 id(IDAllocator<Derived>::allocate()),
186 name(new_name)
187 {
188 }
189
212 Object&& other // NOLINT(*-rvalue-reference-param-not-moved)
213 ) noexcept
214 {
215 if (this == &other)
216 return;
217
218 assert(other.is_valid());
219
221 copy_count = std::exchange(other.copy_count, 0);
222 name = std::move(other.name);
223 }
224
225private:
235 static constexpr std::string_view class_name = "Object";
236
237 id_type id;
240};
241
242} // namespace echomap
243
244#endif // ECHOMAP_OBJECT_HPP
IDAllocator specification.
Provides a simple thread-safe runtime ID generator for Object managers.
static constexpr id_type invalid_id
Sentinel ID used by invalidated (moved-from) objects.
A participant in a runtime object model with a numerically unique identifier and display name.
Definition Object.hpp:36
void move_identity_from(Object &&other) noexcept
Helper for derived classes to provide move-assignment operations.
Definition Object.hpp:211
~Object()=default
Non-virtual base destructor.
Object(CopyTag tag, const Object &old, const std::string_view new_name)
Copy an existing Object.
Definition Object.hpp:179
id_type id
Primary numerical ID.
Definition Object.hpp:237
Object(CopyTag tag, const Object &old)
Copy an existing Object.
Definition Object.hpp:155
bool operator==(const Object &other) const noexcept
Determine shallow equality between two Object instances.
Definition Object.hpp:94
std::string name
Display name for the object.
Definition Object.hpp:239
Object(const std::string_view object_name)
Create a new Object with an auto-allocated ID and custom display name.
Definition Object.hpp:122
static constexpr std::string_view class_name
Display name for objects of the type determined by the templated class.
Definition Object.hpp:235
std::size_t copy_count
Number of times the object has been copied; used only for display names.
Definition Object.hpp:238
Object()
Create a new Object with an auto-allocated ID and default display name.
Definition Object.hpp:111
T exchange(T... args)
The main EchoMap outermost namespace for all non-exported symbols.
STL namespace.
Tag despatch discriminator for copying the base object, preserving uniqueness of IDs.
Definition Object.hpp:106