Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlEnumField.h
Go to the documentation of this file.
1#pragma once
2
5#include "SDL/sdl_helpers.h"
7
8#include <Common/assertion.h>
9
10#include <type_traits>
11#include <string>
12#include <utility>
13
14namespace ph
15{
16
17/*
18@note The definition for SDL enum, i.e., the definition body of `PH_DEFINE_SDL_ENUM()` must
19present before the use of the enum field.
20*/
21template<typename Owner, typename EnumType, typename SdlValueType = TSdlValue<EnumType, Owner>>
22class TSdlEnumField : public SdlValueType
23{
24 static_assert(std::is_base_of_v<TSdlAbstractValue<EnumType, Owner>, SdlValueType>,
25 "SdlValueType should be a subclass of TSdlAbstractValue.");
26
27 static_assert(std::is_enum_v<EnumType>,
28 "EnumType must be a C++ enum.");
29
30public:
31 template<typename ValueType>
32 explicit TSdlEnumField(ValueType Owner::* const valuePtr)
34 TSdlEnum<EnumType>::getSdlEnum()->getName(),
35 valuePtr)
36 {}
37
38 template<typename ValueType>
39 TSdlEnumField(std::string valueName, ValueType Owner::* const valuePtr)
40 : SdlValueType(
41 "enum",
42 std::move(valueName),
43 "E/" + TSdlEnum<EnumType>::getSdlEnum()->getName(),
44 valuePtr)
45 {}
46
47 std::string valueAsString(const EnumType& value) const override
48 {
49 return std::string(TSdlEnum<EnumType>()[value]);
50 }
51
52 SdlNativeData ownedNativeData(Owner& owner) const override
53 {
54 EnumType* const enumPtr = this->getValue(owner);
55 if constexpr(std::is_base_of_v<TSdlOptionalValue<EnumType, Owner>, SdlValueType>)
56 {
57 auto data = SdlNativeData(
58 [&optEnum = this->valueRef(owner)](std::size_t /* elementIdx */) -> SdlGetterVariant
59 {
60 return optEnum
62 : std::monostate{};
63 },
64 [&optEnum = this->valueRef(owner)](std::size_t /* elementIdx */, SdlSetterVariant input) -> bool
65 {
66 if(input.isEmpty())
67 {
68 optEnum = std::nullopt;
69 return true;
70 }
71 else
72 {
73 optEnum = EnumType{};
74 return SdlNativeData::permissiveElementSetter(input, &(*optEnum));
75 }
76 },
77 AnyNonConstPtr(enumPtr));
78
79 data.numElements = enumPtr ? 1 : 0;
80 data.elementContainer = ESdlDataFormat::Single;
81 data.elementType = ESdlDataType::Enum;
82 data.isNullClearable = true;
83 return data;
84 }
85 else
86 {
88 enumPtr, ESdlDataFormat::Single, ESdlDataType::Enum, true, true);
89 }
90 }
91
92protected:
94 Owner& owner,
95 const SdlInputClause& clause,
96 const SdlInputContext& ctx) const override
97 {
98 this->setValue(owner, TSdlEnum<EnumType>()[clause.value]);
99 }
100
102 const Owner& owner,
103 SdlOutputClause& out_clause,
104 const SdlOutputContext& ctx) const override
105 {
106 if(const EnumType* enumValue = this->getConstValue(owner); enumValue)
107 {
108 out_clause.value = TSdlEnum<EnumType>()[*enumValue];
109 }
110 else
111 {
112 out_clause.isEmpty = true;
113 }
114 }
115};
116
117template<typename Owner, typename EnumType>
119
120}// 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
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
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
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
Definition TSdlEnumField.h:23
TSdlEnumField(ValueType Owner::*const valuePtr)
Definition TSdlEnumField.h:32
TSdlEnumField(std::string valueName, ValueType Owner::*const valuePtr)
Definition TSdlEnumField.h:39
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 TSdlEnumField.h:93
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 TSdlEnumField.h:101
std::string valueAsString(const EnumType &value) const override
Definition TSdlEnumField.h:47
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 TSdlEnumField.h:52
A helper for accessing SDL enum properties.
Definition sdl_fwd.h:15
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
T & valueRef(Owner &owner) const
Definition TSdlValue.ipp:132
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.