3#include "sound_bakery/core/database/database_object.h"
4#include "sound_bakery/pch.h"
10 std::weak_ptr<database_object> SB_API findObject(sbk_id
id);
11 bool SB_API objectIdIsChildOfParent(sbk_id childToCheck, sbk_id parent);
12 sbk_id SB_API getParentIdFromId(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), m_objectPtr(findObject(
id())), m_null(
object == nullptr)
75 database_ptr(std::nullptr_t) : m_objectID(), m_objectPtr(), m_null(true) {}
84 sbk_id
id() const noexcept {
return m_objectID; }
86 TObjectShared shared() const noexcept
90 if (m_objectPtr.expired())
92 return std::shared_ptr<TObject>();
96 return std::static_pointer_cast<TObject>(m_objectPtr.lock());
100 TObjectWeak weak() const noexcept {
return m_objectPtr; }
106 TObjectPtr
raw() const noexcept {
return shared().get(); }
108 TObjectPtr lookupRaw() const noexcept
119 bool hasId()
const {
return m_objectID != TIdentifierType(); }
125 bool null()
const {
return m_null || m_objectPtr.expired(); }
139 bool stale()
const {
return !m_null && m_objectPtr.expired(); }
157 m_objectPtr = findObject(
id());
158 m_null = m_objectPtr.expired();
166 void reset(TObjectPtr
object =
nullptr)
168 m_objectID =
object ?
static_cast<TIdentifierType
>(*object) : TIdentifierType();
176 TThisType& operator=(TObjectShared
object)
178 if (
raw() !=
object.get())
200 TThisType& operator=(
const TThisType& other)
202 if (
id() != other.id())
204 m_objectID = other.id();
205 m_objectPtr = other.weak();
206 m_null = other.null();
212 TThisType& operator=(
const TThisType&& other)
214 if (
id() != other.id())
216 m_objectID = other.id();
217 m_objectPtr = other.weak();
218 m_null = other.null();
227 operator bool()
const {
return valid(); }
242 mutable TPtrType m_objectPtr = TPtrType();
256 template <
typename T1,
typename T2>
257 bool operator==(
const database_ptr<T1>& lhs,
const database_ptr<T2>& rhs)
259 return lhs.id() == rhs.id();
270 template <
typename T>
271 bool operator==(
const database_ptr<T>& lhs,
const T* rhs)
273 return lhs.
raw() == rhs;
286 template <
typename T1,
typename T2>
287 bool operator<(
const database_ptr<T1>& lhs,
const database_ptr<T2>& rhs)
289 return lhs.
id() < rhs.id();
296 template <
typename TObject>
318 m_ownerID = getParentIdFromId(
other.m_objectID);
342 TThisType& operator=(
typename database_ptr<TObject>::TIdentifierType
id)
349 TThisType& operator=(
typename database_ptr<TObject>::TObjectPtr
object)
356 TThisType& operator=(
const TThisType&
other)
360 if (m_ownerID == 0 && database_ptr<TObject>::m_objectID != 0)
362 m_ownerID = getParentIdFromId(database_ptr<TObject>::m_objectID);
369 database_ptr<TObject>::m_objectID =
other.id();
370 database_ptr<TObject>::m_objectPtr =
other.weak();
371 database_ptr<TObject>::m_null =
other.null();
372 m_ownerID =
other.m_ownerID;
375 else if (m_ownerID ==
other.m_ownerID ||
other.m_ownerID == 0)
378 if (objectIdIsChildOfParent(
other.m_objectID, m_ownerID))
380 database_ptr<TObject>::m_objectID =
other.
id();
381 database_ptr<TObject>::m_objectPtr =
other.weak();
382 database_ptr<TObject>::m_null =
other.null();
391 void setID(
typename database_ptr<TObject>::TIdentifierType
id = 0)
394 if (m_ownerID == 0 && database_ptr<TObject>::m_objectID != 0)
396 m_ownerID = getParentIdFromId(database_ptr<TObject>::m_objectID);
401 database_ptr<TObject>::m_objectID = 0;
402 database_ptr<TObject>::m_objectPtr.reset();
403 database_ptr<TObject>::m_null =
true;
407 if (m_ownerID == 0 || objectIdIsChildOfParent(
id, m_ownerID))
409 database_ptr<TObject>::m_objectID =
id;
410 database_ptr<TObject>::m_objectPtr.reset();
411 database_ptr<TObject>::m_null =
true;
416 void reset(
typename database_ptr<TObject>::TObjectPtr
object =
nullptr)
419 if (m_ownerID == 0 && database_ptr<TObject>::m_objectID != 0)
421 m_ownerID = getParentIdFromId(database_ptr<TObject>::m_objectID);
425 if (
object ==
nullptr)
427 database_ptr<TObject>::m_objectID = 0;
428 database_ptr<TObject>::m_objectPtr.reset();
429 database_ptr<TObject>::m_null =
true;
434 sbk_id
newObjectID =
static_cast<typename database_ptr<TObject>::TIdentifierType
>(*object);
436 if (m_ownerID == 0 || objectIdIsChildOfParent(
newObjectID, m_ownerID))
439 database_ptr<TObject>::m_objectPtr.reset();
440 database_ptr<TObject>::m_null =
true;
446 typename database_ptr<TObject>::TIdentifierType m_ownerID = 0;
452 template <
typename T>
453 struct hash<sbk::core::database_ptr<T>>
458 template <
typename T>
459 struct hash<sbk::core::child_ptr<T>>
465#include <rttr/wrapper_mapper.h>
469 template <
typename T>
470 struct wrapper_mapper<sbk::core::child_ptr<T>>
475 inline static wrapped_type get(
const type& obj) {
return obj.
id(); }
477 inline static type create(
const wrapped_type& t) {
return type(t); }
479 template <
typename T2>
484 ok = source.
hasId() == convertedLazyPtr.
hasId();
486 return convertedLazyPtr;
490 template <
typename T>
491 struct wrapper_mapper<sbk::core::database_ptr<T>>
496 inline static wrapped_type get(
const type& obj) {
return obj.
id(); }
498 inline static type create(
const wrapped_type& t) {
return type(t); }
500 template <
typename T2>
505 ok = source.
hasId() == convertedLazyPtr.
hasId();
507 return convertedLazyPtr;
Syntactic type to define a pointer that must be a child of the owning object.
Definition database_ptr.h:298
child_ptr(const database_object &owner)
Construct a new Child Ptr object with an owner.
Definition database_ptr.h:333
child_ptr(sbk_id id)
Construct a new child_ptr that points to the ID.
Definition database_ptr.h:340
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
TObjectPtr operator->() const
Access the raw object.
Definition database_ptr.h:238
database_ptr(std::nullptr_t)
Create an empty and null LazyPtr.
Definition database_ptr.h:75
database_ptr(sbk_id id)
Creates a LazyPtr that can lookup its object pointer after construction.
Definition database_ptr.h:49
bool pending() const
Returns true if we hold an ID but haven't found the live object to point to yet.
Definition database_ptr.h:132
bool operator!() const
Returns true if this LazyPtr is invalid.
Definition database_ptr.h:232
bool stale() const
Returns true if we previously referenced an object that has been destroyed.
Definition database_ptr.h:139
bool null() const
Returns true if the object pointer is not set.
Definition database_ptr.h:125
void reset(TObjectPtr object=nullptr)
Clear all references.
Definition database_ptr.h:166
TObjectPtr raw() const noexcept
Get raw pointer of the referenced object.
Definition database_ptr.h:106
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:191
database_ptr(const TObjectPtr &object)
Create a valid LazyPtr.
Definition database_ptr.h:66
sbk_id id() const noexcept
Get ID of the referenced object.
Definition database_ptr.h:84
bool hasId() const
Returns true if we hold a valid ID and can search for an object at runtime.
Definition database_ptr.h:119
bool valid() const
Returns true if we hold an ID and a valid pointer to the object.
Definition database_ptr.h:146
bool lookup() const
Find the live object referenced by the ID and store it.
Definition database_ptr.h:153
Base object that all sound Bakery objects should inherit from.
Definition object.h:21