GCC Code Coverage Report


Directory: ./
File: include/hpp/manipulation/serialization.hh
Date: 2025-03-07 11:10:46
Exec Total Coverage
Lines: 4 44 9.1%
Branches: 0 70 0.0%

Line Branch Exec Source
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/weak_ptr.hpp>
36 #include <hpp/manipulation/fwd.hh>
37 #include <hpp/manipulation/graph/edge.hh>
38 #include <hpp/manipulation/graph/graph.hh>
39 #include <hpp/manipulation/graph/state.hh>
40 #include <hpp/pinocchio/serialization.hh>
41 #include <hpp/util/serialization.hh>
42
43 namespace hpp {
44 namespace serialization {
45 template <typename Archive>
46 manipulation::graph::GraphPtr_t getGraphFromArchive(Archive& ar,
47 const std::string& name) {
48 auto* har = hpp::serialization::cast(&ar);
49 if (!har || !har->contains(name))
50 throw std::runtime_error(
51 "Cannot deserialize edges with a provided graph with correct name.");
52 return har->template get<manipulation::graph::Graph>(name, true)->self();
53 }
54
55 template <class Archive, class GraphCompT>
56 inline void serializeGraphComponent(Archive& ar, shared_ptr<GraphCompT>& c,
57 const unsigned int version) {
58 (void)version;
59
60 std::size_t id;
61 std::string name;
62 if (Archive::is_saving::value) {
63 id = (c ? c->id() : -1);
64 if (c && c->parentGraph()) name = c->parentGraph()->name();
65 }
66 ar& BOOST_SERIALIZATION_NVP(id);
67 ar& BOOST_SERIALIZATION_NVP(name);
68 if (!Archive::is_saving::value) {
69 auto graph = getGraphFromArchive(ar, name);
70 c = HPP_DYNAMIC_PTR_CAST(GraphCompT, graph->get(id).lock());
71 }
72 }
73 } // namespace serialization
74 } // namespace hpp
75
76 1 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::RoadmapNode)
77 1 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::ConnectedComponent)
78 1 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::WeighedLeafConnectedComp)
79 1 BOOST_CLASS_EXPORT_KEY(hpp::manipulation::Roadmap)
80
81 namespace boost {
82 namespace serialization {
83 template <class Archive>
84 inline void serialize(Archive& ar, hpp::manipulation::graph::GraphPtr_t& g,
85 const unsigned int version) {
86 using hpp::serialization::getGraphFromArchive;
87 (void)version;
88
89 std::string name;
90 if (Archive::is_saving::value) name = g->name();
91 ar& BOOST_SERIALIZATION_NVP(name);
92 if (!Archive::is_saving::value) g = getGraphFromArchive(ar, name);
93 }
94
95 template <class Archive>
96 inline void serialize(Archive& ar, hpp::manipulation::graph::EdgePtr_t& e,
97 const unsigned int version) {
98 hpp::serialization::serializeGraphComponent(ar, e, version);
99 }
100
101 template <class Archive>
102 inline void serialize(Archive& ar, hpp::manipulation::graph::StatePtr_t& s,
103 const unsigned int version) {
104 hpp::serialization::serializeGraphComponent(ar, s, version);
105 }
106
107 template <class Archive>
108 inline void serialize(Archive& ar, hpp::manipulation::graph::EdgeWkPtr_t& e,
109 const unsigned int version) {
110 auto e_ = e.lock();
111 serialize(ar, e_, version);
112 e = e_;
113 }
114
115 template <class Archive>
116 inline void serialize(Archive& ar, hpp::manipulation::graph::StateWkPtr_t& s,
117 const unsigned int version) {
118 auto s_ = s.lock();
119 serialize(ar, s_, version);
120 s = s_;
121 }
122
123 template <class Archive>
124 inline void load(Archive& ar, hpp::manipulation::DevicePtr_t& d,
125 const unsigned int version) {
126 load<Archive, hpp::manipulation::Device>(ar, d, version);
127 auto* har = hpp::serialization::cast(&ar);
128 if (d && har && har->contains(d->name()))
129 d = har->template getChildClass<hpp::pinocchio::Device,
130 hpp::manipulation::Device>(d->name(), true)
131 ->self();
132 }
133
134 template <class Archive>
135 inline void load(Archive& ar, hpp::manipulation::DeviceWkPtr_t& d,
136 const unsigned int version) {
137 load<Archive, hpp::manipulation::Device>(ar, d, version);
138 auto* har = hpp::serialization::cast(&ar);
139 auto dd = d.lock();
140 if (!dd) return;
141 if (har && har->contains(dd->name()))
142 d = har->template getChildClass<hpp::pinocchio::Device,
143 hpp::manipulation::Device>(dd->name(), true)
144 ->self();
145 }
146 } // namespace serialization
147 } // namespace boost
148
149 #endif // HPP_MANIPULATION_SERIALIZATION_HH
150