Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlRealArray.h
Go to the documentation of this file.
1#pragma once
2
4#include "SDL/sdl_helpers.h"
7#include "DataIO/io_utils.h"
8
9#include <Common/assertion.h>
10#include <Common/primitive_type.h>
11#include <Common/io_exceptions.h>
12
13#include <type_traits>
14#include <string>
15#include <vector>
16#include <utility>
17
18namespace ph
19{
20
21template<typename Owner, typename Element = real, typename SdlValueType = TSdlValue<std::vector<Element>, Owner>>
22class TSdlRealArray : public SdlValueType
23{
24 static_assert(std::is_base_of_v<TSdlAbstractValue<std::vector<Element>, Owner>, SdlValueType>,
25 "SdlValueType should be a subclass of TSdlAbstractValue.");
26
27public:
28 template<typename ValueType>
29 inline TSdlRealArray(std::string valueName, ValueType Owner::* const valuePtr) :
30 SdlValueType("real-array", std::move(valueName), valuePtr)
31 {}
32
33 inline std::string valueAsString(const std::vector<Element>& realArray) const override
34 {
35 return "[" + std::to_string(realArray.size()) + " real values...]";
36 }
37
38 inline SdlNativeData ownedNativeData(Owner& owner) const override
39 {
40 std::vector<Element>* const vec = this->getValue(owner);
41
42 SdlNativeData data;
43 if(vec)
44 {
45 Element* const vecData = vec->data();
46 data = SdlNativeData(
47 [vecData](std::size_t elementIdx) -> SdlGetterVariant
48 {
49 return SdlNativeData::permissiveElementGetter(&(vecData[elementIdx]));
50 },
51 [vecData](std::size_t elementIdx, SdlSetterVariant input) -> bool
52 {
53 return SdlNativeData::permissiveElementSetter(input, &(vecData[elementIdx]));
54 },
55 AnyNonConstPtr(vec));
56
57 data.numElements = vec->size();
58 }
61 return data;
62 }
63
64protected:
65 inline void loadFromSdl(
66 Owner& owner,
67 const SdlInputClause& clause,
68 const SdlInputContext& ctx) const override
69 {
70 if(clause.isResourceIdentifier())
71 {
72 try
73 {
74 const std::string loadedSdlValue = io_utils::load_text(
75 SdlResourceLocator(ctx).toPath(clause.value));
76 this->setValue(owner, sdl::load_number_array<Element>(loadedSdlValue));
77 }
78 catch(const FileIOError& e)
79 {
80 throw SdlLoadError("on loading real array -> " + e.whatStr());
81 }
82 }
83 else
84 {
86 }
87 }
88
90 const Owner& owner,
91 SdlOutputClause& out_clause,
92 const SdlOutputContext& ctx) const override
93 {
94 // TODO: optionally as file
95
96 if(const std::vector<Element>* const numberArr = this->getConstValue(owner); numberArr)
97 {
98 sdl::save_number_array<Element>(*numberArr, out_clause.value);
99 }
100 }
101};
102
103}// 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
bool isResourceIdentifier() const
Helper to check if the carried value is a SDL resource identifier (SRI).
Definition SdlInputClause.cpp:7
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
Error on the SDL input process.
Definition sdl_exceptions.h:22
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
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
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Definition SdlResourceLocator.h:25
Definition SdlNativeData.h:24
Definition TSdlRealArray.h:23
TSdlRealArray(std::string valueName, ValueType Owner::*const valuePtr)
Definition TSdlRealArray.h:29
std::string valueAsString(const std::vector< Element > &realArray) const override
Definition TSdlRealArray.h:33
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 TSdlRealArray.h:65
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 TSdlRealArray.h:38
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 TSdlRealArray.h:89
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
Miscellaneous file input & output utilities.
std::string load_text(const Path &filePath)
Read the whole file as a formatted string.
Definition io_utils.cpp:242
void save_number_array(TSpanView< NumberType > values, std::string &out_str)
Definition sdl_helpers.ipp:368
std::vector< NumberType > load_number_array(std::string_view sdlNumberArrayStr)
Definition sdl_helpers.ipp:175
constexpr ESdlDataType float_type_of()
Definition sdl_helpers.ipp:464
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.