10#include <Common/assertion.h>
11#include <Common/Utility/string_utils.h>
18template<
typename T,
typename Owner>
20 std::string valueName,
21 std::shared_ptr<T> Owner::* const valuePtr) :
24 std::string(sdl::category_to_string(sdl::category_of<T>())),
25 std::move(valueName)),
29 static_assert(std::is_base_of_v<ISdlResource, T>,
30 "T must be a SDL resource (derive from ISdlResource).");
32 PH_ASSERT(m_valuePtr);
35template<
typename T,
typename Owner>
39 setValueRef(owner,
nullptr);
42template<
typename T,
typename Owner>
47 std::string(getValueRef(owner) ?
"valid" :
"empty") +
"]";
50template<
typename T,
typename Owner>
53 std::vector<const ISdlResource*>& out_resources)
const
55 const T*
const storedResource = getValueRef(owner).get();
56 if(storedResource !=
nullptr)
58 out_resources.push_back(storedResource);
62template<
typename T,
typename Owner>
65 std::shared_ptr<T>& valueRef = owner.*m_valuePtr;
66 T*
const originalDataPtr = valueRef.
get();
73 return originalDataPtr
83template<
typename T,
typename Owner>
86 owner.*m_valuePtr = std::move(value);
89template<
typename T,
typename Owner>
92 return owner.*m_valuePtr;
95template<
typename T,
typename Owner>
103 setValueRef(owner, loadReference(clause, ctx));
107 throw_formatted<SdlLoadError>(
108 "on parsing reference {} -> {}", valueToString(owner), e.whatStr());
112template<
typename T,
typename Owner>
118 const auto& resource = getValueRef(owner);
128 if(resourceName.empty())
131 "resource name unavailable");
135 if(string_utils::has_any_of(resourceName, string_utils::get_whitespaces()))
137 out_clause.
value =
'"';
138 out_clause.
value += resourceName;
139 out_clause.
value +=
'"';
143 out_clause.
value = resourceName;
148 throw_formatted<SdlSaveError>(
149 "on saving resource reference {} -> {}", valueToString(owner), e.whatStr());
153template<
typename T,
typename Owner>
154template<
typename ResourceType>
162 "no target reference group specified");
168 "bad reference type (only persistent target is supported)");
171 return loadReference(clause.
value, ctx);
174template<
typename T,
typename Owner>
175template<
typename ResourceType>
177 std::string_view referenceName,
180 if(referenceName.empty())
183 "reference name cannot be empty");
190 throw_formatted<SdlLoadError>(
191 "cannot find resource referenced by <{}>", referenceName);
197template<
typename T,
typename Owner>
201 this->setImportance(importance);
205template<
typename T,
typename Owner>
209 this->setDescription(std::move(descriptionStr));
213template<
typename T,
typename Owner>
220template<
typename T,
typename Owner>
227template<
typename T,
typename Owner>
std::shared_ptr< T > getTyped(std::string_view resourceName) const
Get a resource reference of type T with name resourceName.
Definition ISdlReferenceGroup.h:48
General exception thrown on error related to SDL.
Definition sdl_exceptions.h:13
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
const ISdlReferenceGroup * getSrcResources() const
Definition SdlInputContext.h:93
Error on the SDL input process.
Definition sdl_exceptions.h:22
Definition SdlNativeData.h:88
void setDirectAccessor(AnyNonConstPtr accessor)
Definition SdlNativeData.ipp:267
static auto permissiveElementGetter(ElementType *elementPtr) -> SdlGetterVariant
Given a valid target element, get its value in a permissive way (with auto conversions).
Definition SdlNativeData.ipp:312
ESdlDataType elementType
Hint for the type of elements.
Definition SdlNativeData.h:113
std::optional< T > get(std::size_t elementIdx) const
Definition SdlNativeData.ipp:182
ESdlDataFormat elementContainer
Hint for the type that encapsulates elements.
Definition SdlNativeData.h:109
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
bool isEmpty
If the clause carries no data and does not need to be written.
Definition SdlOutputClause.h:41
ESdlClauseValue valueType
Type of the carried value. For identifying the type of the value string only. Does not check whether ...
Definition SdlOutputClause.h:37
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
std::string_view getResourceName(const ISdlResource *resource) const
Definition SdlOutputContext.cpp:9
Error on the SDL output process.
Definition sdl_exceptions.h:30
Definition SdlNativeData.h:24
References a SDL object. This is a lightweight utility for referencing SDL objects....
Definition TSdlAnyInstance.h:20
Abstraction for a value that is owned by some owner type. Governs how a field should be initialized o...
Definition TSdlOwnedField.h:15
A value that points to a SDL resource.
Definition TSdlReference.h:21
TSdlReference & optional()
Definition TSdlReference.ipp:214
void ownedValueToDefault(Owner &owner) const override
By default, default value of a SDL reference is empty.
Definition TSdlReference.ipp:36
static std::shared_ptr< ResourceType > loadReference(const SdlInputClause &clause, const SdlInputContext &ctx)
Definition TSdlReference.ipp:155
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 TSdlReference.ipp:96
TSdlReference & required()
Definition TSdlReference.ipp:228
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 TSdlReference.ipp:63
std::string valueToString(const Owner &owner) const override
Convert the value of the field to human-readable string.
Definition TSdlReference.ipp:43
const std::shared_ptr< T > & getValueRef(const Owner &owner) const
Definition TSdlReference.ipp:90
TSdlReference(std::string valueName, std::shared_ptr< T > Owner::*valuePtr)
Definition TSdlReference.ipp:19
TSdlReference & description(std::string descriptionStr)
Definition TSdlReference.ipp:206
TSdlReference & niceToHave()
Definition TSdlReference.ipp:221
TSdlReference & withImportance(EFieldImportance importance)
Definition TSdlReference.ipp:198
void ownedResources(const Owner &owner, std::vector< const ISdlResource * > &out_resources) const override
Get all SDL resources associated by owner.
Definition TSdlReference.ipp:51
void setValueRef(Owner &owner, std::shared_ptr< T > value) const
Definition TSdlReference.ipp:84
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 TSdlReference.ipp:113
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
constexpr ESdlDataType resource_type_of()
Definition sdl_helpers.ipp:492
The root for all renderer implementations.
Definition EEngineProject.h:6
TAnyPtr< false > AnyNonConstPtr
A type-safe, lightweight wrapper for any non-const raw pointer type.
Definition TAnyPtr.h:47
EFieldImportance
Definition EFieldImportance.h:7
@ PersistentTargetName
Definition sdl_fwd.h:48
Low-level helpers for SDL. Helpers are in an additional sdl namespace.