Cogs.Core
ResourceHandle.h
1#pragma once
2
3#include "../Base.h"
4
5#include "Foundation/HashFunctions.h"
6
7#include <functional>
8
9namespace Cogs
10{
11 namespace Core
12 {
13 struct ResourceBase;
14
18 struct COGSCORE_DLL_API ResourceHandleBase
19 {
21 ResourceHandleBase() = default;
22
29 {
30 reset(resource);
31 }
32
39 {
40 reset(other.resource);
41 }
42
48 ResourceHandleBase(ResourceHandleBase && other) noexcept : resource(other.resource)
49 {
50 other.resource = nullptr;
51 }
52
57
65 ResourceHandleBase & operator=(const ResourceHandleBase & other) { reset(other.resource); return *this; }
66
75 bool operator==(const ResourceHandleBase & other) const { return this->resource == other.resource; }
76
85 bool operator!=(const ResourceHandleBase & other) const { return !(*this == other); }
86
102 operator bool() const { return this->resource != nullptr; }
103
104
105 ResourceBase * operator->() { return this->resource; }
106 const ResourceBase * operator->() const { return this->resource; }
107
108 ResourceBase * get() { return this->resource; }
109
110 const ResourceBase * get() const { return this->resource; }
111
120 void reset(ResourceBase * otherResource);
121
124
126 ResourceId getId() const;
127
128 size_t hash(size_t hashValue = Cogs::hash()) const {
129 return Cogs::hash(reinterpret_cast<intptr_t>(resource), hashValue);
130 }
131
132 protected:
141 void resetInternal(ResourceBase * otherResource);
142
144 ResourceBase * resource = nullptr;
145 };
146
153 template<typename ResourceType>
155 {
157 ResourceHandle_t() = default;
158
161
164
166 ResourceHandle_t(ResourceHandle_t && other) noexcept : ResourceHandleBase(std::move(other)) {}
167
170
172 {
173 reset(const_cast<ResourceBase *>(other.get()));
174 }
175
182 ResourceType * resolve() const { return static_cast<ResourceType *>(resource); }
183
190 ResourceType * operator->() const { return static_cast<ResourceType *>(resource); }
191
194
195 private:
196 // Let hashing algorithm access our internal contents.
197 friend struct std::hash<ResourceHandle_t<ResourceType>>;
198 };
199
201 template<typename T>
203
211 template<typename T>
212 inline bool HandleIsValid(const ResourceHandle_t<T> & handle)
213 {
214 return handle != ResourceHandle_t<T>::NoHandle;
215 }
216
217 inline bool HandleIsValid(const ResourceHandleBase & handle)
218 {
219 return handle != ResourceHandleBase::NoHandle;
220 }
221 }
222}
223
225
226namespace std
227{
229 template<>
230 struct hash<Cogs::Core::ResourceHandleBase>
231 {
232 std::size_t operator()(const Cogs::Core::ResourceHandleBase & handle) const
233 {
234 return std::hash<const void *>()(handle.get());
235 }
236 };
237}
238
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
STL namespace.
Base class for engine resources.
Definition: ResourceBase.h:107
Resource handle base class handling reference counting of resources derived from ResourceBase.
static const ResourceHandleBase NoHandle
Provided as shorthand for empty resource handles.
void reset(ResourceBase *otherResource)
Reset the contents of this instance to the given handle and resource.
ResourceHandleBase & operator=(const ResourceHandleBase &other)
Assign the handle from the given other handle.
ResourceHandleBase()=default
Constructs an empty handle, holding no resource.
ResourceHandleBase(ResourceHandleBase &&other) noexcept
Move constructs a handle from the given other handle.
bool operator==(const ResourceHandleBase &other) const
Compares this instance against the contents of other.
bool operator!=(const ResourceHandleBase &other) const
Compares this instance against other for inequality.
ResourceHandleBase(const ResourceHandleBase &other)
Copy constructs a handle from the given other handle.
ResourceHandleBase(ResourceBase *resource)
Constructs a handle with the given integer handle and resource pointer pair.
ResourceBase * resource
Pointer to held resource.
Opaque resource handle for holding references to resources.
ResourceHandle_t()=default
Default construct a resource handle.
ResourceType * operator->() const
Pointer operator, returns the held resource.
ResourceHandle_t & operator=(const ResourceHandle_t &other)
Copy assign the handle from the given other handle.
ResourceHandle_t(const ResourceHandle_t &other)
Copy construct a handle.
ResourceHandle_t(ResourceBase *resource)
Constructs a handle from the given resource pointer.
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
ResourceHandle_t(ResourceHandle_t &&other) noexcept
Move construct a handle from the given r-value.
ResourceType * resolve() const
Resolve the handle, returning a pointer to the actual resource.