12#include <Common/assertion.h>
20template<
typename Owner,
typename FieldSet>
23 :
SdlClass(sdl::category_of<Owner>(),
std::move(displayName))
31template<
typename Owner,
typename FieldSet>
34 if constexpr(!std::is_abstract_v<Owner> && std::is_default_constructible_v<Owner>)
36 if(!isBlueprint() && allowCreateFromClass())
38 return std::make_shared<Owner>();
48 PH_ASSERT_MSG(isBlueprint() || !allowCreateFromClass(),
49 std::format(
"Cannot create resource while the SDL definition permits it (is blueprint: {}, "
50 "allow create from class: {})", isBlueprint(), allowCreateFromClass()));
56template<
typename Owner,
typename FieldSet>
66 getBase()->initResource(resource, clauses, ctx);
69 Owner*
const ownerResource = castTo<Owner>(&resource);
70 loadFieldsFromSdl(*ownerResource, clauses, ctx);
73template<
typename Owner,
typename FieldSet>
80 getBase()->initDefaultResource(resource);
83 Owner*
const ownerResource = castTo<Owner>(&resource);
84 setFieldsToDefaults(*ownerResource);
87template<
typename Owner,
typename FieldSet>
98 getBase()->saveResource(resource, clauses, ctx);
101 const Owner*
const ownerResource = castTo<const Owner>(&resource);
102 saveFieldsToSdl(*ownerResource, clauses, ctx);
105template<
typename Owner,
typename FieldSet>
107 const std::string_view funcName,
114 for(std::size_t funcIdx = 0; funcIdx < m_functions.size(); ++funcIdx)
116 PH_ASSERT(m_functions[funcIdx]);
117 const SdlFunction& funcI = *(m_functions[funcIdx]);
118 if(funcI.
getName() == funcName)
128 func->
call(resource, clauses, ctx);
133 getBase()->call(funcName, resource, clauses, ctx);
138 "cannot find SDL function named <" + std::string(funcName) +
"> in "
139 "SDL class <" + genPrettyName() +
">");
143template<
typename Owner,
typename FieldSet>
146 std::vector<const ISdlResource*>& out_resources)
const
148 static_assert(std::is_base_of_v<ISdlResource, Owner>,
149 "Owner class must derive from ISdlResource.");
151 const Owner& owner = *(castTo<const Owner>(targetResource));
152 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
154 m_fields[fieldIdx].ownedResources(owner, out_resources);
160 PH_ASSERT(getBase());
161 getBase()->referencedResources(targetResource, out_resources);
165template<
typename Owner,
typename FieldSet>
168 return m_fields.numFields();
171template<
typename Owner,
typename FieldSet>
174 return getOwnedField(index);
177template<
typename Owner,
typename FieldSet>
180 return m_functions.size();
183template<
typename Owner,
typename FieldSet>
186 return index < m_functions.size() ? m_functions[index] :
nullptr;
189template<
typename Owner,
typename FieldSet>
192 return m_fields.getField(index);
195template<
typename Owner,
typename FieldSet>
196template<
typename SdlFieldType>
201 static_assert(std::is_base_of_v<SdlField, SdlFieldType>,
202 "Provided SdlFieldType is not a SdlField thus cannot be added.");
204 m_fields.
addField(std::move(sdlField));
209template<
typename Owner,
typename FieldSet>
210template<
typename StructType>
217template<
typename Owner,
typename FieldSet>
218template<
typename StructType>
220 StructType Owner::* const structObjPtr,
226 "SDL struct definition not found.");
228 PH_ASSERT(structObjPtr);
230 m_fields.addFields(structFieldStump.genFieldSet(structObjPtr));
235template<
typename Owner,
typename FieldSet>
241 "SDL function definition not found.");
243 m_functions.pushBack(T::getSdlFunction());
248template<
typename Owner,
typename FieldSet>
254 constexpr auto noticeReceiver = [](std::string noticeMsg,
EFieldImportance importance)
258 PH_LOG_STRING(
SdlClass, Note, noticeMsg);
262 PH_LOG_STRING(
SdlClass, Warning, noticeMsg);
290template<
typename Owner,
typename FieldSet>
293 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
295 const auto& field = m_fields[fieldIdx];
298 field.ownedValueToDefault(owner);
302template<
typename Owner,
typename FieldSet>
308 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
314 field.
toSdl(owner, clause, ctx);
318template<
typename Owner,
typename FieldSet>
322 setDescription(std::move(descriptionStr));
326template<
typename Owner,
typename FieldSet>
330 setDocName(std::move(docName));
334template<
typename Owner,
typename FieldSet>
339 static_assert(!std::is_same_v<T, Owner>,
340 "A SDL class cannot base on itself.");
345 if(getBase() && !getBase()->allowCreateFromClass())
347 allowCreateFromClass(
false);
353template<
typename Owner,
typename FieldSet>
357 setAllowCreateFromClass(allowCreateFromClass);
361template<
typename Owner,
typename FieldSet>
362template<
typename DstType,
typename SrcType>
369 catch(
const SdlException& e)
371 throw_formatted<SdlException>(
372 "input resource is not owned by this class <{}> ({})",
373 genPrettyName(), e.whatStr());
Interface for all SDL resource.
Definition ISdlResource.h:22
SdlClass & setIsBlueprint(bool isBlueprint)
Definition SdlClass.cpp:49
Definition SdlFunction.h:18
virtual void call(ISdlResource *resource, SdlInputClauses &clauses, const SdlInputContext &ctx) const =0
std::string_view getName() const
Definition SdlFunction.h:76
const SdlClass * getSrcClass() const
The SDL class that is involved in the I/O process.
Definition SdlIOContext.h:90
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
Error on the SDL input process.
Definition sdl_exceptions.h:22
Carries SDL representation of various data during the output process. Helps to write output data such...
Definition SdlOutputClause.h:14
Definition SdlOutputClauses.h:14
SdlOutputClause & createClause()
Definition SdlOutputClauses.h:34
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Definition SdlStructFieldStump.h:12
Abstraction for a value that is owned by some owner type. Governs how a field should be initialized o...
Definition TSdlOwnedField.h:15
void toSdl(const Owner &owner, SdlOutputClause &out_clause, const SdlOutputContext &ctx) const
Definition TSdlOwnedField.ipp:101
SDL binding type for a canonical SDL resource class.
Definition TSdlOwnerClass.h:23
void initDefaultResource(ISdlResource &resource) const override
Initialize a resource to default values. Default values are defined by the resource class's SDL defin...
Definition TSdlOwnerClass.ipp:74
const TSdlOwnedField< Owner > * getOwnedField(std::size_t index) const
Definition TSdlOwnerClass.ipp:190
void referencedResources(const ISdlResource *targetResource, std::vector< const ISdlResource * > &out_resources) const override
Get all SDL resources referenced by targetResource.
Definition TSdlOwnerClass.ipp:144
auto docName(std::string docName) -> TSdlOwnerClass &
Definition TSdlOwnerClass.ipp:327
std::shared_ptr< ISdlResource > createResource() const override
Definition TSdlOwnerClass.ipp:32
TSdlOwnerClass & addStruct(StructType Owner::*structObjPtr)
void saveFieldsToSdl(const Owner &owner, SdlOutputClauses &clauses, const SdlOutputContext &ctx) const
Definition TSdlOwnerClass.ipp:303
void loadFieldsFromSdl(Owner &owner, SdlInputClauses &clauses, const SdlInputContext &ctx) const
Definition TSdlOwnerClass.ipp:249
void initResource(ISdlResource &resource, SdlInputClauses &clauses, const SdlInputContext &ctx) const override
Initialize a resource from value clauses. How the resource will be initialized depends on the resourc...
Definition TSdlOwnerClass.ipp:57
TSdlOwnerClass(std::string displayName)
Definition TSdlOwnerClass.ipp:21
bool allowCreateFromClass() const
Whether a resource can be created by calling createResource(). This attribute is useful to decide whe...
Definition SdlClass.h:138
const SdlField * getField(std::size_t index) const override
Definition TSdlOwnerClass.ipp:172
void call(std::string_view funcName, ISdlResource *resource, SdlInputClauses &clauses, const SdlInputContext &ctx) const override
Definition TSdlOwnerClass.ipp:106
auto description(std::string descriptionStr) -> TSdlOwnerClass &
Definition TSdlOwnerClass.ipp:319
TSdlOwnerClass & addField(SdlFieldType sdlField)
auto baseOn() -> TSdlOwnerClass &
Set another SDL class as the base of this class.
Definition TSdlOwnerClass.ipp:336
TSdlOwnerClass & addFunction()
Adds a function that can later be called.
std::size_t numFields() const override
Definition TSdlOwnerClass.ipp:166
void saveResource(const ISdlResource &resource, SdlOutputClauses &clauses, const SdlOutputContext &ctx) const override
Definition TSdlOwnerClass.ipp:88
const SdlFunction * getFunction(std::size_t index) const override
Definition TSdlOwnerClass.ipp:184
std::size_t numFunctions() const override
Definition TSdlOwnerClass.ipp:178
Whether T is a well-defined SDL function.
Definition sdl_traits.h:42
Whether T is a well-defined SDL struct.
Definition sdl_traits.h:36
void load_fields_from_sdl(Owner &owner, FieldSet &fieldSet, SdlInputClauses &clauses, const SdlInputContext &ctx, NoticeReceiver noticeReceiver=NoOpNoticeReceiver())
Definition field_set_op.ipp:21
void load_fields_from_sdl_with_redundant_clauses(Owner &owner, FieldSet &fieldSet, SdlInputClauses &clauses, const SdlInputContext &ctx, NoticeReceiver noticeReceiver=NoOpNoticeReceiver())
Definition field_set_op.ipp:110
DstType * cast_to(SrcType *srcResource)
Cast between SDL resource types. Cast the input SDL resource instance of SrcType to an instance of Ds...
Definition sdl_helpers.ipp:549
void save_field_id(const SdlField *const sdlField, SdlOutputClause &clause)
Save the identity of the field to output clause.
Definition sdl_helpers.cpp:16
The root for all renderer implementations.
Definition EEngineProject.h:6
EFieldImportance
Definition EFieldImportance.h:7
Low-level helpers for SDL. Helpers are in an additional sdl namespace.