26 #include <platform/core/common/Any.hpp>
58 MetaData(std::string label_ = {},
const std::string& description_ =
"no-description")
70 static_assert(std::is_same<T, double>::value || std::is_same<T, int32_t>::value ||
71 std::is_same<T, std::string>::value || std::is_same<T, bool>::value ||
72 std::is_same<T, std::array<double, 2>>::value ||
73 std::is_same<T, std::array<int32_t, 2>>::value ||
74 std::is_same<T, std::array<double, 3>>::value ||
75 std::is_same<T, std::array<int32_t, 3>>::value ||
76 std::is_same<T, std::array<double, 4>>::value,
77 "Invalid property type.");
83 static_assert(std::is_same<T, int32_t>::value || std::is_same<T, std::string>::value,
"Invalid enum type.");
91 if (std::is_same<T, double>::value)
93 if (std::is_same<T, int32_t>::value)
95 if (std::is_same<T, std::string>::value)
97 if (std::is_same<T, bool>::value)
99 if (std::is_same<T, std::array<double, 2>>::value)
101 if (std::is_same<T, std::array<int32_t, 2>>::value)
103 if (std::is_same<T, std::array<double, 3>>::value)
105 if (std::is_same<T, std::array<int32_t, 3>>::value)
107 if (std::is_same<T, std::array<double, 4>>::value)
109 throw std::runtime_error(
"Could not match type for '" +
name +
"'");
112 template <
typename T>
117 , _data(std::move(value))
121 assertValidType<T>();
124 template <
typename T>
125 Property(
const std::string& name_,
const T value,
const T min_,
const T max_,
const MetaData& metaData_ = {})
129 , _data(std::move(value))
133 assertValidType<T>();
140 template <
typename T>
141 Property(
const std::string& name_,
const T value,
const std::vector<std::string>& enums_,
const MetaData& metaData_)
146 , _data(std::move(value))
148 , _max(enums_.size())
150 assertValidEnumType<T>();
158 , _data(std::move(other._data))
159 , _min(std::move(other._min))
160 , _max(std::move(other._max))
161 , _readOnly(other._readOnly)
174 template <
typename T>
177 assertValidType<T>();
182 template <
typename T>
185 assertValidType<T>();
186 return _castValue<T>(_data);
189 template <
typename T>
192 assertValidType<T>();
193 return _castValue<T>(_min);
196 template <
typename T>
199 assertValidType<T>();
200 return _castValue<T>(_max);
215 auto prop{std::move(property)};
216 prop._readOnly =
true;
223 const std::vector<std::string>
enums;
229 const linb::any _min;
230 const linb::any _max;
231 bool _readOnly{
false};
234 template <
typename T>
235 void _checkType()
const
237 const auto requestedType = getType<T>();
238 if (requestedType !=
type)
239 throw std::runtime_error(
"Type mismatch for property '" +
name +
"'");
241 template <
typename T>
242 T _castValue(
const linb::any& v)
const
245 return linb::any_cast<T>(v);
248 void _setData(
const linb::any&
data)
251 if (_modifiedCallback)
252 _modifiedCallback(*
this);
276 : _name(std::move(other._name))
277 , _properties(std::move(other._properties))
283 std::swap(_name, other._name);
284 std::swap(_properties, other._properties);
288 bool empty()
const {
return _properties.empty(); }
295 template <
typename T>
298 if (
auto property = find(name))
300 if (property->type != property->getType<T>())
301 throw std::runtime_error(
"updateProperty does not allow for changing the type");
309 if (
auto property = find(newProperty.
name))
311 if (property->type != newProperty.
type)
312 throw std::runtime_error(
"setProperty does not allow for changing the type");
313 property->_setData(newProperty._data);
316 _properties.push_back(std::make_shared<Property>(newProperty));
322 template <
typename T>
323 inline T
getProperty(
const std::string& name, T valIfNotFound)
const
325 if (
auto property = find(name))
326 return property->get<T>();
327 return valIfNotFound;
334 template <
typename T>
337 if (
auto property = find(name))
338 return property->get<T>();
339 throw std::runtime_error(
"No property found with name " + name);
343 bool hasProperty(
const std::string& name)
const {
return find(name) !=
nullptr; }
349 const auto&
getEnums(
const std::string& name)
const
351 if (
auto property = find(name))
352 return property->enums;
353 throw std::runtime_error(
"No property found with name " + name);
359 if (
auto property = find(name))
360 return property->type;
361 throw std::runtime_error(
"No property found with name " + name);
367 if (
auto property = find(name))
368 return property->metaData;
369 throw std::runtime_error(
"No property found with name " + name);
401 bool parse(
int argc,
const char** argv);
404 Property* find(
const std::string& name)
const
407 std::find_if(_properties.begin(), _properties.end(), [&](
const auto& p) { return p->name == name; });
409 return foundProperty != _properties.end() ? foundProperty->get() :
nullptr;
413 std::vector<std::shared_ptr<Property>> _properties;
void setProperty(const Property &newProperty)
void updateProperty(const std::string &name, const T &t)
void update(const PropertyMap &input)
const auto & getProperties() const
const auto & getName() const
PropertyMap(const PropertyMap &other)=default
const auto & getMetaData(const std::string &name) const
bool hasProperty(const std::string &name) const
const auto & getEnums(const std::string &name) const
PropertyMap(PropertyMap &&other) noexcept
T getProperty(const std::string &name) const
T getProperty(const std::string &name, T valIfNotFound) const
bool parse(int argc, const char **argv)
void merge(const PropertyMap &input)
PropertyMap(const std::string &name)
Property::Type getPropertyType(const std::string &name) const
PropertyMap & operator=(PropertyMap other) noexcept
Property(Property &&other)
std::function< void(const Property &)> ModifiedCallback
Property(const std::string &name_, const T value, const T min_, const T max_, const MetaData &metaData_={})
const std::vector< std::string > enums
void assertValidType() const
void onModified(const ModifiedCallback &callback)
static Property makeReadOnly(Property property)
Property(const Property &other)=default
Property(const std::string &name_, const T value, const std::vector< std::string > &enums_, const MetaData &metaData_)
void assertValidEnumType() const
Property(const std::string &name_, const T value, const MetaData &metaData_={})
const MetaData metaData
user-friendly name and description