Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlOwnerStruct.ipp
Go to the documentation of this file.
1#pragma once
2
8
9#include <Common/assertion.h>
10
11#include <utility>
12#include <type_traits>
13#include <memory>
14
15namespace ph
16{
17
18template<typename StructType>
20
21 : SdlStruct(std::move(name))
22
23 , m_fields()
24{}
25
26template<typename StructType>
29 SdlInputClauses& clauses,
30 const SdlInputContext& ctx) const
31{
32 constexpr auto noticeReceiver = [](std::string noticeMsg, EFieldImportance importance)
33 {
34 if(importance == EFieldImportance::Optional || importance == EFieldImportance::NiceToHave)
35 {
36 PH_LOG_STRING(SdlStruct, Note, noticeMsg);
37 }
38 else
39 {
40 PH_LOG_STRING(SdlStruct, Warning, noticeMsg);
41 }
42 };
43
44 PH_ASSERT(obj);
45
46 // Redundant clauses are not allowed. If this method is called, that means the object is
47 // initialized with clauses dedicated to it.
49 *(obj.get<StructType>()),
50 m_fields,
51 clauses,
52 ctx,
53 noticeReceiver);
54}
55
56template<typename StructType>
58{
59 PH_ASSERT(obj);
60
61 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
62 {
63 const auto& field = m_fields[fieldIdx];
64
65 // Set field to default value regardless of its importance (field importance is for import/export)
66 field.ownedValueToDefault(*(obj.get<StructType>()));
67 }
68}
69
70template<typename StructType>
72 AnyConstPtr obj,
73 SdlOutputClauses& clauses,
74 const SdlOutputContext& ctx) const
75{
76 PH_ASSERT(obj);
77
78 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
79 {
80 const TSdlOwnedField<StructType>& field = m_fields[fieldIdx];
81
82 SdlOutputClause& clause = clauses.createClause();
83 sdl::save_field_id(&field, clause);
84 field.toSdl(*(obj.get<StructType>()), clause, ctx);
85 }
86}
87
88
89template<typename StructType>
91 AnyConstPtr obj,
92 std::vector<const ISdlResource*>& out_resources) const
93{
94 PH_ASSERT(obj);
95
96 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
97 {
98 m_fields[fieldIdx].ownedResources(*(obj.get<StructType>()), out_resources);
99 }
100}
101
102template<typename StructType>
103template<typename T>
106{
107 // More restrictions on the type of T may be imposed by FieldSet
108 static_assert(std::is_base_of_v<SdlField, T>,
109 "T is not a SdlField thus cannot be added.");
110
111 m_fields.addField(std::move(sdlField));
112
113 return *this;
114}
115
116template<typename StructType>
117template<typename StructObjType>
118inline auto TSdlOwnerStruct<StructType>::addStruct(StructObjType StructType::* const structObjPtr)
120{
121 // More restrictions on StructObjType may be imposed by FieldSet
122 static_assert(std::is_base_of_v<SdlStruct, StructObjType>,
123 "StructObjType is not a SdlStruct thus cannot be added.");
124
125 PH_ASSERT(structObjPtr);
126
127 m_fields.addFields(SdlStructFieldStump().genFieldSet(structObjPtr));
128
129 return *this;
130}
131
132template<typename StructType>
133template<typename StructObjType>
135 StructObjType StructType::* const structObjPtr,
136 const SdlStructFieldStump& structFieldStump)
137
139{
140 // More restrictions on StructObjType may be imposed by FieldSet
141 static_assert(std::is_base_of_v<SdlStruct, StructObjType>,
142 "StructObjType is not a SdlStruct thus cannot be added.");
143
144 PH_ASSERT(structObjPtr);
145
146 m_fields.addFields(structFieldStump.genFieldSet(structObjPtr));
147
148 return *this;
149}
150
151template<typename StructType>
152inline void TSdlOwnerStruct<StructType>::initDefaultStruct(StructType& structObj) const
153{
154 setFieldsToDefaults(structObj);
155}
156
157template<typename StructType>
159{
160 return m_fields.numFields();
161}
162
163template<typename StructType>
164inline const SdlField* TSdlOwnerStruct<StructType>::getField(const std::size_t index) const
165{
166 return m_fields.getField(index);
167}
168
169template<typename StructType>
171 -> const TSdlBruteForceFieldSet<TSdlOwnedField<StructType>>&
172{
173 return m_fields;
174}
175
176template<typename StructType>
177inline auto TSdlOwnerStruct<StructType>::description(std::string descriptionStr)
179{
180 setDescription(std::move(descriptionStr));
181 return *this;
182}
183
184template<typename StructType>
185inline void TSdlOwnerStruct<StructType>::setFieldsToDefaults(StructType& structObj) const
186{
187 for(std::size_t fieldIdx = 0; fieldIdx < m_fields.numFields(); ++fieldIdx)
188 {
189 const auto& field = m_fields[fieldIdx];
190
191 // Set field to default value regardless of its importance (field importance is for import/export)
192 field.ownedValueToDefault(structObj);
193 }
194}
195
196}// end namespace ph
Definition SdlField.h:19
Container for input clauses.
Definition SdlInputClauses.h:18
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
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
Definition SdlStruct.h:25
auto * get() const
Definition TAnyPtr.h:72
A set of fields, with basic functionalities.
Definition TSdlBruteForceFieldSet.h:32
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 typical C++ struct.
Definition TSdlOwnerStruct.h:18
void saveObject(AnyConstPtr obj, SdlOutputClauses &clauses, const SdlOutputContext &ctx) const override
Definition TSdlOwnerStruct.ipp:71
TSdlOwnerStruct & addStruct(StructObjType StructType::*structObjPtr)
void referencedResources(AnyConstPtr obj, std::vector< const ISdlResource * > &out_resources) const override
Get all SDL resources referenced by obj.
Definition TSdlOwnerStruct.ipp:90
void initObject(AnyNonConstPtr obj, SdlInputClauses &clauses, const SdlInputContext &ctx) const override
Initialize a struct object from value clauses. How the object will be initialized depends on the stru...
Definition TSdlOwnerStruct.ipp:27
std::size_t numFields() const override
Definition TSdlOwnerStruct.ipp:158
TSdlOwnerStruct & description(std::string descriptionStr)
Definition TSdlOwnerStruct.ipp:177
void initDefaultObject(AnyNonConstPtr obj) const override
Initialize a struct object to default values. Default values are defined by the struct's SDL definiti...
Definition TSdlOwnerStruct.ipp:57
auto getFields() const -> const TSdlBruteForceFieldSet< TSdlOwnedField< StructType > > &
Definition TSdlOwnerStruct.ipp:170
void initDefaultStruct(StructType &structObj) const
Initialize the struct object to default values.
Definition TSdlOwnerStruct.ipp:152
TSdlOwnerStruct(std::string name)
Definition TSdlOwnerStruct.ipp:19
const SdlField * getField(std::size_t index) const override
Definition TSdlOwnerStruct.ipp:164
TSdlOwnerStruct & addField(T sdlField)
void load_fields_from_sdl(Owner &owner, FieldSet &fieldSet, SdlInputClauses &clauses, const SdlInputContext &ctx, NoticeReceiver noticeReceiver=NoOpNoticeReceiver())
Definition field_set_op.ipp:21
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
Definition TAABB2D.h:96