41 #include <boost/any.hpp>
48 #include <ros/serialization.h>
51 namespace task_constructor {
57 boost::any fromName(
const PropertyMap& other,
const std::string& other_name);
75 const boost::any& default_value);
78 using type_info = boost::typeindex::type_info;
92 using SourceFlags = uint;
98 void setCurrentValue(
const boost::any&
value);
99 void setDefaultValue(
const boost::any&
value);
105 inline bool defined()
const {
return !value_.empty(); }
108 inline const boost::any&
value()
const {
return value_.empty() ? default_ : value_; }
109 inline boost::any&
value() {
return value_.empty() ? default_ : value_; }
114 static std::string serialize(
const boost::any&
value);
115 static boost::any deserialize(
const std::string& type_name,
const std::string& wire);
116 std::string serialize()
const {
return serialize(
value()); }
120 void setDescription(
const std::string& desc) { description_ = desc; }
123 static std::string typeName(
const type_info& type_info);
124 std::string typeName()
const;
127 bool initsFrom(SourceFlags source)
const;
134 std::string description_;
135 const type_info& type_info_;
140 SourceFlags source_flags_ = 0;
141 SourceFlags initialized_from_;
148 std::string property_name_;
152 explicit error(
const std::string& msg);
153 const std::string& name()
const {
return property_name_; }
154 void setName(
const std::string& name);
155 const char* what()
const noexcept
override {
return msg_.c_str(); }
160 explicit undeclared(
const std::string& name,
const std::string& msg =
"undeclared");
165 explicit undefined(
const std::string& name,
const std::string& msg =
"undefined");
170 explicit type_error(
const std::string& current_type,
const std::string& declared_type);
175 template <
typename T,
typename = std::ostream&>
179 template <
typename T>
180 struct hasSerialize<T, decltype(std::declval<std::ostream&>() << std::declval<T>())> : std::true_type
183 template <
typename T,
typename = std::istream&>
187 template <
typename T>
188 struct hasDeserialize<T, decltype(std::declval<std::istream&>() >> std::declval<T&>())> : std::true_type
194 using SerializeFunction = std::string (*)(
const boost::any&);
195 using DeserializeFunction = boost::any (*)(
const std::string&);
197 static std::string dummySerialize(
const boost::any& ) {
return ""; }
198 static boost::any dummyDeserialize(
const std::string& ) {
return boost::any(); }
201 static bool insert(
const std::type_index& type_index,
const std::string& type_name, SerializeFunction serialize,
202 DeserializeFunction deserialize);
206 template <
typename T>
212 template <
class Q = T>
213 static typename std::enable_if<ros::message_traits::IsMessage<Q>::value, std::string>::type typeName() {
214 return ros::message_traits::DataType<T>::value();
217 template <
class Q = T>
218 static typename std::enable_if<!ros::message_traits::IsMessage<Q>::value, std::string>::type typeName() {
219 return typeid(T).name();
224 template <
class Q = T>
225 static typename std::enable_if<hasSerialize<Q>::value, std::string>::type serialize(
const boost::any& value) {
226 std::ostringstream oss;
227 oss << boost::any_cast<T>(value);
230 template <
class Q = T>
232 deserialize(
const std::string& wired) {
233 std::istringstream iss(wired);
240 template <
class Q = T>
241 static typename std::enable_if<!hasSerialize<Q>::value, std::string>::type serialize(
const boost::any& value) {
242 return dummySerialize(value);
244 template <
class Q = T>
246 deserialize(
const std::string& wire) {
247 return dummyDeserialize(wire);
258 std::map<std::string, Property> props_;
261 Property& declare(
const std::string& name,
const Property::type_info& type_info,
const std::string& description,
262 const boost::any& default_value);
266 template <
typename T>
269 return declare(name,
typeid(T), description, boost::any());
272 template <
typename T>
273 Property&
declare(
const std::string& name,
const T& default_value,
const std::string& description =
"") {
275 return declare(name,
typeid(T), description, default_value);
282 void exposeTo(
PropertyMap& other,
const std::string& name,
const std::string& other_name)
const;
291 using iterator = std::map<std::string, Property>::iterator;
292 using const_iterator = std::map<std::string, Property>::const_iterator;
294 iterator begin() {
return props_.begin(); }
295 iterator end() {
return props_.end(); }
296 const_iterator begin()
const {
return props_.begin(); }
297 const_iterator end()
const {
return props_.end(); }
300 void configureInitFrom(Property::SourceFlags source,
const std::set<std::string>& properties = {});
303 template <
typename T>
304 void set(
const std::string& name,
const T& value) {
305 auto it = props_.find(name);
306 if (it == props_.end())
307 declare<T>(name, value,
"");
309 it->second.setValue(value);
313 inline void set(
const std::string& name,
const char* value) { set<std::string>(name, value); }
316 void setCurrent(
const std::string& name,
const boost::any& value);
319 const boost::any&
get(
const std::string& name)
const;
322 template <
typename T>
323 const T&
get(
const std::string& name)
const {
324 const boost::any& value =
get(name);
327 return boost::any_cast<const T&>(value);
330 template <
typename T>
331 const T&
get(
const std::string& name,
const T& fallback)
const {
332 const boost::any& value =
get(name);
333 return (value.empty()) ? fallback : boost::any_cast<const T&>(value);
337 size_t countDefined(
const std::vector<std::string>& list)
const;
348 void PropertyMap::set<boost::any>(
const std::string& name,
const boost::any& value);
Definition: properties.h:257
void performInitFrom(Property::SourceFlags source, const PropertyMap &other)
perform initialization of still undefined properties using configured initializers
Definition: properties.cpp:272
void set(const std::string &name, const char *value)
overloading: const char* is stored as std::string
Definition: properties.h:313
void setCurrent(const std::string &name, const boost::any &value)
temporarily set the value of a property
Definition: properties.cpp:250
void exposeTo(PropertyMap &other, const std::set< std::string > &properties) const
declare all given properties also in other PropertyMap
Definition: properties.cpp:216
bool hasProperty(const std::string &name) const
check whether given property is declared
Definition: properties.cpp:204
Property & declare(const std::string &name, const T &default_value, const std::string &description="")
declare a property with default value
Definition: properties.h:273
void configureInitFrom(Property::SourceFlags source, const std::set< std::string > &properties={})
allow initialization from given source for listed properties - always using the same name
Definition: properties.cpp:226
size_t countDefined(const std::vector< std::string > &list) const
count number of defined properties from given list
Definition: properties.cpp:258
Property & property(const std::string &name)
get the property with given name, throws Property::undeclared for unknown name
Definition: properties.cpp:209
const boost::any & get(const std::string &name) const
Get the value of a property. Throws undeclared if unknown name.
Definition: properties.cpp:254
void reset()
reset all properties to their defaults
Definition: properties.cpp:267
const T & get(const std::string &name, const T &fallback) const
get typed value of property, using fallback if undefined. Throws bad_any_cast on type mismatch.
Definition: properties.h:331
const T & get(const std::string &name) const
Get typed value of property. Throws undeclared, undefined, or bad_any_cast.
Definition: properties.h:323
Property & declare(const std::string &name, const std::string &description="")
declare a property for future use
Definition: properties.h:267
void set(const std::string &name, const T &value)
set (and, if neccessary, declare) the value of a property
Definition: properties.h:304
Definition: properties.h:192
utility class to register serializer/deserializer functions for a property of type T
Definition: properties.h:208
Definition: properties.h:146
Definition: properties.h:168
Definition: properties.h:158
Definition: properties.h:163
Definition: properties.h:70
void reset()
reset to default value (which can be empty)
Definition: properties.cpp:143
bool initsFrom(SourceFlags source) const
return true, if property initialized from given SourceId
Definition: properties.cpp:177
std::function< boost::any(const PropertyMap &)> InitializerFunction
function callback used to initialize property value from another PropertyMap
Definition: properties.h:94
const boost::any & value() const
get current value (or default if not defined)
Definition: properties.h:108
bool defined() const
the current value defined or will the fallback be used?
Definition: properties.h:105
const std::string & description() const
get description text
Definition: properties.h:119
Property & configureInitFrom(SourceFlags source, const InitializerFunction &f)
configure initialization from source using an arbitrary function
Definition: properties.cpp:181
const boost::any & defaultValue() const
get default value
Definition: properties.h:111
Property()
Construct a property holding a any value.
Definition: properties.cpp:120
void setValue(const boost::any &value)
set current value and default value
Definition: properties.cpp:122
Definition: properties.h:185
Definition: properties.h:177