4545#include < vector>
4646#include < functional>
4747#include < sstream>
48- #include < ros/serialization.h>
4948
5049#include < moveit_task_constructor_msgs/Property.h>
5150
51+ #include " properties/serialize.hpp"
52+
5253namespace moveit {
5354namespace task_constructor {
5455
@@ -156,6 +157,13 @@ class Property
156157 // / serialize value using registered functions
157158 static std::string serialize (const boost::any& value);
158159 static boost::any deserialize (const std::string& type_name, const std::string& wire);
160+
161+ template <typename T>
162+ static T deserialize (const moveit_task_constructor_msgs::Property& property_msg) {
163+ PropertySerializer<T>();
164+ return boost::any_cast<T>(deserialize (property_msg.type , property_msg.value ));
165+ }
166+
159167 std::string serialize () const { return serialize (value ()); }
160168
161169 // / get description text
@@ -173,6 +181,12 @@ class Property
173181 // / configure initialization from source using given other property name
174182 Property& configureInitFrom (SourceFlags source, const std::string& name);
175183
184+ void fillMsg (moveit_task_constructor_msgs::Property& msg) const {
185+ msg.description = description ();
186+ msg.type = typeName ();
187+ msg.value = serialize ();
188+ }
189+
176190private:
177191 std::string description_;
178192 const type_info& type_info_;
@@ -185,84 +199,6 @@ class Property
185199 InitializerFunction initializer_;
186200};
187201
188- // hasSerialize<T>::value provides a true/false constexpr depending on whether operator<< is supported.
189- // This uses SFINAE, extracted from https://jguegant.github.io/blogs/tech/sfinae-introduction.html
190- template <typename T, typename = std::ostream&>
191- struct hasSerialize : std::false_type
192- {};
193-
194- template <typename T>
195- struct hasSerialize <T, decltype (std::declval<std::ostream&>() << std::declval<T>())> : std::true_type
196- {};
197-
198- template <typename T, typename = std::istream&>
199- struct hasDeserialize : std::false_type
200- {};
201-
202- template <typename T>
203- struct hasDeserialize <T, decltype (std::declval<std::istream&>() >> std::declval<T&>())> : std::true_type
204- {};
205-
206- class PropertySerializerBase
207- {
208- public:
209- using SerializeFunction = std::string (*)(const boost::any&);
210- using DeserializeFunction = boost::any (*)(const std::string&);
211-
212- static std::string dummySerialize (const boost::any& /* unused*/ ) { return " " ; }
213- static boost::any dummyDeserialize (const std::string& /* unused*/ ) { return boost::any (); }
214-
215- protected:
216- static bool insert (const std::type_index& type_index, const std::string& type_name, SerializeFunction serialize,
217- DeserializeFunction deserialize);
218- };
219-
220- // / utility class to register serializer/deserializer functions for a property of type T
221- template <typename T>
222- class PropertySerializer : protected PropertySerializerBase
223- {
224- public:
225- PropertySerializer () { insert (typeid (T), typeName<T>(), &serialize, &deserialize); }
226-
227- template <class Q = T>
228- static typename std::enable_if<ros::message_traits::IsMessage<Q>::value, std::string>::type typeName () {
229- return ros::message_traits::DataType<T>::value ();
230- }
231-
232- template <class Q = T>
233- static typename std::enable_if<!ros::message_traits::IsMessage<Q>::value, std::string>::type typeName () {
234- return typeid (T).name ();
235- }
236-
237- private:
238- /* * Serialization based on std::[io]stringstream */
239- template <class Q = T>
240- static typename std::enable_if<hasSerialize<Q>::value, std::string>::type serialize (const boost::any& value) {
241- std::ostringstream oss;
242- oss << boost::any_cast<T>(value);
243- return oss.str ();
244- }
245- template <class Q = T>
246- static typename std::enable_if<hasSerialize<Q>::value && hasDeserialize<Q>::value, boost::any>::type
247- deserialize (const std::string& wired) {
248- std::istringstream iss (wired);
249- T value;
250- iss >> value;
251- return value;
252- }
253-
254- /* * No serialization available */
255- template <class Q = T>
256- static typename std::enable_if<!hasSerialize<Q>::value, std::string>::type serialize (const boost::any& value) {
257- return dummySerialize (value);
258- }
259- template <class Q = T>
260- static typename std::enable_if<!hasSerialize<Q>::value || !hasDeserialize<Q>::value, boost::any>::type
261- deserialize (const std::string& wire) {
262- return dummyDeserialize (wire);
263- }
264- };
265-
266202/* * PropertyMap is map of (name, Property) pairs.
267203 *
268204 * Conveniency methods are provided to setup property initialization for several
@@ -303,7 +239,8 @@ class PropertyMap
303239 Property& property (const std::string& name);
304240 const Property& property (const std::string& name) const { return const_cast <PropertyMap*>(this )->property (name); }
305241
306- void fillMsgs (std::vector<moveit_task_constructor_msgs::Property>& msg) const ;
242+ void fillMsgs (std::vector<moveit_task_constructor_msgs::Property>& msgs) const ;
243+ void fromMsgs (std::vector<moveit_task_constructor_msgs::Property>& msgs);
307244
308245 using iterator = std::map<std::string, Property>::iterator;
309246 using const_iterator = std::map<std::string, Property>::const_iterator;
0 commit comments