Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
CookedResourceCollectionBase.h
Go to the documentation of this file.
1#pragma once
2
6#include "SDL/SdlResourceId.h"
7
8#include <Common/logging.h>
9
10#include <string>
11#include <memory>
12#include <unordered_map>
13#include <utility>
14
15namespace ph
16{
17
19
25{
26public:
27 virtual ~CookedResourceCollectionBase() = default;
28
29 virtual std::string getStats() const = 0;
30
31protected:
32 template<typename CookedType>
33 using TSdlResourceIdMap = std::unordered_map<SdlResourceId, std::unique_ptr<CookedType>>;
34
35 template<typename DerivedType, typename BaseType, typename... DeducedArgs>
36 [[nodiscard]]
37 static DerivedType* makeCookedResource(
38 TSynchronized<TUniquePtrVector<BaseType>>& syncedResources,
39 DeducedArgs&&... args)
40 {
41 // Create resource in separate expression since no lock is required yet
42 auto newResource = std::make_unique<DerivedType>(std::forward<DeducedArgs>(args)...);
43
44 DerivedType* resourcePtr = nullptr;
45 syncedResources.locked(
46 [&resourcePtr, &newResource](auto& resources)
47 {
48 resourcePtr = resources.add(std::move(newResource));
49 });
50
51 return resourcePtr;
52 }
53
54 template<typename CookedType, typename... DeducedArgs>
55 static CookedType* makeCookedResourceWithID(
56 TSynchronized<TSdlResourceIdMap<CookedType>>& syncedIdToResource,
57 const SdlResourceId id,
58 DeducedArgs&&... args)
59 {
60 // Create resource in separate expression since no lock is required yet
61 auto newResource = std::make_unique<CookedType>(std::forward<DeducedArgs>(args)...);
62
63 CookedType* resourcePtr = nullptr;
64 syncedIdToResource.locked(
65 [id, &resourcePtr, &newResource](auto& idToResource)
66 {
67 auto findResult = idToResource.find(id);
68 if(findResult == idToResource.end())
69 {
70 resourcePtr = newResource.get();
71 idToResource[id] = std::move(newResource);
72 }
73 else
74 {
75 PH_LOG(CookedResourceCollectionBase, Note,
76 "overwriting existing cooked resource (id: {})", id);
77
78 // Clear the content of existing resource while keeping its pointer valid
79 resourcePtr = findResult->second.get();
80 *resourcePtr = std::move(*newResource);
81 }
82 });
83
84 return resourcePtr;
85 }
86
87 template<typename CookedType, typename... DeducedArgs>
88 static const CookedType* getCookedResourceByID(
89 const TSynchronized<TSdlResourceIdMap<CookedType>>& syncedIdToResource,
90 const SdlResourceId id)
91 {
92 const CookedType* resourcePtr = nullptr;
93 syncedIdToResource.constLocked(
94 [id, &resourcePtr](const auto& idToResource)
95 {
96 auto findResult = idToResource.find(id);
97 if(findResult != idToResource.end())
98 {
99 resourcePtr = findResult->second.get();
100 }
101 });
102
103 return resourcePtr;
104 }
105};
106
107}// end namespace ph
Provides thread-safe cooked data creation and storage. All methods are thread-safe to call,...
Definition CookedResourceCollectionBase.h:25
virtual ~CookedResourceCollectionBase()=default
virtual std::string getStats() const =0
static const CookedType * getCookedResourceByID(const TSynchronized< TSdlResourceIdMap< CookedType > > &syncedIdToResource, const SdlResourceId id)
Definition CookedResourceCollectionBase.h:88
static CookedType * makeCookedResourceWithID(TSynchronized< TSdlResourceIdMap< CookedType > > &syncedIdToResource, const SdlResourceId id, DeducedArgs &&... args)
Definition CookedResourceCollectionBase.h:55
static DerivedType * makeCookedResource(TSynchronized< TUniquePtrVector< BaseType > > &syncedResources, DeducedArgs &&... args)
Definition CookedResourceCollectionBase.h:37
std::unordered_map< SdlResourceId, std::unique_ptr< CookedType > > TSdlResourceIdMap
Definition CookedResourceCollectionBase.h:33
Marks the derived class as non-copyable and non-movable.
Definition INoCopyAndMove.h:17
Definition TUniquePtrVector.h:20
The root for all renderer implementations.
Definition EEngineProject.h:6
std::uint64_t SdlResourceId
Definition SdlResourceId.h:8
PH_DEFINE_EXTERNAL_LOG_GROUP(ApiDatabase, Core)