hpp-manipulation  9.0.2
Classes for manipulation planning.
serialization.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 CNRS
3 // Author: Joseph Mirabel
4 //
5 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // 2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 // DAMAGE.
29 
30 #ifndef HPP_MANIPULATION_SERIALIZATION_HH
31 #define HPP_MANIPULATION_SERIALIZATION_HH
32 
33 #include <boost/serialization/shared_ptr.hpp>
34 #include <boost/serialization/split_free.hpp>
35 #include <boost/serialization/vector.hpp>
36 #include <boost/serialization/weak_ptr.hpp>
37 #include <hpp/manipulation/fwd.hh>
43 
44 namespace hpp {
45 namespace serialization {
46 template <typename Archive>
48  const std::string& name) {
49  auto* har = hpp::serialization::cast(&ar);
50  if (!har || !har->contains(name))
51  throw std::runtime_error(
52  "Cannot deserialize edges with a provided graph with correct name.");
53  return har->template get<manipulation::graph::Graph>(name, true)->self();
54 }
55 
56 template <class Archive, class GraphCompT>
57 inline void serializeGraphComponent(Archive& ar, shared_ptr<GraphCompT>& c,
58  const unsigned int version) {
59  (void)version;
60 
61  std::size_t id;
62  std::string name;
63  if (Archive::is_saving::value) {
64  id = (c ? c->id() : -1);
65  if (c && c->parentGraph()) name = c->parentGraph()->name();
66  }
67  ar& BOOST_SERIALIZATION_NVP(id);
68  ar& BOOST_SERIALIZATION_NVP(name);
69  if (!Archive::is_saving::value) {
70  auto graph = getGraphFromArchive(ar, name);
71  c = HPP_DYNAMIC_PTR_CAST(GraphCompT, graph->get(id).lock());
72  }
73 }
74 } // namespace serialization
75 } // namespace hpp
76 
77 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::RoadmapNode)
78 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::ConnectedComponent)
79 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::WeighedLeafConnectedComp)
80 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::Roadmap)
81 
82 namespace boost {
83 namespace serialization {
84 template <class Archive>
85 inline void serialize(Archive& ar, hpp::manipulation::graph::GraphPtr_t& g,
86  const unsigned int version) {
88  (void)version;
89 
90  std::string name;
91  if (Archive::is_saving::value) name = g->name();
92  ar& BOOST_SERIALIZATION_NVP(name);
93  if (!Archive::is_saving::value) g = getGraphFromArchive(ar, name);
94 }
95 
96 template <class Archive>
97 inline void serialize(Archive& ar, hpp::manipulation::graph::EdgePtr_t& e,
98  const unsigned int version) {
100 }
101 
102 template <class Archive>
104  const unsigned int version) {
106 }
107 
108 template <class Archive>
109 inline void serialize(Archive& ar, hpp::manipulation::graph::EdgeWkPtr_t& e,
110  const unsigned int version) {
111  auto e_ = e.lock();
112  serialize(ar, e_, version);
113  e = e_;
114 }
115 
116 template <class Archive>
117 inline void serialize(Archive& ar, hpp::manipulation::graph::StateWkPtr_t& s,
118  const unsigned int version) {
119  auto s_ = s.lock();
120  serialize(ar, s_, version);
121  s = s_;
122 }
123 
124 template <class Archive>
125 inline void load(Archive& ar, hpp::manipulation::DevicePtr_t& d,
126  const unsigned int version) {
127  load<Archive, hpp::manipulation::Device>(ar, d, version);
128  auto* har = hpp::serialization::cast(&ar);
129  if (d && har && har->contains(d->name()))
130  d = har->template getChildClass<hpp::pinocchio::Device,
131  hpp::manipulation::Device>(d->name(), true)
132  ->self();
133 }
134 
135 template <class Archive>
136 inline void load(Archive& ar, hpp::manipulation::DeviceWkPtr_t& d,
137  const unsigned int version) {
138  load<Archive, hpp::manipulation::Device>(ar, d, version);
139  auto* har = hpp::serialization::cast(&ar);
140  auto dd = d.lock();
141  if (!dd) return;
142  if (har && har->contains(dd->name()))
143  d = har->template getChildClass<hpp::pinocchio::Device,
144  hpp::manipulation::Device>(dd->name(), true)
145  ->self();
146 }
147 } // namespace serialization
148 } // namespace boost
149 
150 #endif // HPP_MANIPULATION_SERIALIZATION_HH
Definition: connected-component.hh:44
Definition: device.hh:50
Definition: roadmap-node.hh:42
Definition: roadmap.hh:47
Definition: leaf-connected-comp.hh:110
const Derived & d
void serialize(Archive &ar, Eigen::MatrixBlocks< _allRows, _allCols > &b, const unsigned int version)
void load(Archive &ar, hpp::pinocchio::DevicePtr_t &d, const unsigned int version)
Definition: serialization.hh:125
shared_ptr< Edge > EdgePtr_t
Definition: fwd.hh:49
shared_ptr< State > StatePtr_t
Definition: fwd.hh:48
shared_ptr< Graph > GraphPtr_t
Definition: fwd.hh:47
shared_ptr< Device > DevicePtr_t
Definition: fwd.hh:40
manipulation::graph::GraphPtr_t getGraphFromArchive(Archive &ar, const std::string &name)
Definition: serialization.hh:47
archive_ptr_holder & cast(Archive &ar)
void serializeGraphComponent(Archive &ar, shared_ptr< GraphCompT > &c, const unsigned int version)
Definition: serialization.hh:57
HPP_UTIL_DLLAPI const char * version
#define HPP_DYNAMIC_PTR_CAST(t, x)