Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlMethod.ipp
Go to the documentation of this file.
1#pragma once
2
9#include "SDL/sdl_traits.h"
10#include "SDL/sdl_helpers.h"
11
12#include <Common/assertion.h>
13
14#include <utility>
15
16namespace ph
17{
18
19template<typename MethodStruct, typename TargetType>
21 SdlFunction(std::move(name))
22{}
23
24template<typename MethodStruct, typename TargetType>
26 ISdlResource* resource,
27 SdlInputClauses& clauses,
28 const SdlInputContext& ctx) const
29{
31
32 if(!resource)
33 {
34 throw_formatted<SdlLoadError>(
35 "cannot call SDL method without target resource ({})",
36 sdl::gen_pretty_name(TargetType::getSdlClass()));
37 }
38
39 auto const targetRes = dynamic_cast<TargetType*>(resource);
40 if(!targetRes)
41 {
42 throw_formatted<SdlLoadError>(
43 "incompatible target resource, given {}, expected {}",
45 sdl::gen_pretty_name(TargetType::getSdlClass()));
46 }
47
48 PH_ASSERT(targetRes);
49 callMethod(*targetRes, clauses, ctx);
50}
51
52template<typename MethodStruct, typename TargetType>
54 TargetType& targetType,
55 SdlInputClauses& clauses,
56 const SdlInputContext& ctx) const
57{
58 static_assert(!std::is_abstract_v<MethodStruct> && std::is_default_constructible_v<MethodStruct>,
59 "MethodStruct must be non-abstract and default-constructible.");
60
61 static_assert(std::is_invocable_v<MethodStruct, TargetType&>,
62 "MethodStruct must contain an operator() that can take a TargetType instance.");
63
64 MethodStruct methodStructObj{};
65 loadParameters(
66 methodStructObj,
67 clauses,
68 ctx);
69
70 methodStructObj(targetType);
71}
72
73template<typename MethodStruct, typename TargetType>
75 MethodStruct& parameterStruct,
76 SdlInputClauses& clauses,
77 const SdlInputContext& ctx) const
78{
80 parameterStruct,
81 m_fields,
82 clauses,
83 ctx,
84 [](std::string noticeMsg, EFieldImportance importance)
85 {
86 if(importance == EFieldImportance::Optional || importance == EFieldImportance::NiceToHave)
87 {
88 PH_LOG_STRING(SdlFunction, Note, noticeMsg);
89 }
90 else
91 {
92 PH_LOG_STRING(SdlFunction, Warning, noticeMsg);
93 }
94 });
95}
96
97template<typename MethodStruct, typename TargetType>
99{
100 return m_fields.numFields();
101}
102
103template<typename MethodStruct, typename TargetType>
104inline const SdlField* TSdlMethod<MethodStruct, TargetType>::getParam(const std::size_t index) const
105{
106 return m_fields.getField(index);
107}
108
109template<typename MethodStruct, typename TargetType>
110template<typename T>
112 -> TSdlMethod&
113{
114 // More restrictions on the type of T may be imposed by FieldSet
115 static_assert(std::is_base_of_v<SdlField, T>,
116 "T is not a SdlField thus cannot be added.");
117
118 m_fields.addField(std::move(sdlField));
119
120 return *this;
121}
122
123template<typename MethodStruct, typename TargetType>
124inline auto TSdlMethod<MethodStruct, TargetType>::description(std::string descriptionStr)
125 -> TSdlMethod&
126{
127 setDescription(std::move(descriptionStr));
128 return *this;
129}
130
131}// end namespace ph
Interface for all SDL resource.
Definition ISdlResource.h:22
virtual const SdlClass * getDynamicSdlClass() const
Get runtime SDL class of the resource.
Definition ISdlResource.h:54
Definition SdlField.h:19
Definition SdlFunction.h:18
Container for input clauses.
Definition SdlInputClauses.h:18
Data that SDL input process can rely on.
Definition SdlInputContext.h:19
SDL binding type for a canonical SDL method.
Definition TSdlMethod.h:26
void call(ISdlResource *resource, SdlInputClauses &clauses, const SdlInputContext &ctx) const override
Definition TSdlMethod.ipp:25
const SdlField * getParam(std::size_t index) const override
Definition TSdlMethod.ipp:104
std::size_t numParams() const override
Definition TSdlMethod.ipp:98
void callMethod(TargetType &targetType, SdlInputClauses &clauses, const SdlInputContext &ctx) const
Definition TSdlMethod.ipp:53
auto description(std::string descriptionStr) -> TSdlMethod &
Definition TSdlMethod.ipp:124
TSdlMethod(std::string name)
Definition TSdlMethod.ipp:20
void loadParameters(MethodStruct &parameterStruct, SdlInputClauses &clauses, const SdlInputContext &ctx) const
Definition TSdlMethod.ipp:74
TSdlMethod & addParam(T sdlField)
Whether T is a well-defined SDL class.
Definition sdl_traits.h:29
void load_fields_from_sdl(Owner &owner, FieldSet &fieldSet, SdlInputClauses &clauses, const SdlInputContext &ctx, NoticeReceiver noticeReceiver=NoOpNoticeReceiver())
Definition field_set_op.ipp:21
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
EFieldImportance
Definition EFieldImportance.h:7
Definition TAABB2D.h:96
Low-level helpers for SDL. Helpers are in an additional sdl namespace.