Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlOwnedField.ipp
Go to the documentation of this file.
1#pragma once
2
7#include "SDL/sdl_helpers.h"
10#include "Utility/traits.h"
11#include "SDL/TSdlAnyInstance.h"
12
13#include <Common/assertion.h>
14
15#include <utility>
16
17namespace ph
18{
19
20template<typename Owner>
22 std::string typeName,
23 std::string valueName)
24
25 : SdlField(
26 std::move(typeName),
27 std::move(valueName))
28{}
29
30template<typename Owner>
32 std::string typeName,
33 std::string valueName,
34 std::string typeSignature)
35
36 : SdlField(
37 std::move(typeName),
38 std::move(valueName),
39 std::move(typeSignature))
40{}
41
42template<typename Owner>
44{
45 try
46 {
47 Owner* const owner = instance.get<Owner>();
48 if(!owner)
49 {
50 // Cannot get native data
51 return {};
52 }
53
54 return ownedNativeData(*owner);
55 }
56 catch(const SdlException& /* e */)
57 {
58 // Cannot get native data
59 return {};
60 }
61}
63template<typename Owner>
65 Owner& owner,
66 const SdlInputClause& clause,
67 const SdlInputContext& ctx) const
68{
69 try
70 {
71 loadFromSdl(owner, clause, ctx);
72 }
73 catch(const SdlException& e)
74 {
75 if(isFallbackEnabled())
76 {
77 ownedValueToDefault(owner);
78
79 // Always log for recovered failed loading attempt
80 // (as the user provided a SDL value string for the field,
81 // a successful parse was expected)
82 //
83 PH_LOG(SdlField, Warning, "load error from {} -> {}; value defaults to {}",
85 e.whatStr(),
86 valueToString(owner));
87 }
88 else
89 {
90 // Let caller handle the error if fallback is disabled
91 //
92 throw SdlLoadError(
93 "load error from " + sdl::gen_pretty_name(ctx.getSrcClass(), this) +
94 " -> " + e.whatStr() + "; " +
95 "value left uninitialized");
96 }
97 }
98}
99
100template<typename Owner>
102 const Owner& owner,
103 SdlOutputClause& out_clause,
104 const SdlOutputContext& ctx) const
105{
106 try
107 {
108 saveToSdl(owner, out_clause, ctx);
109 }
110 catch(const SdlException& e)
111 {
112 // Provide more information and let caller handle the error
113 //
114 throw SdlSaveError(
115 "save error from " + sdl::gen_pretty_name(ctx.getSrcClass(), this) +
116 " -> " + e.whatStr());
117 }
118}
119
120}// end namespace ph
General exception thrown on error related to SDL.
Definition sdl_exceptions.h:13
Definition SdlField.h:19
const SdlClass * getSrcClass() const
The SDL class that is involved in the I/O process.
Definition SdlIOContext.h:90
Carries SDL representation of various data during the input process. Helps to read input data such as...
Definition SdlInputClause.h:15
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
Carries SDL representation of various data during the output process. Helps to write output data such...
Definition SdlOutputClause.h:14
Data that SDL output process can rely on.
Definition SdlOutputContext.h:19
Error on the SDL output process.
Definition sdl_exceptions.h:30
References a SDL object. This is a lightweight utility for referencing SDL objects....
Definition TSdlAnyInstance.h:20
auto * get() const
Definition TSdlAnyInstance.ipp:82
void toSdl(const Owner &owner, SdlOutputClause &out_clause, const SdlOutputContext &ctx) const
Definition TSdlOwnedField.ipp:101
void fromSdl(Owner &owner, const SdlInputClause &clause, const SdlInputContext &ctx) const
Acquire value and store in the owner's field. The loading process will follow a series of preset poli...
Definition TSdlOwnedField.ipp:64
TSdlOwnedField(std::string typeName, std::string valueName)
Definition TSdlOwnedField.ipp:21
SdlNativeData nativeData(SdlNonConstInstance instance) const override
Direct access to the field memory of a SDL instance. Note that this field may not necessarily be boun...
Definition TSdlOwnedField.ipp:43
std::string gen_pretty_name(const SdlClass *const clazz)
Generate a human-readable name for the SDL types. These helpers allow input types to be null.
Definition sdl_helpers.cpp:24
The root for all renderer implementations.
Definition EEngineProject.h:6
Definition TAABB2D.h:96
Low-level helpers for SDL. Helpers are in an additional sdl namespace.