3#include "sound_bakery/core/database/database_object.h"
4#include "sound_bakery/pch.h"
10 std::weak_ptr<database_object> SB_API find_object(sbk_id
id);
11 bool SB_API object_id_is_child_of_parent(sbk_id childToCheck, sbk_id parent);
12 sbk_id SB_API get_parent_id_from_id(sbk_id
id);
21 template <
typename TObject>
26 using TIdentifierType = sbk_id;
27 using TObjectPtr = TObject*;
28 using TObjectRef = TObject&;
29 using TObjectWeak = std::weak_ptr<database_object>;
30 using TObjectShared = std::shared_ptr<TObject>;
31 using TPtrType = std::weak_ptr<database_object>;
33 static_assert(!std::is_pointer<TObject>::value);
56 : m_objectID(
object ? static_cast<TIdentifierType>(*
object) : 0),
58 m_null(
object.use_count() == 0)
67 : m_objectID(
object ? static_cast<TIdentifierType>(*
object) : 0),
68 m_objectPtr(find_object(
id())),
77 database_ptr(std::nullptr_t) : m_objectID(), m_objectPtr(), m_null(true) {}
85 auto id() const noexcept -> sbk_id {
return m_objectID; }
87 auto shared() const noexcept -> TObjectShared
91 if (m_objectPtr.expired())
93 return std::shared_ptr<TObject>();
97 return std::static_pointer_cast<TObject>(m_objectPtr.lock());
101 auto weak() const noexcept -> TObjectWeak {
return m_objectPtr; }
106 auto raw() const noexcept -> TObjectPtr {
return shared().get(); }
108 auto lookup_raw() const noexcept -> TObjectPtr
118 auto has_id() const noexcept ->
bool {
return m_objectID != TIdentifierType(); }
123 auto null() const noexcept ->
bool {
return m_null || m_objectPtr.expired(); }
135 auto stale() const noexcept ->
bool {
return !m_null && m_objectPtr.expired(); }
151 m_objectPtr = find_object(
id());
152 m_null = m_objectPtr.expired();
160 auto reset(TObjectPtr
object =
nullptr) ->
void
162 m_objectID =
object ?
static_cast<TIdentifierType
>(*object) : TIdentifierType();
169 TThisType& operator=(TObjectShared
object)
171 if (
raw() !=
object.get())
193 TThisType& operator=(
const TThisType& other)
195 if (
id() != other.id())
197 m_objectID = other.id();
198 m_objectPtr = other.weak();
199 m_null = other.null();
205 TThisType& operator=(
const TThisType&& other)
207 if (
id() != other.id())
209 m_objectID = other.id();
210 m_objectPtr = other.weak();
211 m_null = other.null();
220 operator bool()
const {
return valid(); }
235 mutable TPtrType m_objectPtr = TPtrType();
245 template <
typename T1,
typename T2>
246 bool operator==(
const database_ptr<T1>& lhs,
const database_ptr<T2>& rhs)
248 return lhs.id() == rhs.id();
255 template <
typename T>
256 bool operator==(
const database_ptr<T>& lhs,
const T* rhs)
258 return lhs.raw() == rhs;
264 template <
typename T1,
typename T2>
265 bool operator<(
const database_ptr<T1>& lhs,
const database_ptr<T2>& rhs)
267 return lhs.id() < rhs.id();
274 template <
typename TObject>
296 m_ownerID = get_parent_id_from_id(other.m_objectID);
320 TThisType& operator=(
typename database_ptr<TObject>::TIdentifierType
id)
327 TThisType& operator=(
typename database_ptr<TObject>::TObjectPtr
object)
334 TThisType& operator=(
const TThisType& other)
338 if (m_ownerID == 0 && database_ptr<TObject>::m_objectID != 0)
340 m_ownerID = get_parent_id_from_id(database_ptr<TObject>::m_objectID);
347 database_ptr<TObject>::m_objectID = other.id();
348 database_ptr<TObject>::m_objectPtr = other.weak();
349 database_ptr<TObject>::m_null = other.null();
350 m_ownerID = other.m_ownerID;
353 else if (m_ownerID == other.m_ownerID || other.m_ownerID == 0)
356 if (object_id_is_child_of_parent(other.m_objectID, m_ownerID))
358 database_ptr<TObject>::m_objectID = other.id();
359 database_ptr<TObject>::m_objectPtr = other.weak();
360 database_ptr<TObject>::m_null = other.null();
369 void setID(
typename database_ptr<TObject>::TIdentifierType
id = 0)
372 if (m_ownerID == 0 && database_ptr<TObject>::m_objectID != 0)
374 m_ownerID = get_parent_id_from_id(database_ptr<TObject>::m_objectID);
379 database_ptr<TObject>::m_objectID = 0;
380 database_ptr<TObject>::m_objectPtr.reset();
381 database_ptr<TObject>::m_null =
true;
385 if (m_ownerID == 0 || object_id_is_child_of_parent(
id, m_ownerID))
387 database_ptr<TObject>::m_objectID =
id;
388 database_ptr<TObject>::m_objectPtr.reset();
389 database_ptr<TObject>::m_null =
true;
394 void reset(
typename database_ptr<TObject>::TObjectPtr
object =
nullptr)
397 if (m_ownerID == 0 && database_ptr<TObject>::m_objectID != 0)
399 m_ownerID = get_parent_id_from_id(database_ptr<TObject>::m_objectID);
403 if (
object ==
nullptr)
405 database_ptr<TObject>::m_objectID = 0;
406 database_ptr<TObject>::m_objectPtr.reset();
407 database_ptr<TObject>::m_null =
true;
412 sbk_id newObjectID =
static_cast<typename database_ptr<TObject>::TIdentifierType
>(*object);
414 if (m_ownerID == 0 || object_id_is_child_of_parent(newObjectID, m_ownerID))
416 database_ptr<TObject>::m_objectID = newObjectID;
417 database_ptr<TObject>::m_objectPtr.reset();
418 database_ptr<TObject>::m_null =
true;
424 typename database_ptr<TObject>::TIdentifierType m_ownerID = 0;
430 template <
typename T>
431 struct hash<sbk::core::database_ptr<T>>
436 template <
typename T>
437 struct hash<sbk::core::child_ptr<T>>
443#include <rttr/wrapper_mapper.h>
447 template <
typename T>
448 struct wrapper_mapper<sbk::core::child_ptr<T>>
453 inline static wrapped_type get(
const type& obj) {
return obj.
id(); }
455 inline static type create(
const wrapped_type& t) {
return type(t); }
457 template <
typename T2>
464 return convertedLazyPtr;
468 template <
typename T>
469 struct wrapper_mapper<sbk::core::database_ptr<T>>
474 inline static wrapped_type get(
const type& obj) {
return obj.
id(); }
476 inline static type create(
const wrapped_type& t) {
return type(t); }
478 template <
typename T2>
485 return convertedLazyPtr;
Syntactic type to define a pointer that must be a child of the owning object.
Definition database_ptr.h:276
child_ptr(const database_object &owner)
Construct a new Child Ptr object with an owner.
Definition database_ptr.h:311
child_ptr(sbk_id id)
Construct a new child_ptr that points to the ID.
Definition database_ptr.h:318
child_ptr()=default
Default constructor is exposed for RTTR but not for the user.
Base object type for any object that can exist in the editor/database. Holds an ID and name.
Definition database_object.h:12
Definition database_ptr.h:23
auto raw() const noexcept -> TObjectPtr
Get raw pointer of the referenced object.
Definition database_ptr.h:106
auto stale() const noexcept -> bool
Returns true if we previously referenced an object that has been destroyed.
Definition database_ptr.h:135
TObjectPtr operator->() const
Access the raw object.
Definition database_ptr.h:231
database_ptr(std::nullptr_t)
Create an empty and null LazyPtr.
Definition database_ptr.h:77
database_ptr(sbk_id id)
Creates a LazyPtr that can lookup its object pointer after construction.
Definition database_ptr.h:49
auto pending() const noexcept -> bool
Returns true if we hold an ID but haven't found the live object to point to yet.
Definition database_ptr.h:129
auto reset(TObjectPtr object=nullptr) -> void
Clear all references.
Definition database_ptr.h:160
bool operator!() const
Returns true if this LazyPtr is invalid.
Definition database_ptr.h:225
database_ptr()
Creates an empty and null LazyPtr.
Definition database_ptr.h:39
database_ptr(const TObjectShared &object)
Create a valid LazyPtr.
Definition database_ptr.h:55
TThisType & operator=(TObjectPtr object)
Assign this LazyPtr to a new object, potentially destroying the current object if we're acting as a U...
Definition database_ptr.h:184
database_ptr(const TObjectPtr &object)
Create a valid LazyPtr.
Definition database_ptr.h:66
auto null() const noexcept -> bool
Returns true if the object pointer is not set.
Definition database_ptr.h:123
auto valid() const noexcept -> bool
Returns true if we hold an ID and a valid pointer to the object.
Definition database_ptr.h:141
auto has_id() const noexcept -> bool
Returns true if we hold a valid ID and can search for an object at runtime.
Definition database_ptr.h:118
auto lookup() const noexcept -> bool
Find the live object referenced by the ID and store it.
Definition database_ptr.h:147
auto id() const noexcept -> sbk_id
Get ID of the referenced object.
Definition database_ptr.h:85
Base object that all sound Bakery objects should inherit from.
Definition object.h:23