Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlSpectrum.h
Go to the documentation of this file.
1#pragma once
2
6#include "SDL/sdl_helpers.h"
9#include "DataIO/io_utils.h"
12
13#include <Common/assertion.h>
14#include <Common/primitive_type.h>
15#include <Common/io_exceptions.h>
16
17#include <type_traits>
18#include <string>
19#include <vector>
20#include <utility>
21
22namespace ph
23{
24
25template<typename Owner, typename SdlValueType = TSdlValue<math::Spectrum, Owner>>
26class TSdlSpectrum : public SdlValueType
27{
28 static_assert(std::is_base_of_v<TSdlAbstractValue<math::Spectrum, Owner>, SdlValueType>,
29 "SdlValueType should be a subclass of TSdlAbstractValue.");
30
31public:
32 template<typename ValueType>
34 std::string valueName,
35 const math::EColorUsage usage,
36 ValueType Owner::* const valuePtr)
37
38 : SdlValueType("spectrum", std::move(valueName), valuePtr)
39
40 , m_usage(usage)
41 {}
42
43 std::string valueAsString(const math::Spectrum& spectrum) const override
44 {
45 // TODO: add type, # values?
46 return spectrum.toString();
47 }
48
49 SdlNativeData ownedNativeData(Owner& owner) const override
50 {
51 math::Spectrum* const spectrum = this->getValue(owner);
52 if constexpr(std::is_base_of_v<TSdlOptionalValue<math::Spectrum, Owner>, SdlValueType>)
53 {
54 auto data = SdlNativeData(
55 [&optSpectrum = this->valueRef(owner)](std::size_t /* elementIdx */) -> SdlGetterVariant
56 {
57 return optSpectrum
59 : std::monostate{};
60 },
61 [&optSpectrum = this->valueRef(owner)](std::size_t /* elementIdx */, SdlSetterVariant input) -> bool
62 {
63 if(input.isEmpty())
64 {
65 optSpectrum = std::nullopt;
66 return true;
67 }
68 else
69 {
70 *optSpectrum = math::Spectrum{};
71 return SdlNativeData::permissiveElementSetter(input, &(*optSpectrum));
72 }
73 },
74 AnyNonConstPtr(spectrum));
75
76 data.numElements = spectrum ? 1 : 0;
77 data.elementContainer = ESdlDataFormat::Single;
78 data.elementType = ESdlDataType::Spectrum;
79 data.isNullClearable = true;
80 return data;
81 }
82 else
83 {
85 spectrum, ESdlDataFormat::Single, ESdlDataType::Spectrum, true, true);
86 }
87 }
88
89protected:
91 Owner& owner,
92 const SdlInputClause& clause,
93 const SdlInputContext& ctx) const override
94 {
95 this->setValue(owner, sdl::load_spectrum(clause.value, clause.tag, m_usage));
96 }
97
99 const Owner& owner,
100 SdlOutputClause& out_clause,
101 const SdlOutputContext& ctx) const override
102 {
103 if(const math::Spectrum* value = this->getConstValue(owner); value)
104 {
105 sdl::save_spectrum(*value, out_clause.value, out_clause.tag);
106 }
107 else
108 {
109 out_clause.isEmpty = true;
110 }
111 }
112
113private:
114 math::EColorUsage m_usage;
115};
116
117template<typename Owner>
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
std::string tag
Definition SdlInputClause.h:27
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
std::string tag
Definition SdlOutputClause.h:31
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 TSdlSpectrum.h:27
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 TSdlSpectrum.h:98
std::string valueAsString(const math::Spectrum &spectrum) const override
Definition TSdlSpectrum.h:43
TSdlSpectrum(std::string valueName, const math::EColorUsage usage, ValueType Owner::*const valuePtr)
Definition TSdlSpectrum.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 TSdlSpectrum.h:90
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 TSdlSpectrum.h:49
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
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
Definition TTristimulusSpectrum.h:11
Miscellaneous file input & output utilities.
EColorUsage
Definition color_enums.h:140
math::Spectrum load_spectrum(std::string_view sdlSpectrumStr, std::string_view tag, math::EColorUsage usage)
Definition sdl_spectrum_io.cpp:52
void save_spectrum(const math::Spectrum &spectrum, std::string &out_sdlSpectrumStr, std::string &out_tag)
Definition sdl_spectrum_io.cpp: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.