Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlInteger.h
Go to the documentation of this file.
1#pragma once
2
5#include "SDL/sdl_helpers.h"
6
7#include <Common/assertion.h>
8#include <Common/primitive_type.h>
9
10#include <type_traits>
11#include <string>
12#include <utility>
13
14namespace ph
15{
16
19template<typename Owner, typename IntType = integer, typename SdlValueType = TSdlValue<IntType, Owner>>
20class TSdlInteger : public SdlValueType
21{
22 static_assert(std::is_base_of_v<TSdlAbstractValue<IntType, Owner>, SdlValueType>,
23 "SdlValueType should be a subclass of TSdlAbstractValue.");
24
25public:
26 template<typename ValueType>
27 inline TSdlInteger(std::string valueName, ValueType Owner::* const valuePtr) :
28 SdlValueType("integer", std::move(valueName), valuePtr)
29 {}
30
31 inline std::string valueAsString(const IntType& value) const override
32 {
33 return std::to_string(value);
34 }
35
36 inline SdlNativeData ownedNativeData(Owner& owner) const override
37 {
38 IntType* const intPtr = this->getValue(owner);
39 if constexpr(std::is_base_of_v<TSdlOptionalValue<IntType, Owner>, SdlValueType>)
40 {
41 auto data = SdlNativeData(
42 [&optInt = this->valueRef(owner)](std::size_t /* elementIdx */) -> SdlGetterVariant
43 {
44 return optInt
46 : std::monostate{};
47 },
48 [&optInt = this->valueRef(owner)](std::size_t /* elementIdx */, SdlSetterVariant input) -> bool
49 {
50 if(input.isEmpty())
51 {
52 optInt = std::nullopt;
53 return true;
54 }
55 else
56 {
57 optInt = IntType{};
58 return SdlNativeData::permissiveElementSetter(input, &(*optInt));
59 }
60 },
61 AnyNonConstPtr(intPtr));
62
63 data.numElements = intPtr ? 1 : 0;
64 data.elementContainer = ESdlDataFormat::Single;
65 data.elementType = sdl::int_type_of<IntType>();
66 data.isNullClearable = true;
67 return data;
68 }
69 else
70 {
73 }
74 }
75
76protected:
77 inline void loadFromSdl(
78 Owner& owner,
79 const SdlInputClause& clause,
80 const SdlInputContext& ctx) const override
81 {
82 this->setValue(owner, sdl::load_int<IntType>(clause.value));
83 }
84
85 inline void saveToSdl(
86 const Owner& owner,
87 SdlOutputClause& out_clause,
88 const SdlOutputContext& ctx) const override
89 {
90 if(const IntType* value = this->getConstValue(owner); value)
91 {
92 sdl::save_int<IntType>(*value, out_clause.value);
93 }
94 else
95 {
96 out_clause.isEmpty = true;
97 }
98 }
99};
100
103template<typename Owner, typename IntType = integer>
105
106template<typename Owner>
108
109template<typename Owner>
111
112template<typename Owner>
114
115template<typename Owner>
117
118template<typename Owner>
120
121template<typename Owner>
123
124template<typename Owner>
126
127template<typename Owner>
129
130template<typename Owner>
132
133template<typename Owner>
135
136template<typename Owner>
138
139template<typename Owner>
141
142template<typename Owner>
144
145template<typename Owner>
147
148template<typename Owner>
150
151template<typename Owner>
153
154template<typename Owner>
156
157template<typename Owner>
159
160}// 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
A field class that binds a integral member variable.
Definition TSdlInteger.h:21
TSdlInteger(std::string valueName, ValueType Owner::*const valuePtr)
Definition TSdlInteger.h:27
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 TSdlInteger.h:36
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 TSdlInteger.h:85
std::string valueAsString(const IntType &value) const override
Definition TSdlInteger.h:31
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 TSdlInteger.h:77
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
IntType load_int(std::string_view sdlIntStr)
Returns a integer number by processing its SDL representation. Supports ph::integer and all signed an...
Definition sdl_helpers.ipp:37
void save_int(IntType value, std::string &out_str)
Converts a integer number to its SDL representation. Supports ph::real, float, double,...
Definition sdl_helpers.ipp:248
constexpr ESdlDataType int_type_of()
Definition sdl_helpers.ipp:422
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.