3#include <Core/CoreDLL.h>
4#include <Core/ResourceManager/Implementation/Declarations.h>
5#include <Foundation/Reflection/Reflection.h>
6#include <Foundation/Strings/String.h>
11#define PL_RESOURCEHANDLE_STACK_TRACES PL_OFF
19PL_CORE_DLL
void IncreaseResourceRefCount(
plResource* pResource,
const void* pOwner);
20PL_CORE_DLL
void DecreaseResourceRefCount(
plResource* pResource,
const void* pOwner);
22#if PL_ENABLED(PL_RESOURCEHANDLE_STACK_TRACES)
23PL_CORE_DLL
void MigrateResourceRefCount(
plResource* pResource,
const void* pOldOwner,
const void* pNewOwner);
25PL_ALWAYS_INLINE
void MigrateResourceRefCount(
plResource* pResource,
const void* pOldOwner,
const void* pNewOwner)
42 m_pResource = rhs.m_pResource;
46 IncreaseResourceRefCount(m_pResource,
this);
53 m_pResource = rhs.m_pResource;
54 rhs.m_pResource =
nullptr;
58 MigrateResourceRefCount(m_pResource, &rhs,
this);
66 PL_ALWAYS_INLINE
bool IsValid()
const {
return m_pResource !=
nullptr; }
73 plUInt64 GetResourceIDHash()
const;
77 const plString& GetResourceID()
const;
101 const plRTTI* GetResourceType()
const;
109 friend class plResourceHandleWriteContext;
110 friend class plResourceHandleReadContext;
134template <
typename RESOURCE_TYPE>
138 using ResourceType = RESOURCE_TYPE;
145 : m_hTypeless(pResource)
151 : m_hTypeless(rhs.m_hTypeless)
157 : m_hTypeless(std::move(rhs.m_hTypeless))
161 template <
typename BaseOrDerivedType>
163 : m_hTypeless(rhs.m_hTypeless)
165 static_assert(std::is_base_of<ResourceType, BaseOrDerivedType>::value || std::is_base_of<BaseOrDerivedType, ResourceType>::value,
"Only related types can be assigned to handles of this type");
167#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
168 if (std::is_base_of<BaseOrDerivedType, ResourceType>::value)
170 PL_ASSERT_DEBUG(rhs.
IsValid(),
"Cannot cast invalid base handle to derived type!");
172 PL_ASSERT_DEBUG(plDynamicCast<const ResourceType*>(lock.GetPointer()) !=
nullptr,
"Types are not related!");
209 PL_ALWAYS_INLINE
explicit operator bool()
const {
return m_hTypeless.
IsValid(); }
232 m_hTypeless = hHandle;
236 template <
typename T>
241 friend class plResourceHandleWriteContext;
242 friend class plResourceHandleReadContext;
262 template <
typename ResourceType>
265 WriteHandle(inout_stream, hResource.m_hTypeless.m_pResource);
268 template <
typename ResourceType>
271 ReadHandle(inout_stream, ref_hResourceHandle.m_hTypeless);
280template <
typename ResourceType>
283 plResourceHandleStreamOperations::WriteHandle(inout_stream, hValue);
287template <
typename ResourceType>
290 plResourceHandleStreamOperations::ReadHandle(inout_stream, ref_hValue);
static constexpr plUInt32 StringHashTo32(plUInt64 uiHash)
Truncates a 64 bit string hash to 32 bit.
Definition HashingUtils_inl.h:138
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
PL_ALWAYS_INLINE plStringView GetTypeName() const
Returns the name of this type.
Definition RTTI.h:48
PL_ALWAYS_INLINE bool IsDerivedFrom(const plRTTI *pBaseType) const
Returns true if this type is derived from the given type (or of the same type).
Definition RTTI.h:60
Definition ResourceHandle.h:260
The base class for all resources.
Definition Resource.h:10
Helper class to acquire and release a resource safely.
Definition ResourceLock.h:21
The central class for managing all types derived from plResource.
Definition ResourceManager.h:16
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
The plTypedResourceHandle controls access to an plResource.
Definition ResourceHandle.h:136
plTypedResourceHandle(plTypedResourceHandle< ResourceType > &&rhs)
Move constructor, no refcount change is necessary.
Definition ResourceHandle.h:156
PL_ALWAYS_INLINE void Invalidate()
Clears any reference to a resource and reduces its refcount.
Definition ResourceHandle.h:212
PL_ALWAYS_INLINE bool operator<(const plTypedResourceHandle< ResourceType > &rhs) const
For storing handles as keys in maps.
Definition ResourceHandle.h:190
plTypedResourceHandle(ResourceType *pResource)
Increases the refcount of the given resource.
Definition ResourceHandle.h:144
PL_ALWAYS_INLINE bool operator==(const plTypedResourceHandle< ResourceType > &rhs) const
Checks whether the two handles point to the same resource.
Definition ResourceHandle.h:184
PL_ALWAYS_INLINE bool operator==(const plResource *rhs) const
Checks whether the handle points to the given resource.
Definition ResourceHandle.h:193
PL_ALWAYS_INLINE bool IsValid() const
Returns whether the handle stores a valid pointer to a resource.
Definition ResourceHandle.h:206
void operator=(const plTypedResourceHandle< ResourceType > &rhs)
Releases the current reference and increases the refcount of the given resource.
Definition ResourceHandle.h:178
plTypedResourceHandle(const plTypedResourceHandle< ResourceType > &rhs)
Increases the refcount of the given resource.
Definition ResourceHandle.h:150
PL_ALWAYS_INLINE bool operator!=(const plResource *rhs) const
Checks whether the handle points to the given resource.
Definition ResourceHandle.h:196
void operator=(plTypedResourceHandle< ResourceType > &&rhs)
Move operator, no refcount change is necessary.
Definition ResourceHandle.h:181
PL_ALWAYS_INLINE plUInt64 GetResourceIDHash() const
Returns the Resource ID hash of the exact resource that this handle points to, without acquiring the ...
Definition ResourceHandle.h:216
PL_ALWAYS_INLINE bool operator!=(const plTypedResourceHandle< ResourceType > &rhs) const
Checks whether the two handles point to the same resource.
Definition ResourceHandle.h:187
plTypedResourceHandle()=default
A default constructed handle is invalid and does not reference any resource.
void AssignFromTypelessHandle(const plTypelessResourceHandle &hHandle)
Attempts to copy the given typeless handle to this handle.
Definition ResourceHandle.h:225
PL_ALWAYS_INLINE const plString & GetResourceID() const
Returns the Resource ID of the exact resource that this handle points to, without acquiring the resou...
Definition ResourceHandle.h:220
The typeless implementation of resource handles. A typed interface is provided by plTypedResourceHand...
Definition ResourceHandle.h:32
PL_ALWAYS_INLINE bool operator==(const plResource *rhs) const
Checks whether the handle points to the given resource.
Definition ResourceHandle.h:95
PL_ALWAYS_INLINE plTypelessResourceHandle(plTypelessResourceHandle &&rhs)
Move constructor, no refcount change is necessary.
Definition ResourceHandle.h:51
const plRTTI * GetResourceType() const
Returns the type information of the resource or nullptr if the handle is invalid.
Definition ResourceHandle.cpp:35
plUInt64 GetResourceIDHash() const
Returns the Resource ID hash of the exact resource that this handle points to, without acquiring the ...
Definition ResourceHandle.cpp:25
PL_ALWAYS_INLINE bool operator!=(const plResource *rhs) const
Checks whether the handle points to the given resource.
Definition ResourceHandle.h:98
PL_ALWAYS_INLINE ~plTypelessResourceHandle()
Releases any referenced resource.
Definition ResourceHandle.h:63
PL_ALWAYS_INLINE plTypelessResourceHandle(const plTypelessResourceHandle &rhs)
Increases the refcount of the given resource.
Definition ResourceHandle.h:40
PL_ALWAYS_INLINE bool operator==(const plTypelessResourceHandle &rhs) const
Checks whether the two handles point to the same resource.
Definition ResourceHandle.h:86
PL_ALWAYS_INLINE bool IsValid() const
Returns whether the handle stores a valid pointer to a resource.
Definition ResourceHandle.h:66
PL_ALWAYS_INLINE bool operator!=(const plTypelessResourceHandle &rhs) const
Checks whether the two handles point to the same resource.
Definition ResourceHandle.h:89
const plString & GetResourceID() const
Returns the Resource ID of the exact resource that this handle points to, without acquiring the resou...
Definition ResourceHandle.cpp:30
PL_ALWAYS_INLINE bool operator<(const plTypelessResourceHandle &rhs) const
For storing handles as keys in maps.
Definition ResourceHandle.h:92
void Invalidate()
Clears any reference to a resource and reduces its refcount.
Definition ResourceHandle.cpp:15
Helper struct to calculate the Hash of different types.
Definition HashingUtils.h:75