26#ifndef APRA_SCALAR_PROPERTY_VALUE_DEFINED
27#define APRA_SCALAR_PROPERTY_VALUE_DEFINED
39 const std::map<std::string, ScalarPropertyValue>& values,
41 std::vector<std::string>& missingRequired
43 auto it = values.find(propName);
44 if (it == values.end()) {
46 missingRequired.push_back(propName);
51 std::visit([&member, propName](
auto&& val) {
52 using V = std::decay_t<
decltype(val)>;
54 if constexpr (std::is_same_v<T, std::string>) {
55 if constexpr (std::is_same_v<V, std::string>) {
59 else if constexpr (std::is_same_v<T, bool>) {
60 if constexpr (std::is_same_v<V, bool>) {
64 else if constexpr (std::is_integral_v<T>) {
65 if constexpr (std::is_same_v<V, int64_t>) {
66 member =
static_cast<T
>(val);
69 else if constexpr (std::is_floating_point_v<T>) {
70 if constexpr (std::is_same_v<V, double>) {
71 member =
static_cast<T
>(val);
73 else if constexpr (std::is_same_v<V, int64_t>) {
74 member =
static_cast<T
>(val);
85 if constexpr (std::is_same_v<T, std::string>) {
88 else if constexpr (std::is_same_v<T, bool>) {
91 else if constexpr (std::is_integral_v<T>) {
92 return static_cast<int64_t
>(member);
94 else if constexpr (std::is_floating_point_v<T>) {
95 return static_cast<double>(member);
99 return std::string(
"");
109 return std::visit([&member](
auto&& val) ->
bool {
110 using V = std::decay_t<
decltype(val)>;
112 if constexpr (std::is_same_v<T, std::string> && std::is_same_v<V, std::string>) {
116 else if constexpr (std::is_same_v<T, bool> && std::is_same_v<V, bool>) {
120 else if constexpr (std::is_integral_v<T> && std::is_same_v<V, int64_t>) {
121 member =
static_cast<T
>(val);
124 else if constexpr (std::is_floating_point_v<T> && std::is_same_v<V, double>) {
125 member =
static_cast<T
>(val);
128 else if constexpr (std::is_floating_point_v<T> && std::is_same_v<V, int64_t>) {
129 member =
static_cast<T
>(val);
154#define APRA_PROP_MUT_Static false
155#define APRA_PROP_MUT_Dynamic true
158#define APRA_PROP_REQ_Required true
159#define APRA_PROP_REQ_Optional false
165#define APRA_PROP_DECL_MEMBER(type, name, mut, req, def, desc) \
171#define APRA_PROP_INFO(type, name, mut, req, def, desc) \
172 { #name, #type, desc, APRA_PROP_REQ_##req, APRA_PROP_MUT_##mut },
177#define APRA_PROP_APPLY(type, name, mut, req, def, desc) \
178 apra::applyProp(props.name, #name, values, APRA_PROP_REQ_##req, missingRequired);
183#define APRA_PROP_GET(type, name, mut, req, def, desc) \
184 if (propName == #name) return apra::toPropertyValue(name);
189#define APRA_PROP_SET_IMPL_false(type, name, desc) \
190 if (propName == #name) { \
191 throw std::runtime_error("Cannot modify static property '" #name "' after initialization"); \
194#define APRA_PROP_SET_IMPL_true(type, name, desc) \
195 if (propName == #name) { \
196 if (apra::applyFromVariant(name, value)) return true; \
197 throw std::runtime_error("Type mismatch for property '" #name "'"); \
201#define APRA_PROP_SET_CALL(impl, type, name, desc) impl(type, name, desc)
202#define APRA_PROP_SET_CONCAT(x, y) x ## y
203#define APRA_PROP_SET_DISPATCH(isDynamic, type, name, desc) \
204 APRA_PROP_SET_CALL(APRA_PROP_SET_CONCAT(APRA_PROP_SET_IMPL_, isDynamic), type, name, desc)
206#define APRA_PROP_SET(type, name, mut, req, def, desc) \
207 APRA_PROP_SET_DISPATCH(APRA_PROP_MUT_##mut, type, name, desc)
212#define APRA_PROP_DYN_NAME_false(name)
213#define APRA_PROP_DYN_NAME_true(name) names.push_back(#name);
216#define APRA_PROP_DYN_CALL(impl, name) impl(name)
217#define APRA_PROP_DYN_CONCAT(x, y) x ## y
218#define APRA_PROP_DYN_DISPATCH(isDynamic, name) \
219 APRA_PROP_DYN_CALL(APRA_PROP_DYN_CONCAT(APRA_PROP_DYN_NAME_, isDynamic), name)
221#define APRA_PROP_DYN_NAME(type, name, mut, req, def, desc) \
222 APRA_PROP_DYN_DISPATCH(APRA_PROP_MUT_##mut, name)
239#define DECLARE_PROPS(PROPS_MACRO) \
241 PROPS_MACRO(APRA_PROP_DECL_MEMBER) \
244 static std::vector<apra::PropertyInfo> getPropertyInfos() { \
246 PROPS_MACRO(APRA_PROP_INFO) \
251 template<typename PropsT> \
252 static void applyProperties( \
254 const std::map<std::string, apra::ScalarPropertyValue>& values, \
255 std::vector<std::string>& missingRequired \
257 PROPS_MACRO(APRA_PROP_APPLY) \
261 apra::ScalarPropertyValue getProperty(const std::string& propName) const { \
262 PROPS_MACRO(APRA_PROP_GET) \
263 throw std::runtime_error("Unknown property: " + propName); \
267 bool setProperty(const std::string& propName, const apra::ScalarPropertyValue& value) { \
268 PROPS_MACRO(APRA_PROP_SET) \
269 throw std::runtime_error("Unknown property: " + propName); \
273 static std::vector<std::string> dynamicPropertyNames() { \
274 std::vector<std::string> names; \
275 PROPS_MACRO(APRA_PROP_DYN_NAME) \
280 static bool isPropertyDynamic(const std::string& propName) { \
281 auto names = dynamicPropertyNames(); \
282 return std::find(names.begin(), names.end(), propName) != names.end(); \
Definition FrameTypeRegistrations.h:10
void applyProp(T &member, const char *propName, const std::map< std::string, ScalarPropertyValue > &values, bool isRequired, std::vector< std::string > &missingRequired)
Definition PropertyMacros.h:36
ScalarPropertyValue toPropertyValue(const T &member)
Definition PropertyMacros.h:84
bool applyFromVariant(T &member, const ScalarPropertyValue &value)
Definition PropertyMacros.h:108
std::variant< int64_t, double, bool, std::string > ScalarPropertyValue
Definition ModuleRegistry.h:30
Definition PropertyMacros.h:139
std::string name
Definition PropertyMacros.h:140
std::string description
Definition PropertyMacros.h:142
std::string type
Definition PropertyMacros.h:141
bool dynamic
Definition PropertyMacros.h:144
bool required
Definition PropertyMacros.h:143