Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
ResourceFactory.cpp
1/*
2Copyright (C) 2018,2022,2025 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
8 http://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#include <tuple>
17#include <sstream>
18#include <exception>
19#include <unordered_map>
20#include "aeongames/ResourceFactory.hpp"
21#include "aeongames/ResourceCache.hpp"
22#include "aeongames/ResourceId.hpp"
23
24namespace AeonGames
25{
26 static std::unordered_map < uint32_t, std::tuple<std::function < UniqueAnyPtr ( uint32_t ) >, UniqueAnyPtr >> Constructors;
27
29 {
30 auto it = Constructors.find ( aResourceId.GetType() );
31 if ( it != Constructors.end() )
32 {
33 return std::get<0> ( it->second ) ( aResourceId.GetPath() );
34 }
35 std::ostringstream stream;
36 stream << "No constructor registered for type " << aResourceId.GetType();
37 throw std::runtime_error ( stream.str().c_str() );
38 }
39 bool RegisterResourceConstructor ( uint32_t aType, const std::function < UniqueAnyPtr ( uint32_t ) > & aConstructor, UniqueAnyPtr&& aDefaultResource )
40 {
41 if ( Constructors.find ( aType ) == Constructors.end() )
42 {
43 Constructors[aType] = std::make_pair ( aConstructor, std::move ( aDefaultResource ) );
44 return true;
45 }
46 return false;
47 }
48 bool UnregisterResourceConstructor ( uint32_t aType )
49 {
50 auto it = Constructors.find ( aType );
51 if ( it != Constructors.end() )
52 {
53 Constructors.erase ( it );
54 return true;
55 }
56 return false;
57 }
58 void EnumerateResourceConstructors ( const std::function<bool ( uint32_t ) >& aEnumerator )
59 {
60 for ( auto& i : Constructors )
61 {
62 if ( !aEnumerator ( i.first ) )
63 {
64 return;
65 }
66 }
67 }
68
69 const UniqueAnyPtr& GetDefaultResource ( uint32_t aType )
70 {
71 static const UniqueAnyPtr unique_any_nullptr{nullptr};
72 auto it = Constructors.find ( aType );
73 if ( it != Constructors.end() )
74 {
75 return std::get<1> ( it->second );
76 }
77 return unique_any_nullptr;
78 }
79}
Identifies a resource by its type and path CRC32 hashes.
uint32_t GetType() const
Get the CRC32 hash of the resource type.
uint32_t GetPath() const
Get the CRC32 hash of the resource path.
A type-erased owning smart pointer with unique ownership semantics.
<- This is here just for the literals
Definition AABB.hpp:31
DLL const UniqueAnyPtr & GetDefaultResource(uint32_t aType)
Get the default resource for a given type.
DLL UniqueAnyPtr ConstructResource(const ResourceId &aResourceId)
Construct a resource identified by a ResourceId.
DLL bool UnregisterResourceConstructor(uint32_t aType)
Unregister a resource constructor.
DLL void EnumerateResourceConstructors(const std::function< bool(uint32_t) > &aEnumerator)
Enumerate all registered resource constructors.
DLL bool RegisterResourceConstructor(uint32_t aType, const std::function< UniqueAnyPtr(uint32_t) > &aConstructor, UniqueAnyPtr &&aDefaultResource=nullptr)
Register a constructor for a resource type.