EchoMap
2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Toggle main menu visibility
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
20
namespace
echomap
21
{
22
35
template
<
typename
Derived>
class
Object
36
{
37
public
:
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
{
54
return
id
!=
IDAllocator<Derived>::invalid_id
;
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
101
protected
:
105
struct
CopyTag
106
{};
107
111
Object
() :
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 :
133
id
(
std::exchange
(
134
other.id,
135
IDAllocator<Derived>::invalid_id
136
)),
137
copy_count
(
138
std::exchange
(
139
other.copy_count,
140
0
141
)
142
),
143
name
(std::move(other.name))
144
{
145
}
146
155
Object
(
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(),
164
copy_count
165
))
166
{
167
++old.
copy_count
;
168
}
169
179
Object
(
180
[[maybe_unused]]
CopyTag
tag,
181
const
Object& old,
182
const
std::string_view
new_name
183
) :
184
id
(
IDAllocator
<Derived>::allocate()),
185
copy_count
(old.
copy_count
),
186
name
(new_name)
187
{
188
}
189
211
void
move_identity_from
(
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
220
id
=
std::exchange
(other.id,
IDAllocator<Derived>::invalid_id
);
221
copy_count
=
std::exchange
(other.copy_count, 0);
222
name
= std::move(other.name);
223
}
224
225
private
:
235
static
constexpr
std::string_view
class_name
=
"Object"
;
236
237
id_type
id
;
238
mutable
std::size_t
copy_count
= 0;
239
std::string
name
;
240
};
241
242
}
// namespace echomap
243
244
#endif
// ECHOMAP_OBJECT_HPP
IDAllocator.hpp
IDAllocator specification.
std::string
std::string_view
echomap::IDAllocator
Provides a simple thread-safe runtime ID generator for Object managers.
Definition
IDAllocator.hpp:28
echomap::IDAllocator::invalid_id
static constexpr id_type invalid_id
Sentinel ID used by invalidated (moved-from) objects.
Definition
IDAllocator.hpp:35
echomap::Object
A participant in a runtime object model with a numerically unique identifier and display name.
Definition
Object.hpp:36
echomap::Object::move_identity_from
void move_identity_from(Object &&other) noexcept
Helper for derived classes to provide move-assignment operations.
Definition
Object.hpp:211
echomap::Object::~Object
~Object()=default
Non-virtual base destructor.
echomap::Object::Object
Object(CopyTag tag, const Object &old, const std::string_view new_name)
Copy an existing Object.
Definition
Object.hpp:179
echomap::Object::id
id_type id
Primary numerical ID.
Definition
Object.hpp:237
echomap::Object::Object
Object(CopyTag tag, const Object &old)
Copy an existing Object.
Definition
Object.hpp:155
echomap::Object::operator==
bool operator==(const Object &other) const noexcept
Determine shallow equality between two Object instances.
Definition
Object.hpp:94
echomap::Object::name
std::string name
Display name for the object.
Definition
Object.hpp:239
echomap::Object::Object
Object(const std::string_view object_name)
Create a new Object with an auto-allocated ID and custom display name.
Definition
Object.hpp:122
echomap::Object::class_name
static constexpr std::string_view class_name
Display name for objects of the type determined by the templated class.
Definition
Object.hpp:235
echomap::Object::copy_count
std::size_t copy_count
Number of times the object has been copied; used only for display names.
Definition
Object.hpp:238
echomap::Object::Object
Object()
Create a new Object with an auto-allocated ID and default display name.
Definition
Object.hpp:111
std::exchange
T exchange(T... args)
echomap
The main EchoMap outermost namespace for all non-exported symbols.
Definition
ActionController.hpp:20
std
STL namespace.
std::size_t
string
string_view
echomap::Object::CopyTag
Tag despatch discriminator for copying the base object, preserving uniqueness of IDs.
Definition
Object.hpp:106
utility
src
objects
Object.hpp
Generated by
1.17.0