19 #ifndef HPP_PINOCCHIO_SERIALIZATION_HH
20 # define HPP_PINOCCHIO_SERIALIZATION_HH
23 # include <type_traits>
25 # include <pinocchio/fwd.hpp>
26 # include <boost/serialization/split_free.hpp>
27 # include <boost/serialization/shared_ptr.hpp>
28 # include <boost/serialization/weak_ptr.hpp>
30 # include <pinocchio/serialization/eigen.hpp>
31 # include <hpp/util/serialization.hh>
37 BOOST_SERIALIZATION_SPLIT_FREE(hpp::pinocchio::DeviceWkPtr_t)
40 namespace serialization {
41 template<
class Archive>
44 load<Archive, hpp::pinocchio::Device> (ar, d, version);
45 auto* har = hpp::serialization::cast(&ar);
46 if (d && har && har->contains(d->name()))
47 d = har->template get<hpp::pinocchio::Device>(d->name(),
true)->self();
49 template<
class Archive>
50 inline void load (Archive& ar, hpp::pinocchio::DeviceWkPtr_t& d,
const unsigned int version)
52 load<Archive, hpp::pinocchio::Device> (ar, d, version);
53 auto* har = hpp::serialization::cast(&ar);
54 std::string name (d.lock()->name());
55 if (d.lock() && har && har->contains(name))
56 d = har->template get<hpp::pinocchio::Device>(name,
true)->self();
58 template<
class Archive>
61 load<Archive, hpp::pinocchio::HumanoidRobot> (ar, d, version);
62 auto* har = hpp::serialization::cast(&ar);
63 if (d && har && har->contains(d->name()))
64 d = har->template getChildClass<hpp::pinocchio::Device, hpp::pinocchio::HumanoidRobot>(d->name(),
true)->self();
66 template<
class Archive>
67 inline void load (Archive& ar, hpp::pinocchio::HumanoidRobotWkPtr_t& d,
const unsigned int version)
69 load<Archive, hpp::pinocchio::HumanoidRobot> (ar, d, version);
70 auto* har = hpp::serialization::cast(&ar);
71 std::string name (d.lock()->name());
72 if (d.lock() && har && har->contains(name))
73 d = har->template getChildClass<hpp::pinocchio::Device, hpp::pinocchio::HumanoidRobot>(name,
true)->self();
76 template <
class Archive,
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
77 inline void serialize(Archive & ar, Eigen::Array<_Scalar,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & m,
const unsigned int version)
80 Eigen::DenseIndex rows(m.rows()), cols(m.cols());
81 ar & BOOST_SERIALIZATION_NVP(rows);
82 ar & BOOST_SERIALIZATION_NVP(cols);
83 if (!Archive::is_saving::value)
86 ar & make_nvp(
"data",make_array(m.data(), (
size_t)m.size()));
93 namespace serialization {
94 namespace remove_duplicate {
95 template<
typename Key,
typename Compare = std::less<Key> >
97 inline bool operator() (Key
const* t1, Key
const* t2)
const {
return Compare::operator() (*t1, *t2); }
100 template<
typename Derived>
103 const Eigen::PlainObjectBase<Derived>& b)
const
105 if (a.size() < b.size())
return true;
106 if (a.size() > b.size())
return false;
107 for(Eigen::Index i = 0; i < a.size(); ++i) {
108 if (a.derived().data()[i] < b.derived().data()[i])
return true;
109 if (a.derived().data()[i] > b.derived().data()[i])
return false;
115 template<
typename Key,
typename Compare = std::less<Key> >
119 std::set<Key const*, ptr_compare_type >
datas;
127 template<
class Archive,
typename Key>
131 const unsigned int version)
135 ar & boost::serialization::make_nvp(name, value);
136 if (!Archive::is_saving::value) key = *value;
139 template<
class Archive,
typename Key>
143 const unsigned int version)
146 Key
const* value = &key;
147 ar << boost::serialization::make_nvp(name, value);
150 template<
class Archive,
typename Key,
typename Compare = std::less<Key>>
156 const unsigned int version)
159 if(!Archive::is_saving::value)
160 throw std::logic_error(
"HPP serialization: cannot load into a const element. This should never happen.");
161 auto result = set.insert(&key);
162 bool inserted = result.second;
163 Key
const* k = (inserted ? &key : *result.first);
164 ar & boost::serialization::make_nvp(name, k);
165 if (!inserted) hitcount++;
168 template<
class Archive,
typename Key,
typename Compare = std::less<Key>>
174 const unsigned int version)
176 if (Archive::is_saving::value) {
177 save_impl(ar, set, hitcount, name, key, version);
183 template <
bool is_base>
185 template<
typename Archive,
typename Key,
typename Compare = std::less<Key> >
186 static inline void run (Archive& ar,
189 const unsigned int version)
194 template<
typename Archive,
typename Key,
typename Compare = std::less<Key> >
195 static inline void save (Archive& ar,
198 const unsigned int version)
207 template<
typename Archive,
typename Key,
typename Compare = std::less<Key> >
208 static inline void run (Archive& ar,
211 const unsigned int version)
218 template<
typename Archive,
typename Key,
typename Compare = std::less<Key> >
219 static inline void save (Archive& ar,
222 const unsigned int version)
231 template<
typename Archive,
typename Key,
typename Compare = std::less<Key>,
232 bool is_base = std::is_base_of<archive<Key, Compare>, Archive>::value >
236 const unsigned int version)
241 template<
typename Archive,
typename Key,
typename Compare = std::less<Key>,
242 bool is_base = std::is_base_of<archive<Key, Compare>, Archive>::value >
246 const unsigned int version)
251 template<
typename Archive>
255 const unsigned int version)
257 serialize<Archive, ::hpp::pinocchio::vector_t, vector_archive::compare_type>
258 (ar, name, key, version);
261 template<
typename Archive>
265 const unsigned int version)
267 save<Archive, ::hpp::pinocchio::vector_t, vector_archive::compare_type>
268 (ar, name, key, version);
275 #endif // HPP_PINOCCHIO_SERIALIZATION_HH