MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Facets.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <typeindex>
7
8namespace MayaFlux {
9
10namespace detail {
11 struct FacetToken {
12 };
13}
14
15/**
16 * @class Facet
17 * @brief Typed key for an arbitrary attribute stored in a Facets bag.
18 *
19 * The type is fixed by the key. A key carries an initial value, so Facet(0.5F)
20 * deduces Facet<float> and Facet<double>() value-initialises one. Copies share
21 * the identity, so a copied key addresses the same attribute.
22 *
23 * @tparam T The attribute type. Must be an object type that is not const.
24 */
25template <typename T>
26class Facet {
27 static_assert(std::is_object_v<T> && !std::is_const_v<T> && !std::is_volatile_v<T>,
28 "Facet<T> requires a non-const, non-volatile object type");
29
30public:
31 explicit Facet(T initial)
32 : m_token(std::make_shared<detail::FacetToken>())
33 , m_initial(std::move(initial))
34 {
35 }
36
38 requires std::default_initializable<T>
39 : m_token(std::make_shared<detail::FacetToken>())
40 , m_initial()
41 {
42 }
43
44 /** @brief Identity shared by every copy of this key. */
45 [[nodiscard]] const void* id() const noexcept { return m_token.get(); }
46
47 /** @brief Keeps the identity alive while a Facets bag holds a value for it. */
48 [[nodiscard]] const std::shared_ptr<const void>& token() const noexcept { return m_token; }
49
50 /** @brief Value stored by set(key) with no explicit value. */
51 [[nodiscard]] const T& initial() const noexcept { return m_initial; }
52
53private:
54 std::shared_ptr<const void> m_token;
56};
57
58namespace detail {
59 template <auto Member>
60 struct MemberOf;
61
62 template <typename Owner, typename T, T Owner::* Member>
63 struct MemberOf<Member> {
64 using value_type = T;
65 };
66
67 template <auto Member>
68 const void* member_identity() noexcept
69 {
70 static constexpr char tag = 0;
71 return &tag;
72 }
73}
74
75/**
76 * @class FacetMember
77 * @brief Key for one data member of a struct, identified by its member pointer.
78 *
79 * Facets::from builds one of these for every member of a struct described with
80 * MF_DESCRIBE. Every instance of the struct produces the same keys.
81 *
82 * @tparam Member Pointer to a non-static data member.
83 */
84template <auto Member>
87
88 static constexpr auto member = Member;
89
90 /** @brief Identity shared by every instance of the owning struct. */
91 [[nodiscard]] static const void* id() noexcept { return detail::member_identity<Member>(); }
92};
93
94/**
95 * @class Facets
96 * @brief Arbitrary typed attributes keyed by Facet objects.
97 *
98 * Values are held as shared immutable entries, so copying a Facets copies
99 * pointers and never the values. Writing replaces an entry; get() returns a
100 * const view that stays valid until that entry is replaced or erased.
101 * Any struct may also be stored whole by its type, separately from Facet keys.
102 */
103class Facets {
104public:
105 /** @brief Stores a struct under its type identity. */
106 template <typename T>
107 void set(T value)
108 {
109 m_struct_entries.insert_or_assign(std::type_index(typeid(T)),
110 std::make_shared<Entry<T>>(std::shared_ptr<const void> {}, std::move(value)));
111 }
112
113 /** @brief Stores @p value under @p key, replacing any previous value. */
114 template <typename T>
115 void set(const Facet<T>& key, std::type_identity_t<T> value)
116 {
117 m_entries.insert_or_assign(key.id(), std::make_shared<Entry<T>>(key.token(), std::move(value)));
118 }
119
120 /** @brief Stores the key's initial value under @p key. */
121 template <typename T>
122 void set(const Facet<T>& key)
123 {
124 set(key, key.initial());
125 }
126
127 /** @brief Stores @p value under the member key @p key, replacing any previous value. */
128 template <auto Member>
129 void set(FacetMember<Member>, std::type_identity_t<typename FacetMember<Member>::value_type> value)
130 {
131 using T = typename FacetMember<Member>::value_type;
132 m_entries.insert_or_assign(FacetMember<Member>::id(), std::make_shared<Entry<T>>(std::shared_ptr<const void> {}, std::move(value)));
133 }
134
135 /**
136 * @brief Builds a bag holding every data member of @p value.
137 * @tparam S A struct with a generated description (MF_DESCRIBE).
138 * @return One entry per member, keyed by the member, with its current value.
139 */
140 template <typename S>
141 requires requires { Reflect::Generated::Description<S>::members(); }
142 [[nodiscard]] static Facets from(const S& value)
143 {
144 Facets bag;
145 std::apply(
146 [&](auto... member) {
147 (bag.set(FacetMember<std::remove_cvref_t<decltype(member)>::pointer> {},
148 value.*(std::remove_cvref_t<decltype(member)>::pointer)),
149 ...);
150 },
152 return bag;
153 }
154
155 /** @brief The value under @p key, or nullptr when absent. */
156 template <typename T>
157 [[nodiscard]] const T* get(const Facet<T>& key) const
158 {
159 const auto it = m_entries.find(key.id());
160 if (it == m_entries.end()) {
161 return nullptr;
162 }
163 return &static_cast<const Entry<T>*>(it->second.get())->value;
164 }
165
166 /** @brief The shared pointer under @p key, or an empty one when absent. */
167 template <typename U>
168 [[nodiscard]] std::shared_ptr<U> get(const Facet<std::shared_ptr<U>>& key) const
169 {
170 const auto it = m_entries.find(key.id());
171 if (it == m_entries.end()) {
172 return nullptr;
173 }
174 return static_cast<const Entry<std::shared_ptr<U>>*>(it->second.get())->value;
175 }
176
177 /** @brief Returns the struct stored under its type, or nullptr. */
178 template <typename T>
179 [[nodiscard]] const T* get() const
180 {
181 const auto it = m_struct_entries.find(std::type_index(typeid(T)));
182 if (it == m_struct_entries.end()) {
183 return nullptr;
184 }
185 return &static_cast<const Entry<T>*>(it->second.get())->value;
186 }
187
188 /** @brief The value under the member key @p key, or nullptr when absent. */
189 template <auto Member>
190 [[nodiscard]] const typename FacetMember<Member>::value_type* get(FacetMember<Member>) const
191 {
192 using T = typename FacetMember<Member>::value_type;
193 const auto it = m_entries.find(FacetMember<Member>::id());
194 if (it == m_entries.end()) {
195 return nullptr;
196 }
197 return &static_cast<const Entry<T>*>(it->second.get())->value;
198 }
199
200 /** @brief True when @p key has a value. */
201 template <typename T>
202 [[nodiscard]] bool has(const Facet<T>& key) const
203 {
204 return m_entries.find(key.id()) != m_entries.end();
205 }
206
207 /** @brief Removes the value under @p key. @return true if there was one. */
208 template <typename T>
209 bool erase(const Facet<T>& key)
210 {
211 return m_entries.erase(key.id());
212 }
213
214 [[nodiscard]] size_t size() const noexcept { return m_entries.size() + m_struct_entries.size(); }
215 [[nodiscard]] bool empty() const noexcept { return m_entries.empty() && m_struct_entries.empty(); }
216
217 /** @brief Removes all entries. */
218 void clear() noexcept
219 {
220 m_entries.clear();
221 m_struct_entries.clear();
222 }
223
224 /** @brief Adds entries from @p source, replacing values for shared keys. */
225 void merge(const Facets& source)
226 {
227 for (const auto& [id, entry] : source.m_entries) {
228 m_entries.insert_or_assign(id, entry);
229 }
230 for (const auto& [type, entry] : source.m_struct_entries) {
231 m_struct_entries.insert_or_assign(type, entry);
232 }
233 }
234
235private:
236 struct EntryBase {
237 std::shared_ptr<const void> token;
238
239 explicit EntryBase(std::shared_ptr<const void> t)
240 : token(std::move(t))
241 {
242 }
243
244 virtual ~EntryBase() = default;
245 };
246
247 template <typename T>
248 struct Entry final : EntryBase {
249 Entry(std::shared_ptr<const void> t, T v)
250 : EntryBase(std::move(t))
251 , value(std::move(v))
252 {
253 }
254
256 };
257
259 std::unordered_map<std::type_index, std::shared_ptr<const EntryBase>> m_struct_entries;
260};
261
262} // namespace MayaFlux
Facet(T initial)
Definition Facets.hpp:31
std::shared_ptr< const void > m_token
Definition Facets.hpp:54
const std::shared_ptr< const void > & token() const noexcept
Keeps the identity alive while a Facets bag holds a value for it.
Definition Facets.hpp:48
const T & initial() const noexcept
Value stored by set(key) with no explicit value.
Definition Facets.hpp:51
const void * id() const noexcept
Identity shared by every copy of this key.
Definition Facets.hpp:45
Typed key for an arbitrary attribute stored in a Facets bag.
Definition Facets.hpp:26
const FacetMember< Member >::value_type * get(FacetMember< Member >) const
The value under the member key key, or nullptr when absent.
Definition Facets.hpp:190
bool empty() const noexcept
Definition Facets.hpp:215
std::unordered_map< std::type_index, std::shared_ptr< const EntryBase > > m_struct_entries
Definition Facets.hpp:259
void set(const Facet< T > &key, std::type_identity_t< T > value)
Stores value under key, replacing any previous value.
Definition Facets.hpp:115
void merge(const Facets &source)
Adds entries from source, replacing values for shared keys.
Definition Facets.hpp:225
bool erase(const Facet< T > &key)
Removes the value under key.
Definition Facets.hpp:209
std::shared_ptr< U > get(const Facet< std::shared_ptr< U > > &key) const
The shared pointer under key, or an empty one when absent.
Definition Facets.hpp:168
void set(const Facet< T > &key)
Stores the key's initial value under key.
Definition Facets.hpp:122
const T * get(const Facet< T > &key) const
The value under key, or nullptr when absent.
Definition Facets.hpp:157
void set(T value)
Stores a struct under its type identity.
Definition Facets.hpp:107
void clear() noexcept
Removes all entries.
Definition Facets.hpp:218
const T * get() const
Returns the struct stored under its type, or nullptr.
Definition Facets.hpp:179
bool has(const Facet< T > &key) const
True when key has a value.
Definition Facets.hpp:202
Memory::FlatMap< const void *, std::shared_ptr< const EntryBase > > m_entries
Definition Facets.hpp:258
void set(FacetMember< Member >, std::type_identity_t< typename FacetMember< Member >::value_type > value)
Stores value under the member key key, replacing any previous value.
Definition Facets.hpp:129
static Facets from(const S &value)
Builds a bag holding every data member of value.
Definition Facets.hpp:142
size_t size() const noexcept
Definition Facets.hpp:214
Arbitrary typed attributes keyed by Facet objects.
Definition Facets.hpp:103
Map kept as one sorted vector, cheap to copy.
Definition FlatMap.hpp:21
const void * member_identity() noexcept
Definition Facets.hpp:68
Main namespace for the Maya Flux audio engine.
static const void * id() noexcept
Identity shared by every instance of the owning struct.
Definition Facets.hpp:91
typename detail::MemberOf< Member >::value_type value_type
Definition Facets.hpp:86
static constexpr auto member
Definition Facets.hpp:88
Key for one data member of a struct, identified by its member pointer.
Definition Facets.hpp:85
std::shared_ptr< const void > token
Definition Facets.hpp:237
virtual ~EntryBase()=default
EntryBase(std::shared_ptr< const void > t)
Definition Facets.hpp:239
Entry(std::shared_ptr< const void > t, T v)
Definition Facets.hpp:249