Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
ResourceId.hpp
1/*
2Copyright (C) 2018,2019,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGAMES_RESOURCEID_H
17#define AEONGAMES_RESOURCEID_H
18
19#include <cstdint>
20#include <string>
21#include "aeongames/AeonEngine.hpp"
22#include "aeongames/CRC.hpp"
23#include "aeongames/ResourceFactory.hpp"
24#include "aeongames/ResourceCache.hpp"
25
26namespace AeonGames
27{
30 {
31 public:
33 ResourceId() = default;
35 ResourceId ( const ResourceId& ) = default;
39 ResourceId ( uint32_t aType, uint32_t aPath ) :
40 mType{aType}, mPath{aPath} {}
41
44 ResourceId ( const std::string& aType, const std::string& aPath ) :
45 mType{crc32i ( aType.data(), aType.length() ) }, mPath{crc32i ( aPath.data(), aPath.length() ) } {}
46
49 ResourceId ( const std::string& aType, uint32_t aPath ) :
50 mType{crc32i ( aType.data(), aType.length() ) }, mPath{aPath} {}
51
54 ResourceId ( uint32_t aType, const std::string& aPath ) :
55 mType{aType}, mPath{crc32i ( aPath.data(), aPath.length() ) } {}
56 ~ResourceId() = default;
57
60 operator bool() const
61 {
62 return GetResource ( *this ).GetRaw() != nullptr;
63 }
64
67 uint32_t GetType() const
68 {
69 return mType;
70 }
71
74 uint32_t GetPath() const
75 {
76 return mPath;
77 }
78
81 std::string GetPathString() const
82 {
83 return GetResourcePath ( mPath );
84 }
85
89 template<typename T>
90 T* Cast() const
91 {
92 return GetResource ( *this ).Get<T>();
93 }
94
98 template<typename T>
99 T* Get() const
100 {
101 T* t = GetResource ( *this ).Get<T>();
102 if ( !t )
103 {
104 t = StoreResource ( mPath, ConstructResource ( *this ) ).Get<T>();
105 }
106 return t;
107 }
108
110 void Store() const
111 {
112 // Don't store nullptrs
113 if ( !GetResource ( *this ).GetRaw() )
114 {
115 StoreResource ( mPath, ConstructResource ( *this ) );
116 }
117 }
118
120 void Dispose() const
121 {
122 if ( GetResource ( *this ).GetRaw() )
123 {
124 DisposeResource ( mPath );
125 }
126 }
127
128 private:
129 uint32_t mType{};
130 uint32_t mPath{};
131 };
132}
133
134#endif
Identifies a resource by its type and path CRC32 hashes.
uint32_t GetType() const
Get the CRC32 hash of the resource type.
ResourceId()=default
Default constructor.
T * Get() const
Get the resource, loading and caching it if necessary.
ResourceId(uint32_t aType, const std::string &aPath)
Construct from a pre-computed type hash and a path string.
void Dispose() const
Remove the resource from the cache if present.
T * Cast() const
Cast the cached resource to the specified type without loading.
void Store() const
Construct and store the resource in the cache if not already present.
ResourceId(const std::string &aType, uint32_t aPath)
Construct from a type string and a pre-computed path hash.
ResourceId(const ResourceId &)=default
Copy constructor.
ResourceId(const std::string &aType, const std::string &aPath)
Construct from type and path strings (hashed internally).
std::string GetPathString() const
Get the original resource path string from the hash.
uint32_t GetPath() const
Get the CRC32 hash of the resource path.
ResourceId(uint32_t aType, uint32_t aPath)
Construct from pre-computed CRC32 hashes.
const void * GetRaw() const
Returns a const void pointer to the owned object.
T * Get() const
Returns a typed pointer to the owned object (const overload).
<- This is here just for the literals
Definition AABB.hpp:31
DLL std::vector< std::string > GetResourcePath()
Get the list of resource search paths.
uint32_t crc32i(const char *message, size_t size, uint32_t previous_crc)
Compute the CRC32 of a given message, continuing from a previous CRC value.
Definition CRC.cpp:27
DLL UniqueAnyPtr ConstructResource(const ResourceId &aResourceId)
Construct a resource identified by a ResourceId.
DLL const UniqueAnyPtr & StoreResource(uint32_t aKey, UniqueAnyPtr &&pointer)
Store a resource in the cache.
DLL UniqueAnyPtr DisposeResource(uint32_t aKey)
Remove and return a resource from the cache.
DLL const UniqueAnyPtr & GetResource(uint32_t aKey)
Retrieve a cached resource by key.