Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Resource.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_RESOURCE_H_
34#define DVD_RESOURCE_H_
35
37
38namespace Divide
39{
40
55
56 enum class ResourceState : U8
57 {
58 RES_UNKNOWN = 0,
59 RES_CREATED = 1,
60 RES_LOADING = 2,
62 RES_LOADED = 4,
63 RES_UNLOADING = 5,
64 COUNT
65 };
66
67 class Resource : public GUIDWrapper
68 {
69 public:
70 explicit Resource( std::string_view resourceName, std::string_view typeName );
71
72 [[nodiscard]] ResourceState getState() const noexcept;
73
74 PROPERTY_R( Str<32>, typeName );
75 PROPERTY_R( Str<256>, resourceName );
76
77 protected:
78 virtual void setState( ResourceState currentState );
79
80 protected:
81 std::atomic<ResourceState> _resourceState;
82 };
83
84 void WaitForReady( Resource* res );
85 [[nodiscard]] bool SafeToDelete( Resource* res );
86
88
89 class CachedResource : public Resource
90 {
91 friend class ResourceCache;
92 friend struct ResourceLoader;
93
94 public:
95 explicit CachedResource( const ResourceDescriptorBase& descriptor, std::string_view typeName);
96
98 virtual bool load( PlatformContext& context );
99 virtual bool postLoad();
100 virtual bool unload();
101
102 void setState( ResourceState currentState ) final;
103
104 protected:
106 PROPERTY_RW( ResourcePath, assetLocation );
107 PROPERTY_RW( Str<256>, assetName );
108 PROPERTY_R( size_t, descriptorHash );
109 };
110
111 template<typename T> requires std::is_base_of_v<CachedResource, T>
112 using ResourcePtr = T*;
113
114 template<typename T>
116 {
117 };
118
119 template<typename T>
120 bool operator==( const PropertyDescriptor<T>& lhs, const PropertyDescriptor<T>& rhs ) noexcept;
121 template<typename T>
122 bool operator!=( const PropertyDescriptor<T>& lhs, const PropertyDescriptor<T>& rhs ) noexcept;
123
124
125 template<typename T>
126 [[nodiscard]] size_t GetHash( const PropertyDescriptor<T>& descriptor ) noexcept;
127
129 {
130 explicit ResourceDescriptorBase( const std::string_view resourceName );
131
132 [[nodiscard]] size_t getHash() const override;
133
134 PROPERTY_RW( ResourcePath, assetLocation );
135 PROPERTY_RW( Str<256>, assetName );
136 PROPERTY_RW( Str<256>, resourceName );
138 PROPERTY_RW( U32, enumValue, 0u );
139 PROPERTY_RW( U32, ID, 0u );
140 PROPERTY_RW( P32, mask );
141 PROPERTY_RW( bool, flag, false );
142 PROPERTY_RW( bool, waitForReady, true );
143 };
144
145 template <typename T>
147 {
148 explicit ResourceDescriptor( std::string_view resourceName );
149 explicit ResourceDescriptor( std::string_view resourceName, const PropertyDescriptor<T>& descriptor );
150
152
153 [[nodiscard]] size_t getHash() const final;
154 };
155
156 template<typename T>
157 bool operator==( const ResourceDescriptor<T>& lhs, const ResourceDescriptor<T>& rhs ) noexcept;
158 template<typename T>
159 bool operator!=( const ResourceDescriptor<T>& lhs, const ResourceDescriptor<T>& rhs ) noexcept;
160
161} // namespace Divide
162
163#endif //DVD_RESOURCE_H_
164
165#include "Resource.inl"
friend struct ResourceLoader
Definition: Resource.h:92
virtual bool postLoad()
Definition: Resource.cpp:60
virtual bool unload()
Definition: Resource.cpp:65
PROPERTY_RW(ResourcePath, assetLocation)
PROPERTY_RW(Str< 256 >, assetName)
void setState(ResourceState currentState) final
Definition: Resource.cpp:70
PROPERTY_R(size_t, descriptorHash)
Utility class that adds basic GUID management to objects.
Definition: GUIDWrapper.h:44
PROPERTY_R(Str< 32 >, typeName)
std::atomic< ResourceState > _resourceState
Definition: Resource.h:81
ResourceState getState() const noexcept
Definition: Resource.cpp:17
PROPERTY_R(Str< 256 >, resourceName)
virtual void setState(ResourceState currentState)
Definition: Resource.cpp:22
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
FORCE_INLINE bool operator!=(const GUIDWrapper &lhs, const GUIDWrapper &rhs) noexcept
Definition: GUIDWrapper.h:67
bool load() override
bool SafeToDelete(Resource *res)
Definition: Resource.cpp:35
std::mutex Mutex
Definition: SharedMutex.h:40
T * ResourcePtr
Definition: Resource.h:112
uint8_t U8
ResourceState
Definition: Resource.h:57
@ RES_UNLOADING
The resource is unloading, deleting data, etc.
@ RES_LOADED
The resource is available for usage.
@ RES_THREAD_LOADED
The resource is loaded but not yet available.
@ RES_CREATED
The pointer has been created and instantiated, but no data has been loaded.
@ RES_LOADING
The resource is loading, creating data, parsing scripts, etc.
@ RES_UNKNOWN
The resource exists, but it's state is undefined.
size_t GetHash(const PropertyDescriptor< T > &descriptor) noexcept
Definition: Resource.inl:40
void WaitForReady(Resource *res)
Definition: Resource.cpp:27
bool operator==(const DisplayManager::OutputDisplayProperties &lhs, const DisplayManager::OutputDisplayProperties &rhs) noexcept
Definition: Application.inl:37
static const vec3< F32 > VECTOR3_ZERO
Definition: MathVectors.h:1434
uint32_t U32
PROPERTY_RW(bool, flag, false)
PROPERTY_RW(bool, waitForReady, true)
PROPERTY_RW(vec3< U32 >, data, VECTOR3_ZERO)
general data
PROPERTY_RW(P32, mask)
4 bool values representing ... anything ...
PROPERTY_RW(ResourcePath, assetLocation)
Can't be fixed size due to the need to handle array textures, cube maps, etc.
size_t getHash() const override
Definition: Resource.cpp:82
PROPERTY_RW(Str< 256 >, assetName)
Resource instance name (for lookup)
PROPERTY_RW(Str< 256 >, resourceName)
PROPERTY_RW(U32, enumValue, 0u)
size_t getHash() const final
Definition: Resource.inl:71
PropertyDescriptor< T > _propertyDescriptor
Definition: Resource.h:151