Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlVector3.h
Go to the documentation of this file.
1#pragma once
2
5#include "Math/TVector3.h"
6#include "SDL/sdl_helpers.h"
7
8#include <Common/assertion.h>
9#include <Common/primitive_type.h>
10
11#include <type_traits>
12#include <string>
13
14namespace ph
15{
16
17template<typename Owner, typename Element = real, typename SdlValueType = TSdlValue<math::TVector3<Element>, Owner>>
18class TSdlVector3 : public SdlValueType
19{
20 static_assert(std::is_base_of_v<TSdlAbstractValue<math::TVector3<Element>, Owner>, SdlValueType>,
21 "SdlValueType should be a subclass of TSdlAbstractValue.");
22
23 static_assert(std::is_same_v<Element, real>,
24 "Currently supports only ph::real");
25
26public:
31 template<typename ValueType>
32 TSdlVector3(std::string valueName, ValueType Owner::* const valuePtr) :
33 SdlValueType("vector3", std::move(valueName), valuePtr)
34 {
35 if constexpr(std::is_same_v<SdlValueType, TSdlValue<math::TVector3<Element>, Owner>>)
36 {
37 this->defaultTo(math::TVector3<Element>(0, 0, 0));
38 }
39 }
40
41 std::string valueAsString(const math::TVector3<Element>& vec3) const override
42 {
43 return vec3.toString();
44 }
45
46 SdlNativeData ownedNativeData(Owner& owner) const override
47 {
48 math::TVector3<Element>* const vec3 = this->getValue(owner);
49 if constexpr(std::is_base_of_v<TSdlOptionalValue<math::TVector3<Element>, Owner>, SdlValueType>)
50 {
51 auto data = SdlNativeData(
52 [&optVec3 = this->valueRef(owner)](std::size_t elementIdx) -> SdlGetterVariant
53 {
54 return optVec3
55 ? SdlNativeData::permissiveElementGetter(&((*optVec3)[elementIdx]))
56 : std::monostate{};
57 },
58 [&optVec3 = this->valueRef(owner)](std::size_t elementIdx, SdlSetterVariant input) -> bool
59 {
60 if(input.isEmpty())
61 {
62 optVec3 = std::nullopt;
63 return true;
64 }
65 else
66 {
67 if(!optVec3)
68 {
69 optVec3 = math::TVector3<Element>{};
70 }
71
72 return SdlNativeData::permissiveElementSetter(input, &((*optVec3)[elementIdx]));
73 }
74 },
75 AnyNonConstPtr(vec3));
76
78 data.elementType = sdl::number_type_of<Element>();
79 data.numElements = vec3 ? 3 : 0;
80 data.tupleSize = 3;
81 data.isNullClearable = true;
82 return data;
83 }
84 else
85 {
86 SdlNativeData data;
87 if(vec3)
88 {
89 data = SdlNativeData(
90 [vec3](std::size_t elementIdx) -> SdlGetterVariant
91 {
92 return SdlNativeData::permissiveElementGetter(&((*vec3)[elementIdx]));
93 },
94 [vec3](std::size_t elementIdx, SdlSetterVariant input) -> bool
95 {
96 return SdlNativeData::permissiveElementSetter(input, &((*vec3)[elementIdx]));
97 },
98 AnyNonConstPtr(vec3));
99 }
102 data.numElements = vec3 ? 3 : 0;
103 data.tupleSize = 3;
104 return data;
105 }
106 }
107
108protected:
110 Owner& owner,
111 const SdlInputClause& clause,
112 const SdlInputContext& ctx) const override
113 {
114 this->setValue(owner, sdl::load_vector3<Element>(clause.value));
115 }
116
118 const Owner& owner,
119 SdlOutputClause& out_clause,
120 const SdlOutputContext& ctx) const override
121 {
122 if(const math::TVector3<Element>* vec3 = this->getConstValue(owner); vec3)
123 {
124 sdl::save_vector3(*vec3, out_clause.value);
125 }
126 else
127 {
128 out_clause.isEmpty = true;
129 }
130 }
131};
132
135template<typename Owner, typename Element = real>
137
138}// end namespace ph
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 auto permissiveElementSetter(SdlSetterVariant input, ElementType *out_elementPtr) -> bool
Given a valid target element, set its value in a permissive way (with auto conversions).
Definition SdlNativeData.ipp:349
std::size_t numElements
Hint for number of elements in this block of native data. For example, numElements would be 12 for an...
Definition SdlNativeData.h:99
std::size_t tupleSize
Hint for number of elements that form a natural group. For an array of 10 vec3s, tupleSize may have a...
Definition SdlNativeData.h:105
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
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
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Definition SdlNativeData.h:24
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
TSdlValue & defaultTo(T defaultValue)
Definition TSdlValue.ipp:71
T & valueRef(Owner &owner) const
Definition TSdlValue.ipp:132
Definition TSdlVector3.h:19
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 TSdlVector3.h:46
std::string valueAsString(const math::TVector3< Element > &vec3) const override
Definition TSdlVector3.h:41
TSdlVector3(std::string valueName, ValueType Owner::*const valuePtr)
Bind a Vector3 field to SDL.
Definition TSdlVector3.h:32
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 TSdlVector3.h:109
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 TSdlVector3.h:117
Represents a 3-D vector.
Definition TVector3.h:17
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
void save_vector3(const math::TVector3< Element > &value, std::string &out_str)
Definition sdl_helpers.ipp:323
constexpr ESdlDataType number_type_of()
Definition sdl_helpers.ipp:478
math::TVector3< Element > load_vector3(std::string_view sdlVec3Str)
Definition sdl_helpers.ipp:130
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
Definition TAABB2D.h:96
Low-level helpers for SDL. Helpers are in an additional sdl namespace.