Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlResourceIdentifier.h
Go to the documentation of this file.
1#pragma once
2
5#include "SDL/sdl_helpers.h"
9
10#include <Common/assertion.h>
11
12#include <string>
13
14namespace ph
15{
16
26template<typename Owner, bool IS_SRI = true, typename SdlValueType = TSdlValue<ResourceIdentifier, Owner>>
27class TSdlResourceIdentifier : public SdlValueType
28{
29 static_assert(std::is_base_of_v<TSdlAbstractValue<ResourceIdentifier, Owner>, SdlValueType>,
30 "SdlValueType should be a subclass of TSdlAbstractValue.");
31
32public:
33 template<typename ValueType>
34 inline TSdlResourceIdentifier(std::string valueName, ValueType Owner::* const valuePtr) :
35 SdlValueType("PRI", std::move(valueName), valuePtr)
36 {}
37
38 inline std::string valueAsString(const ResourceIdentifier& pri) const override
39 {
40 return pri.toString();
41 }
42
43 inline SdlNativeData ownedNativeData(Owner& owner) const override
44 {
45 ResourceIdentifier* const identifier = this->getValue(owner);
48 }
49
50protected:
51 inline void loadFromSdl(
52 Owner& owner,
53 const SdlInputClause& clause,
54 const SdlInputContext& ctx) const override
55 {
56 if constexpr(IS_SRI)
57 {
58 SdlResourceLocator resolver(ctx);
59
60 ResourceIdentifier sri(clause.value);
61 if(sri.resolve(resolver))
62 {
63 this->setValue(owner, sri);
64 }
65 else
66 {
67 // Expected to be a valid SRI. It is an error if it is not a SRI (or cannot be resolved).
68 throw_formatted<SdlLoadError>(
69 "failed loading SRI -> cannot resolve {}", sri.getIdentifier());
70 }
71 }
72 else
73 {
74 this->setValue(
75 owner,
77 }
78 }
79
80 inline void saveToSdl(
81 const Owner& owner,
82 SdlOutputClause& out_clause,
83 const SdlOutputContext& ctx) const override
84 {
85 const ResourceIdentifier* const pri = this->getConstValue(owner);
86 if(!pri)
87 {
88 return;
89 }
90
91 if constexpr(IS_SRI)
92 {
93 // Store the original identifier (the unresolved one)
94 if(pri->hasIdentifier())
95 {
96 out_clause.value = pri->getIdentifier();
97 }
98 // Try to work out a SRI from path
99 else
100 {
101 const Path& path = pri->getPath();
102 if(path.isEmpty())
103 {
104 throw SdlSaveError(
105 "failed saving SRI -> no valid information provided");
106 }
107
109 }
110 }
111 else
112 {
113 // Store the original identifier (the unresolved one)
114 out_clause.value = pri->getIdentifier();
115 }
116 }
117};
118
119}// end namespace ph
General path representation. Does not check whether the target actually exists (e....
Definition Path.h:21
bool isEmpty() const
Definition Path.h:165
An general identifier that points to some resource. This is the most general form of a resource ident...
Definition ResourceIdentifier.h:20
const std::string & getIdentifier() const
Definition ResourceIdentifier.cpp:55
Path getPath() const
Definition ResourceIdentifier.cpp:60
std::string toString() const
Definition ResourceIdentifier.cpp:79
bool resolve(IResourceIdentifierResolver &resolver)
Work out the resource the identifier points to.
Definition ResourceIdentifier.cpp:19
bool hasIdentifier() const
Whether there is an identifier. An identifier may not exist in the following situations:
Definition ResourceIdentifier.cpp:50
Carries SDL representation of various data during the input process. Helps to read input data such as...
Definition SdlInputClause.h:15
std::string value
Loaded stringified data of a clause. All potential SDL value prefixes or suffixes (e....
Definition SdlInputClause.h:25
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
Definition SdlNativeData.h:88
static SdlNativeData fromSingleElement(ElementType *elementPtr, ESdlDataFormat elementContainer, ESdlDataType elementType, bool canSet=false, bool canDirectAccess=false)
Creates native data for a single element pointer.
Definition SdlNativeData.ipp:86
Carries SDL representation of various data during the output process. Helps to write output data such...
Definition SdlOutputClause.h:14
std::string value
Stores stringified data of a clause. As the output clause generator knows best how its data look like...
Definition SdlOutputClause.h:29
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Definition SdlResourceLocator.h:25
ResourceIdentifier toExternalIdentifier(const Path &path) const
Get an identifier representing the path.
Definition SdlResourceLocator.cpp:160
Error on the SDL output process.
Definition sdl_exceptions.h:30
A field class that binds a resource identifier member. Though the member type that is binded by defau...
Definition TSdlResourceIdentifier.h:28
std::string valueAsString(const ResourceIdentifier &pri) const override
Definition TSdlResourceIdentifier.h:38
SdlNativeData ownedNativeData(Owner &owner) const override
Direct access to the field memory of an owner. Short-lived owner objects such as function parameter s...
Definition TSdlResourceIdentifier.h:43
void loadFromSdl(Owner &owner, const SdlInputClause &clause, const SdlInputContext &ctx) const override
Load SDL value to actual value and store it in the owner's field. Implementations are highly encourag...
Definition TSdlResourceIdentifier.h:51
TSdlResourceIdentifier(std::string valueName, ValueType Owner::*const valuePtr)
Definition TSdlResourceIdentifier.h:34
void saveToSdl(const Owner &owner, SdlOutputClause &out_clause, const SdlOutputContext &ctx) const override
Convert actual value back to SDL value. Saving a loaded value as SDL value should rarely fail–as load...
Definition TSdlResourceIdentifier.h:80
void setValue(Owner &owner, T value) const override
Store a value.
Definition TSdlValue.ipp:38
const T * getConstValue(const Owner &owner) const override
Get a pointer to the stored value.
Definition TSdlValue.ipp:50
T * getValue(Owner &owner) const override
Get a pointer to the stored value.
Definition TSdlValue.ipp:44
The root for all renderer implementations.
Definition EEngineProject.h:6
Definition TAABB2D.h:96
Low-level helpers for SDL. Helpers are in an additional sdl namespace.