3#include "sound_bakery/core/database/database_object.h"
4#include "sound_bakery/pch.h"
10 std::weak_ptr<DatabaseObject> SB_API findObject(SB_ID
id);
11 bool SB_API objectIdIsChildOfParent(SB_ID childToCheck, SB_ID parent);
12 SB_ID SB_API getParentIdFromId(SB_ID
id);
21 template <
typename TObject>
26 using TIdentifierType = SB_ID;
27 using TObjectPtr = TObject*;
28 using TObjectRef = TObject&;
29 using TObjectWeak = std::weak_ptr<DatabaseObject>;
30 using TObjectShared = std::shared_ptr<TObject>;
31 using TPtrType = std::weak_ptr<DatabaseObject>;
33 static_assert(!std::is_pointer<TObject>::value);
56 : m_objectID(static_cast<TIdentifierType>(*object)), m_objectPtr(findObject(
id())), m_null(false)
64 DatabasePtr(std::nullptr_t) : m_objectID(), m_objectPtr(), m_null(true) {}
73 SB_ID
id() const noexcept {
return m_objectID; }
75 TObjectShared shared() const noexcept
79 if (m_objectPtr.expired())
81 return std::shared_ptr<TObject>();
85 return std::static_pointer_cast<TObject>(m_objectPtr.lock());
89 TObjectWeak weak() const noexcept {
return m_objectPtr; }
95 TObjectPtr
raw() const noexcept {
return shared().get(); }
97 TObjectPtr lookupRaw() const noexcept
108 bool hasId()
const {
return m_objectID != TIdentifierType(); }
114 bool null()
const {
return m_null || m_objectPtr.expired(); }
128 bool stale()
const {
return !m_null && m_objectPtr.expired(); }
146 m_objectPtr = findObject(
id());
147 m_null = m_objectPtr.expired();
155 void reset(TObjectPtr
object =
nullptr)
157 m_objectID =
object ?
static_cast<TIdentifierType
>(*object) : TIdentifierType();
180 TThisType&
operator=(
const TThisType& other)
182 if (
id() != other.id())
184 m_objectID = other.id();
185 m_objectPtr = other.weak();
186 m_null = other.null();
192 TThisType&
operator=(
const TThisType&& other)
194 if (
id() != other.id())
196 m_objectID = other.id();
197 m_objectPtr = other.weak();
198 m_null = other.null();
207 operator bool()
const {
return valid(); }
222 mutable TPtrType m_objectPtr = TPtrType();
236 template <
typename T1,
typename T2>
237 bool operator==(
const DatabasePtr<T1>& lhs,
const DatabasePtr<T2>& rhs)
239 return lhs.id() == rhs.id();
250 template <
typename T>
251 bool operator==(
const DatabasePtr<T>& lhs,
const T* rhs)
253 return lhs.
raw() == rhs;
266 template <
typename T1,
typename T2>
267 bool operator<(
const DatabasePtr<T1>& lhs,
const DatabasePtr<T2>& rhs)
269 return lhs.
id() < rhs.id();
276 template <
typename TObject>
298 m_ownerID = getParentIdFromId(
other.m_objectID);
322 TThisType& operator=(
typename DatabasePtr<TObject>::TIdentifierType
id)
329 TThisType& operator=(
typename DatabasePtr<TObject>::TObjectPtr
object)
336 TThisType& operator=(
const TThisType&
other)
340 if (m_ownerID == 0 && DatabasePtr<TObject>::m_objectID != 0)
342 m_ownerID = getParentIdFromId(DatabasePtr<TObject>::m_objectID);
349 DatabasePtr<TObject>::m_objectID =
other.id();
350 DatabasePtr<TObject>::m_objectPtr =
other.weak();
351 DatabasePtr<TObject>::m_null =
other.null();
352 m_ownerID =
other.m_ownerID;
355 else if (m_ownerID ==
other.m_ownerID ||
other.m_ownerID == 0)
358 if (objectIdIsChildOfParent(
other.m_objectID, m_ownerID))
360 DatabasePtr<TObject>::m_objectID =
other.
id();
361 DatabasePtr<TObject>::m_objectPtr =
other.weak();
362 DatabasePtr<TObject>::m_null =
other.null();
371 void setID(
typename DatabasePtr<TObject>::TIdentifierType
id = 0)
374 if (m_ownerID == 0 && DatabasePtr<TObject>::m_objectID != 0)
376 m_ownerID = getParentIdFromId(DatabasePtr<TObject>::m_objectID);
381 DatabasePtr<TObject>::m_objectID = 0;
382 DatabasePtr<TObject>::m_objectPtr.reset();
383 DatabasePtr<TObject>::m_null =
true;
387 if (m_ownerID == 0 || objectIdIsChildOfParent(
id, m_ownerID))
389 DatabasePtr<TObject>::m_objectID =
id;
390 DatabasePtr<TObject>::m_objectPtr.reset();
391 DatabasePtr<TObject>::m_null =
true;
396 void reset(
typename DatabasePtr<TObject>::TObjectPtr
object =
nullptr)
399 if (m_ownerID == 0 && DatabasePtr<TObject>::m_objectID != 0)
401 m_ownerID = getParentIdFromId(DatabasePtr<TObject>::m_objectID);
405 if (
object ==
nullptr)
407 DatabasePtr<TObject>::m_objectID = 0;
408 DatabasePtr<TObject>::m_objectPtr.reset();
409 DatabasePtr<TObject>::m_null =
true;
416 if (m_ownerID == 0 || objectIdIsChildOfParent(
newObjectID, m_ownerID))
419 DatabasePtr<TObject>::m_objectPtr.reset();
420 DatabasePtr<TObject>::m_null =
true;
426 typename DatabasePtr<TObject>::TIdentifierType m_ownerID = 0;
432 template <
typename T>
433 struct hash<SB::Core::DatabasePtr<T>>
438 template <
typename T>
439 struct hash<SB::Core::ChildPtr<T>>
445#include <rttr/wrapper_mapper.h>
449 template <
typename T>
450 struct wrapper_mapper<SB::Core::ChildPtr<T>>
455 inline static wrapped_type get(
const type& obj) {
return obj.
id(); }
457 inline static type create(
const wrapped_type& t) {
return type(t); }
459 template <
typename T2>
464 ok = source.
hasId() == convertedLazyPtr.
hasId();
466 return convertedLazyPtr;
470 template <
typename T>
471 struct wrapper_mapper<SB::Core::DatabasePtr<T>>
476 inline static wrapped_type get(
const type& obj) {
return obj.
id(); }
478 inline static type create(
const wrapped_type& t) {
return type(t); }
480 template <
typename T2>
485 ok = source.
hasId() == convertedLazyPtr.
hasId();
487 return convertedLazyPtr;
Syntactic type to define a pointer that must be a child of the owning object.
Definition database_ptr.h:278
ChildPtr(SB_ID id)
Construct a new ChildPtr that points to the ID.
Definition database_ptr.h:320
ChildPtr(const DatabaseObject &owner)
Construct a new Child Ptr object with an owner.
Definition database_ptr.h:313
ChildPtr()=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:22
Definition database_ptr.h:23
DatabasePtr()
Creates an empty and null LazyPtr.
Definition database_ptr.h:39
bool null() const
Returns true if the object pointer is not set.
Definition database_ptr.h:114
bool stale() const
Returns true if we previously referenced an object that has been destroyed.
Definition database_ptr.h:128
bool lookup() const
Find the live object referenced by the ID and store it.
Definition database_ptr.h:142
void reset(TObjectPtr object=nullptr)
Clear all references.
Definition database_ptr.h:155
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:121
TObjectPtr raw() const noexcept
Get raw pointer of the referenced object.
Definition database_ptr.h:95
bool valid() const
Returns true if we hold an ID and a valid pointer to the object.
Definition database_ptr.h:135
TObjectPtr operator->() const
Access the raw object.
Definition database_ptr.h:218
DatabasePtr(std::nullptr_t)
Create an empty and null LazyPtr.
Definition database_ptr.h:64
bool hasId() const
Returns true if we hold a valid ID and can search for an object at runtime.
Definition database_ptr.h:108
DatabasePtr(SB_ID id)
Creates a LazyPtr that can lookup its object pointer after construction.
Definition database_ptr.h:49
SB_ID id() const noexcept
Get ID of the referenced object.
Definition database_ptr.h:73
DatabasePtr(const TObjectPtr &object)
Create a valid LazyPtr.
Definition database_ptr.h:55
bool operator!() const
Returns true if this LazyPtr is invalid.
Definition database_ptr.h:212
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:171