hpp-manipulation  4.12.0
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 // This file is part of hpp-manipulation
6 // hpp-manipulation is free software: you can redistribute it
7 // and/or modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation, either version
9 // 3 of the License, or (at your option) any later version.
10 //
11 // hpp-manipulation is distributed in the hope that it will be
12 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Lesser Public License for more details. You should have
15 // received a copy of the GNU Lesser General Public License along with
16 // hpp-manipulation If not, see
17 // <http://www.gnu.org/licenses/>.
18 
19 #ifndef HPP_MANIPULATION_SERIALIZATION_HH
20 # define HPP_MANIPULATION_SERIALIZATION_HH
21 
22 # include <boost/serialization/split_free.hpp>
23 # include <boost/serialization/shared_ptr.hpp>
24 # include <boost/serialization/weak_ptr.hpp>
25 
26 # include <hpp/util/serialization.hh>
27 # include <hpp/pinocchio/serialization.hh>
28 
29 # include <hpp/manipulation/fwd.hh>
30 
34 
35 namespace hpp {
36 namespace serialization {
37 template<typename Archive>
38 manipulation::graph::GraphPtr_t getGraphFromArchive(Archive& ar, const std::string& name)
39 {
40  auto* har = hpp::serialization::cast(&ar);
41  if (!har || !har->contains(name))
42  throw std::runtime_error("Cannot deserialize edges with a provided graph with correct name.");
43  return har->template get<manipulation::graph::Graph>(name, true)->self();
44 }
45 
46 template<class Archive, class GraphCompT>
47 inline void serializeGraphComponent(Archive & ar, shared_ptr<GraphCompT>& c, const unsigned int version)
48 {
49  (void) version;
50 
51  std::size_t id;
52  std::string name;
53  if (Archive::is_saving::value) {
54  id = (c ? c->id() : -1);
55  if (c && c->parentGraph()) name = c->parentGraph()->name();
56  }
57  ar & BOOST_SERIALIZATION_NVP(id);
58  ar & BOOST_SERIALIZATION_NVP(name);
59  if (!Archive::is_saving::value) {
60  auto graph = getGraphFromArchive(ar, name);
61  c = HPP_DYNAMIC_PTR_CAST(GraphCompT, graph->get(id).lock());
62  }
63 }
64 } // namespace manipulation
65 } // namespace hpp
66 
67 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::RoadmapNode)
68 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::ConnectedComponent)
69 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::WeighedLeafConnectedComp)
70 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::Roadmap)
71 
72 namespace boost {
73 namespace serialization {
74 template<class Archive>
75 inline void serialize(Archive & ar, hpp::manipulation::graph::GraphPtr_t& g, const unsigned int version)
76 {
78  (void) version;
79 
80  std::string name;
81  if (Archive::is_saving::value) name = g->name();
82  ar & BOOST_SERIALIZATION_NVP(name);
83  if (!Archive::is_saving::value)
84  g = getGraphFromArchive(ar, name);
85 }
86 
87 template<class Archive>
88 inline void serialize(Archive & ar, hpp::manipulation::graph::EdgePtr_t& e, const unsigned int version)
89 {
91 }
92 
93 template<class Archive>
94 inline void serialize(Archive & ar, hpp::manipulation::graph::StatePtr_t& s, const unsigned int version)
95 {
97 }
98 
99 template<class Archive>
100 inline void serialize(Archive & ar, hpp::manipulation::graph::EdgeWkPtr_t& e, const unsigned int version)
101 {
102  auto e_ = e.lock();
103  serialize(ar, e_, version);
104  e = e_;
105 }
106 
107 template<class Archive>
108 inline void serialize(Archive & ar, hpp::manipulation::graph::StateWkPtr_t& s, const unsigned int version)
109 {
110  auto s_ = s.lock();
111  serialize(ar, s_, version);
112  s = s_;
113 }
114 
115 template<class Archive>
116 inline void load (Archive& ar, hpp::manipulation::DevicePtr_t& d, const unsigned int version)
117 {
118  load<Archive, hpp::manipulation::Device> (ar, d, version);
119  auto* har = hpp::serialization::cast(&ar);
120  if (d && har && har->contains(d->name()))
121  d = har->template getChildClass<hpp::pinocchio::Device, hpp::manipulation::Device>(d->name(), true)->self();
122 }
123 
124 template<class Archive>
125 inline void load (Archive& ar, hpp::manipulation::DeviceWkPtr_t& d, const unsigned int version)
126 {
127  load<Archive, hpp::manipulation::Device> (ar, d, version);
128  auto* har = hpp::serialization::cast(&ar);
129  auto dd = d.lock();
130  if (!dd) return;
131  if (har && har->contains(dd->name()))
132  d = har->template getChildClass<hpp::pinocchio::Device, hpp::manipulation::Device>(dd->name(), true)->self();
133 }
134 } // namespace serialization
135 } // namespace boost
136 
137 #endif // HPP_MANIPULATION_SERIALIZATION_HH
manipulation::graph::GraphPtr_t getGraphFromArchive(Archive &ar, const std::string &name)
Definition: serialization.hh:38
Definition: connected-component.hh:32
Definition: serialization.hh:72
Definition: main.hh:1
void serializeGraphComponent(Archive &ar, shared_ptr< GraphCompT > &c, const unsigned int version)
Definition: serialization.hh:47
void load(Archive &ar, hpp::manipulation::DeviceWkPtr_t &d, const unsigned int version)
Definition: serialization.hh:125
shared_ptr< Graph > GraphPtr_t
Definition: fwd.hh:35
void serialize(Archive &ar, hpp::manipulation::graph::StateWkPtr_t &s, const unsigned int version)
Definition: serialization.hh:108
Definition: roadmap.hh:35
shared_ptr< State > StatePtr_t
Definition: fwd.hh:36
shared_ptr< Device > DevicePtr_t
Definition: fwd.hh:29
shared_ptr< Edge > EdgePtr_t
Definition: fwd.hh:37
Definition: leaf-connected-comp.hh:116
Definition: roadmap-node.hh:32