Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
RawResourceCollection.h
Go to the documentation of this file.
1#pragma once
2
5#include "Utility/utility.h"
6
7#include <Common/assertion.h>
8#include <Common/Container/TStdUnorderedStringMap.h>
9
10#include <unordered_map>
11#include <string>
12#include <vector>
13#include <type_traits>
14#include <cstddef>
15#include <utility>
16
17namespace ph
18{
19
21{
22public:
23 std::shared_ptr<ISdlResource> get(std::string_view resourceName) const override;
24 bool has(std::string_view resourceName) const override;
25
31 void add(
32 std::shared_ptr<ISdlResource> resource,
33 std::string_view resourceName);
34
39 std::shared_ptr<ISdlResource> remove(std::string_view resourceName);
40
49 std::string rename(std::string_view resourceName, std::string_view newResourceName);
50
55 std::string makeResourceName(std::string_view intendedName);
56
64 template<typename T>
65 std::vector<std::shared_ptr<T>> getAllOfType(
66 std::vector<std::string>* out_resourceNames = nullptr) const;
67
71 std::vector<std::shared_ptr<ISdlResource>> getAll(
72 std::vector<std::string>* out_resourceNames = nullptr) const;
73
84 template<typename T>
85 std::vector<const T*> listAllOfType(
86 std::vector<std::string>* out_resourceNames = nullptr) const;
87
91 std::vector<const ISdlResource*> listAll(
92 std::vector<std::string>* out_resourceNames = nullptr) const;
93
94private:
95 using ResourceMapType = TStdUnorderedStringMap<std::shared_ptr<ISdlResource>>;
96
97 ResourceMapType m_nameToResource;
98};
99
100inline std::shared_ptr<ISdlResource> RawResourceCollection::get(std::string_view resourceName) const
101{
102 const auto& iter = m_nameToResource.find(resourceName);
103 return iter != m_nameToResource.end() ? iter->second : nullptr;
104}
105
106inline bool RawResourceCollection::has(std::string_view resourceName) const
107{
108 // Mostly the same as `get(1)`, while saving a copy of shared pointer.
109
110 const auto& iter = m_nameToResource.find(resourceName);
111 return iter != m_nameToResource.end();
112}
113
114template<typename T>
115inline std::vector<std::shared_ptr<T>> RawResourceCollection::getAllOfType(
116 std::vector<std::string>* const out_resourceNames) const
117{
118 static_assert(std::is_base_of_v<ISdlResource, T>,
119 "T is not a SDL resource.");
120
121 std::vector<std::shared_ptr<T>> resources;
122 for(const auto& [name, resource] : m_nameToResource)
123 {
124 std::shared_ptr<T> castedResource = std::dynamic_pointer_cast<T>(resource);
125 if(castedResource)
126 {
127 resources.push_back(std::move(castedResource));
128
129 if(out_resourceNames)
130 {
131 out_resourceNames->push_back(name);
132 }
133 }
134 }
135 return resources;
136}
137
138template<typename T>
139inline std::vector<const T*> RawResourceCollection::listAllOfType(
140 std::vector<std::string>* const out_resourceNames) const
141{
142 static_assert(std::is_base_of_v<ISdlResource, T>,
143 "T is not a SDL resource.");
144
145 std::vector<const T*> resources;
146 for(const auto& [name, resource] : m_nameToResource)
147 {
148 T* castedResource = dynamic_cast<T*>(resource.get());
149 if(castedResource)
150 {
151 resources.push_back(castedResource);
152
153 if(out_resourceNames)
154 {
155 out_resourceNames->push_back(name);
156 }
157 }
158 }
159 return resources;
160}
161
162}// end namespace ph
View for a group of SDL references.
Definition ISdlReferenceGroup.h:18
Definition RawResourceCollection.h:21
std::vector< std::shared_ptr< ISdlResource > > getAll(std::vector< std::string > *out_resourceNames=nullptr) const
Get a list of all resources in the scene. See getAllOfType() for details on the method.
Definition RawResourceCollection.cpp:85
std::string rename(std::string_view resourceName, std::string_view newResourceName)
Rename a resource. The new name must be unique within this collection. If newResourceName is not a un...
Definition RawResourceCollection.cpp:47
std::vector< const T * > listAllOfType(std::vector< std::string > *out_resourceNames=nullptr) const
Get a list of all resources of type T. Does not involve any change in ownership. Useful for operation...
Definition RawResourceCollection.h:139
std::shared_ptr< ISdlResource > get(std::string_view resourceName) const override
Get a resource reference.
Definition RawResourceCollection.h:100
void add(std::shared_ptr< ISdlResource > resource, std::string_view resourceName)
Add a resource.
Definition RawResourceCollection.cpp:11
std::string makeResourceName(std::string_view intendedName)
Create a resource name that is unique within this collection.
Definition RawResourceCollection.cpp:63
std::vector< const ISdlResource * > listAll(std::vector< std::string > *out_resourceNames=nullptr) const
Get a list of all resources in the scene. See listAllOfType() for details on the method.
Definition RawResourceCollection.cpp:91
std::vector< std::shared_ptr< T > > getAllOfType(std::vector< std::string > *out_resourceNames=nullptr) const
Get all resources of type T. Different to its list<XXX>() variants, this method returns actual resour...
Definition RawResourceCollection.h:115
bool has(std::string_view resourceName) const override
Check the existence of a resource reference.
Definition RawResourceCollection.h:106
std::shared_ptr< ISdlResource > remove(std::string_view resourceName)
Remove a resource.
Definition RawResourceCollection.cpp:34
The root for all renderer implementations.
Definition EEngineProject.h:6