Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
ISdlReferenceGroup.h
Go to the documentation of this file.
1#pragma once
2
3#include "SDL/ISdlResource.h"
4#include "SDL/sdl_helpers.h"
6#include "Utility/utility.h"
7
8#include <string_view>
9#include <memory>
10#include <type_traits>
11
12namespace ph
13{
14
18{
19public:
21 virtual ~ISdlReferenceGroup() = default;
22
27 virtual std::shared_ptr<ISdlResource> get(std::string_view resourceName) const = 0;
28
32 virtual bool has(std::string_view resourceName) const = 0;
33
38 template<typename T>
39 std::shared_ptr<T> getTyped(std::string_view resourceName) const;
40
43 template<typename T>
44 bool hasTyped(std::string_view resourceName) const;
45};
46
47template<typename T>
48inline std::shared_ptr<T> ISdlReferenceGroup::getTyped(std::string_view resourceName) const
49{
50 static_assert(std::is_base_of_v<ISdlResource, T>,
51 "T is not a SDL resource.");
52
53 std::shared_ptr<ISdlResource> rawResource = get(resourceName);
54 if(!rawResource)
55 {
56 return nullptr;
57 }
58
59 std::shared_ptr<T> castedResource = std::dynamic_pointer_cast<T>(std::move(rawResource));
60 if(!castedResource)
61 {
62 // The cast can fail if a wrong type for the resource is specified
63 // (name is correct, but with a wrong type).
64
65 throw_formatted<SdlLoadError>(
66 "resource type (category: {}) is not the requested type (category: {}, name: {})",
67 sdl::category_to_string(get(resourceName)->getDynamicCategory()),
69 }
70
71 return castedResource;
72}
73
74template<typename T>
75inline bool ISdlReferenceGroup::hasTyped(std::string_view resourceName) const
76{
77 static_assert(std::is_base_of_v<ISdlResource, T>,
78 "T is not a SDL resource.");
79
80 return has(resourceName, sdl::category_of<T>());
81}
82
83}// end namespace ph
View for a group of SDL references.
Definition ISdlReferenceGroup.h:18
bool hasTyped(std::string_view resourceName) const
Check the existence of resource of type T.
Definition ISdlReferenceGroup.h:75
std::shared_ptr< T > getTyped(std::string_view resourceName) const
Get a resource reference of type T with name resourceName.
Definition ISdlReferenceGroup.h:48
virtual ~ISdlReferenceGroup()=default
virtual std::shared_ptr< ISdlResource > get(std::string_view resourceName) const =0
Get a resource reference.
PH_DEFINE_INLINE_RULE_OF_5_MEMBERS_NO_DTOR(ISdlReferenceGroup)
virtual bool has(std::string_view resourceName) const =0
Check the existence of a resource reference.
std::string_view category_to_string(const ESdlTypeCategory category)
Definition ESdlTypeCategory.h:59
constexpr ESdlTypeCategory category_of()
Statically gets the SDL category of T.
Definition sdl_helpers.ipp:409
The root for all renderer implementations.
Definition EEngineProject.h:6
Low-level helpers for SDL. Helpers are in an additional sdl namespace.