CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Resource.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "ResourceHandle.hpp"
13#include "ResourceStream.hpp"
14
16
21
24
25namespace CeresEngine {
26
27 class InputStream;
28 class OutputStream;
29
30 class MemoryDataStream;
31
37 struct ResourceID : public UUID {
38 using UUID::UUID;
39
41 explicit ResourceID(const UUID& uuid) : UUID(uuid) {}
42
44 static const ResourceID null;
45 };
46
47 const inline ResourceID ResourceID::null = ResourceID();
48
49 // /// Type that uniquely represents a resource stream contained in a resource.
50 // struct ResourceStreamID : public UUID {
51 // using UUID::UUID;
52 //
53 // /// Converts a raw UUID into a `ResourceStreamID`.
54 // explicit ResourceStreamID(const UUID& uuid) : UUID(uuid) {}
55 //
56 // /// A null (or empty) resource stream ID.
57 // static const ResourceStreamID null;
58 // };
59 //
60 // const inline ResourceStreamID ResourceStreamID::null = ResourceStreamID();
61
64 const std::type_info& typeInfo;
65 const Type type;
66
67 ResourceTypeInfo(const std::type_info& typeInfo, const Type type) : typeInfo(typeInfo), type(type) {}
68
70
71 template<typename T> static const ResourceTypeInfo& get() {
72 static const ResourceTypeInfo typeInfo{typeid(T), getType<T>()};
73 return typeInfo;
74 }
75 };
76
80 template<typename T, typename... Args> SPtr<T> constructResource(ResourceData& resourceData, Args&&... args) {
81 if constexpr(std::is_constructible_v<T, ResourceData&, Args...>) {
82 return ce_shared_new<T>(resourceData, std::forward<Args>(args)...);
83 } else {
84 return ce_shared_new<T>(std::forward<Args>(args)...);
85 }
86 }
87
89 class ResourceData : public RefCounted<ResourceData> {
90 public:
93
96 const PackagePtr package = nullptr;
97
99 const ResourceID id = ResourceID::null;
100
103 const WeakResourceHandle<Resource> handle = nullptr;
104
109
115 SPtr<ResourceMetadata> metadata = nullptr;
116
117 private:
120
123
124 public:
128 explicit ResourceData(ResourceManager& resourceManager) noexcept : manager(resourceManager) {}
129
131 virtual ~ResourceData() noexcept = default;
132
133 public:
138 [[nodiscard]] virtual ResourceStreamData& createStream(const ResourceStreamFlags& flags = ResourceStreamFlag::Default) = 0;
139
144 [[nodiscard]] virtual ResourceStreamDataPtr getStream(const ResourceStreamID& streamID) const = 0;
145
147 [[nodiscard]] virtual Vector<ResourceStreamDataPtr> getStreams() const = 0;
148
152 virtual void destroyStream(ResourceStreamData& stream) = 0;
153 };
154
155 class MemoryResourceData final : public ResourceData {
156 private:
158
161
162 public:
166 explicit MemoryResourceData(ResourceManager& resourceManager) noexcept : ResourceData(resourceManager) {}
167
169 ~MemoryResourceData() noexcept final = default;
170
171 public:
173 [[nodiscard]] ResourceStreamData& createStream(const ResourceStreamFlags& flags = ResourceStreamFlag::Default) override;
174
176 [[nodiscard]] ResourceStreamDataPtr getStream(const ResourceStreamID& streamID) const override;
177
179 [[nodiscard]] Vector<ResourceStreamDataPtr> getStreams() const override;
180
182 void destroyStream(ResourceStreamData& stream) override;
183 };
184
186 class Resource : public std::enable_shared_from_this<Resource>, public IReflectable {
188
189 private:
195 ResourceDataPtr mData = nullptr;
196
197 public:
200
203 explicit Resource(ResourceData& data) : mData(&data) {}
204
206 virtual ~Resource() noexcept = default;
207
208 public:
210 [[nodiscard]] ResourceManager* getResourceManager() const noexcept { return mData != nullptr ? &mData->manager : nullptr; }
211
213 [[nodiscard]] HResource getResourceHandle() const noexcept { return mData != nullptr ? mData->handle.lock() : nullptr; }
214
216 [[nodiscard]] UUID getResourceID() const noexcept { return mData != nullptr ? mData->handle.getUUID() : UUID(); }
217
219 [[nodiscard]] HResource getParentResource() const noexcept { return mData != nullptr ? mData->parent.lock() : nullptr; }
220
222 [[nodiscard]] SPtr<ResourceMetadata> getResourceMetadata() const noexcept { return mData != nullptr ? mData->metadata : nullptr; }
223
225 [[nodiscard]] const PackagePtr& getPackage() const noexcept {
226 static const PackagePtr nullPackagePtr = nullptr;
227 return mData != nullptr ? mData->package : nullPackagePtr;
228 }
229
231 template<typename T = Resource> [[nodiscard]] SPtr<T> getShared() noexcept { return static_pointer_cast<T>(shared_from_this()); }
232
234 template<typename T = Resource> [[nodiscard]] SPtr<const T> getShared() const noexcept { return static_pointer_cast<const T>(shared_from_this()); }
235
236 protected: // Resource Streams
241 [[nodiscard]] HResourceStream createStream(const ResourceStreamFlags& flags = ResourceStreamFlag::Default);
242
245
246 public:
248 [[nodiscard]] virtual const ResourceTypeInfo& getResourceType() const noexcept = 0;
249
250 protected: // Change detection & tracking
252 void markAsDirty();
253
254 private:
255 friend class ResourceManager;
256
257 public: // Serialization
259 explicit Resource(ResourceData& data, InputStream& dataStream) : Resource(data) {}
260
262 virtual void serialize(OutputStream& dataStream) const = 0;
263
264 public: // Reflection
267 template<typename Processor> static constexpr void reflect(Processor&& RTTI) { CE_REFL_DATA_GET(resourceID, getResourceID); }
268 };
269
272 template<typename T, typename Base = Resource> class TResource : public Base {
273 [[nodiscard]] ::CeresEngine::ClassInfo getClassInfo() noexcept override { return {::CeresEngine::getTypeID<T>(), this}; }
274 [[nodiscard]] ::CeresEngine::ClassInfo getClassInfo() const noexcept override { return {::CeresEngine::getTypeID<T>(), this}; }
275
276 public:
277 using Base::Base;
278
279 public:
281 [[nodiscard]] ResourceHandle<T> getResourceHandle() const noexcept { return static_resource_cast<T>(Resource::getResourceHandle()); }
282
284 [[nodiscard]] SPtr<T> getShared() noexcept { return std::static_pointer_cast<T>(Resource::getShared()); }
285
287 [[nodiscard]] SPtr<const T> getShared() const noexcept { return std::static_pointer_cast<const T>(Resource::getShared()); }
288
289 public:
291 [[nodiscard]] const ResourceTypeInfo& getResourceType() const noexcept override { return ResourceTypeInfo::get<T>(); }
292
293 public: // Serialization
295 explicit TResource(ResourceData& data, InputStream& dataStream) : Base(data, dataStream) {
296 // TODO: Deserialize with reflection.
297 }
298
300 void serialize(OutputStream& dataStream) const override {
301 // TODO: Serialize with reflection.
302 }
303 };
304
308 template<typename T, bool = std::is_final_v<T>> class ResourceObject;
309
311 template<typename T> class ResourceObject<T, false> : public TResource<ResourceObject<T>>, public T {
313
314 public:
317 template<typename... Args>
318 explicit ResourceObject(Args&&... args) requires(is_explicitly_constructible<T, Args...>)
319 : T(std::forward<Args>(args)...) {}
320
323 template<typename... Args>
324 ResourceObject(Args&&... args) requires(is_implicitly_constructible<T, Args...>)
325 : T(std::forward<Args>(args)...) {}
326
330 template<typename... Args>
331 explicit ResourceObject(ResourceData& data, Args&&... args) : TResource<ResourceObject<T>>(data), T(std::forward<Args>(args)...) {}
332 };
333
335 template<typename T> class ResourceObject<T, true> : public TResource<ResourceObject<T>> {
337
338 private:
341
342 public:
345 template<typename... Args>
346 explicit ResourceObject(Args&&... args) requires(is_explicitly_constructible<T, Args...>)
347 : mValue(std::forward<Args>(args)...) {}
348
351 template<typename... Args>
352 ResourceObject(Args&&... args) requires(is_implicitly_constructible<T, Args...>)
353 : mValue(std::forward<Args>(args)...) {}
354
358 template<typename... Args>
359 explicit ResourceObject(ResourceData& data, Args&&... args) : TResource<ResourceObject<T>>(data), mValue(std::forward<Args>(args)...) {}
360
361 public:
363 T& get() { return mValue; }
364
366 const T& get() const { return mValue; }
367 };
368
371
372} // namespace CeresEngine
373
374template<> struct std::hash<CeresEngine::ResourceID> : std::hash<CeresEngine::UUID> {};
#define CE_REFLECTABLE_CLASS
Definition IReflectable.hpp:38
#define CE_REFL_DATA_GET(N, G)
Definition Macros.hpp:548
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
A stream associated to a resource.
Definition ResourceStream.hpp:430
An interface that must be implemented by types that wish to expose richer reflection data.
Definition IReflectable.hpp:29
A stream that provides read-only stream functionality.
Definition Stream.hpp:210
A helper class that wraps and protects a value from non-synchronized access.
Definition Threading.hpp:516
Definition Resource.hpp:155
LockedObject< StreamMap, SharedMutex > mStreams
The known resource streams.
Definition Resource.hpp:160
~MemoryResourceData() noexcept final=default
MemoryResourceData virtual destructor.
HashMap< ResourceStreamID, ResourceStreamDataPtr > StreamMap
Definition Resource.hpp:157
MemoryResourceData(ResourceManager &resourceManager) noexcept
Creates a new MemoryResourceData instance.
Definition Resource.hpp:166
A stream that provides write-only stream functionality.
Definition Stream.hpp:307
A simple reference counter base class.
Definition SmartPtr.hpp:438
An object, provided by the resource manager, to view and alter data from the resource itself.
Definition Resource.hpp:89
virtual ~ResourceData() noexcept=default
ResourceData virtual destructor.
ResourceManager & manager
The resource manager that owns the resource.
Definition Resource.hpp:92
Vector< HResource > mUnsavedResources
A list of sub-resources that are pending a save.
Definition Resource.hpp:122
Map< ResourceID, WeakResourceHandle< Resource > > mSubResources
A map that enumerates the sub-resurces attached to this resource.
Definition Resource.hpp:119
ResourceData(ResourceManager &resourceManager) noexcept
Creates a new ResourceData instance.
Definition Resource.hpp:128
Definition ResourceHandle.hpp:166
A base class that all resources must extend from.
Definition Resource.hpp:186
virtual ~Resource() noexcept=default
Resource virtual destructor.
virtual const ResourceTypeInfo & getResourceType() const noexcept=0
Returns the resource type information descriptor.
SPtr< ResourceMetadata > getResourceMetadata() const noexcept
A pointer to the user-defined resource metadata.
Definition Resource.hpp:222
Resource()
Default constructor for the Resource class.
static constexpr void reflect(Processor &&RTTI)
Executes the given processor for every field of the struct.
Definition Resource.hpp:267
HResourceStream createStream(const ResourceStreamFlags &flags=ResourceStreamFlag::Default)
Creates a new resource stream.
HResource getParentResource() const noexcept
A parent resource to which this resource is attached to.
Definition Resource.hpp:219
SPtr< T > getShared() noexcept
Gets a shared pointer to the resource.
Definition Resource.hpp:231
virtual void serialize(OutputStream &dataStream) const =0
Serializes a resource by writing data to dataStream.
HResource getResourceHandle() const noexcept
Definition Resource.hpp:213
SPtr< const T > getShared() const noexcept
Gets a shared pointer to the resource.
Definition Resource.hpp:234
Resource(ResourceData &data)
Creates a new Resource instance by passing a ResourceData instance.
Definition Resource.hpp:203
void destroyStream(HResourceStream &stream)
Destroys a stream.
UUID getResourceID() const noexcept
Definition Resource.hpp:216
const PackagePtr & getPackage() const noexcept
The package from which the resource was loaded from.
Definition Resource.hpp:225
The ResourceManager is the main class responsible for managing and handling all resources in the engi...
Definition ResourceManager.hpp:47
ResourceObject(Args &&... args)
Creates a new instance of the T resource.
Definition Resource.hpp:324
ResourceObject(ResourceData &data, Args &&... args)
Creates a new instance of the T resource.
Definition Resource.hpp:331
ResourceObject(Args &&... args)
Creates a new instance of the T resource.
Definition Resource.hpp:318
ResourceObject(Args &&... args)
Creates a new instance of the T resource.
Definition Resource.hpp:352
const T & get() const
The object value.
Definition Resource.hpp:366
T & get()
The object value.
Definition Resource.hpp:363
T mValue
The object value.
Definition Resource.hpp:340
ResourceObject(Args &&... args)
Creates a new instance of the T resource.
Definition Resource.hpp:346
ResourceObject(ResourceData &data, Args &&... args)
Creates a new instance of the T resource.
Definition Resource.hpp:359
A wrapper type that wraps a generic object into a resource.
Definition Resource.hpp:308
An object that allows storing side-band data for a resource.
Definition ResourceStream.hpp:129
Utility template class that can be extended by Resources to automatically implement methods that are ...
Definition Resource.hpp:272
::CeresEngine::ClassInfo getClassInfo() const noexcept override
Definition Resource.hpp:274
SPtr< T > getShared() noexcept
Gets a shared pointer to the resource.
Definition Resource.hpp:284
void serialize(OutputStream &dataStream) const override
Serializes a resource by writing data to dataStream.
Definition Resource.hpp:300
ResourceHandle< T > getResourceHandle() const noexcept
Definition Resource.hpp:281
const ResourceTypeInfo & getResourceType() const noexcept override
Returns the resource type information descriptor.
Definition Resource.hpp:291
SPtr< const T > getShared() const noexcept
Gets a shared pointer to the resource.
Definition Resource.hpp:287
TResource(ResourceData &data, InputStream &dataStream)
Definition Resource.hpp:295
::CeresEngine::ClassInfo getClassInfo() noexcept override
Definition Resource.hpp:273
Represents a reflected C++ type. Can be used to get metadata from a C++ type.
Definition Type.hpp:32
StringView getTypeName() const noexcept
Definition ResourceHandle.hpp:339
Definition Application.hpp:19
constexpr bool is_implicitly_constructible
A type trait type that checks if Type is implicitly constructible from Arguments.
Definition TypeTraits.hpp:41
SPtr< T > constructResource(ResourceData &resourceData, Args &&... args)
Constructs a new resource instance.
Definition Resource.hpp:80
std::shared_ptr< T > SPtr
SPtr is a smart pointer that retains shared ownership of an object through a pointer.
Definition SmartPtr.hpp:37
constexpr bool is_explicitly_constructible
A type trait type that checks if Type is explicitly constructible from Arguments.
Definition TypeTraits.hpp:27
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
ResourceStreamFlag
A set of flags that can be given when creating a new resource stream.
Definition ResourceStream.hpp:38
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
auto parent(const Entity &parent)
Sets the entity parent.
Definition Helpers.hpp:52
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
std::map< Key, T, Compare, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > Map
Map is a sorted associative container that contains key-value pairs with unique keys.
Definition Map.hpp:24
Definition Span.hpp:668
A structure that contains type information for a class.
Definition IReflectable.hpp:15
Wrapper around an enum that allows simple use of bitwise logic operations.
Definition Flags.hpp:19
Type that uniquely represents a resource in the resource system.
Definition Resource.hpp:37
ResourceID(const UUID &uuid)
Converts a raw UUID into a ResouceID.
Definition Resource.hpp:41
static const ResourceID null
A null (or empty) resource ID.
Definition Resource.hpp:44
Type that uniquely represents a stream in the resource system.
Definition ResourceStream.hpp:24
A structure that describes type information for a resource.
Definition Resource.hpp:63
StringView getName() const noexcept
Definition Resource.hpp:69
const std::type_info & typeInfo
Definition Resource.hpp:64
ResourceTypeInfo(const std::type_info &typeInfo, const Type type)
Definition Resource.hpp:67
static const ResourceTypeInfo & get()
Definition Resource.hpp:71
const Type type
Definition Resource.hpp:65
Represents a universally unique identifier (UUID).
Definition UUID.hpp:27
constexpr UUID()=default
Initializes an empty UUID.