Skip to content
Snippets Groups Projects
Commit 0e05c8b2 authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

Update serialization functions.

parent 35861681
No related branches found
No related tags found
No related merge requests found
...@@ -50,6 +50,8 @@ namespace hpp { ...@@ -50,6 +50,8 @@ namespace hpp {
return shPtr; return shPtr;
} }
DevicePtr_t self () const { return self_.lock(); }
/// Print object in a stream /// Print object in a stream
virtual std::ostream& print (std::ostream& os) const; virtual std::ostream& print (std::ostream& os) const;
......
...@@ -52,6 +52,8 @@ namespace hpp { ...@@ -52,6 +52,8 @@ namespace hpp {
static GraphPtr_t create(const std::string& name, DevicePtr_t robot, static GraphPtr_t create(const std::string& name, DevicePtr_t robot,
const ProblemPtr_t& problem); const ProblemPtr_t& problem);
GraphPtr_t self () const { return wkPtr_.lock(); }
/// Create and insert a state selector inside the graph. /// Create and insert a state selector inside the graph.
StateSelectorPtr_t createStateSelector (const std::string& name); StateSelectorPtr_t createStateSelector (const std::string& name);
...@@ -301,4 +303,6 @@ namespace hpp { ...@@ -301,4 +303,6 @@ namespace hpp {
} // namespace hpp } // namespace hpp
BOOST_CLASS_EXPORT_KEY(hpp::manipulation::graph::Graph)
#endif // HPP_MANIPULATION_GRAPH_GRAPH_HH #endif // HPP_MANIPULATION_GRAPH_GRAPH_HH
...@@ -34,10 +34,33 @@ ...@@ -34,10 +34,33 @@
namespace hpp { namespace hpp {
namespace serialization { namespace serialization {
struct archive_graph_wrapper { template<typename Archive>
manipulation::graph::GraphPtr_t graph; manipulation::graph::GraphPtr_t getGraphFromArchive(Archive& ar, const std::string& name)
virtual ~archive_graph_wrapper() {} {
}; auto* har = hpp::serialization::cast(&ar);
if (!har || !har->contains(name))
throw std::runtime_error("Cannot deserialize edges with a provided graph with correct name.");
return har->template get<manipulation::graph::Graph>(name, true)->self();
}
template<class Archive, class GraphCompT>
inline void serializeGraphComponent(Archive & ar, boost::shared_ptr<GraphCompT>& c, const unsigned int version)
{
(void) version;
std::size_t id;
std::string name;
if (Archive::is_saving::value) {
id = (c ? c->id() : -1);
if (c && c->parentGraph()) name = c->parentGraph()->name();
}
ar & BOOST_SERIALIZATION_NVP(id);
ar & BOOST_SERIALIZATION_NVP(name);
if (!Archive::is_saving::value) {
auto graph = getGraphFromArchive(ar, name);
c = HPP_DYNAMIC_PTR_CAST(GraphCompT, graph->get(id).lock());
}
}
} // namespace manipulation } // namespace manipulation
} // namespace hpp } // namespace hpp
...@@ -51,64 +74,32 @@ namespace serialization { ...@@ -51,64 +74,32 @@ namespace serialization {
template<class Archive> template<class Archive>
inline void serialize(Archive & ar, hpp::manipulation::graph::GraphPtr_t& g, const unsigned int version) inline void serialize(Archive & ar, hpp::manipulation::graph::GraphPtr_t& g, const unsigned int version)
{ {
using hpp::serialization::archive_graph_wrapper; using hpp::serialization::getGraphFromArchive;
using namespace hpp::manipulation::graph;
(void) version; (void) version;
std::size_t id; std::string name;
if (Archive::is_saving::value) id = g->id(); if (Archive::is_saving::value) name = g->name();
ar & BOOST_SERIALIZATION_NVP(id); ar & BOOST_SERIALIZATION_NVP(name);
if (!Archive::is_saving::value) { if (!Archive::is_saving::value)
archive_graph_wrapper* agw = dynamic_cast<archive_graph_wrapper*>(&ar); g = getGraphFromArchive(ar, name);
if (agw == NULL)
throw std::runtime_error("Cannot deserialize edges with a archive_graph_wrapper");
g = agw->graph;
}
} }
template<class Archive> template<class Archive>
inline void serialize(Archive & ar, hpp::manipulation::graph::EdgePtr_t& e, const unsigned int version) inline void serialize(Archive & ar, hpp::manipulation::graph::EdgePtr_t& e, const unsigned int version)
{ {
using hpp::serialization::archive_graph_wrapper; hpp::serialization::serializeGraphComponent (ar, e, version);
using namespace hpp::manipulation::graph;
(void) version;
std::size_t id;
if (Archive::is_saving::value) id = (e ? e->id() : -1);
ar & BOOST_SERIALIZATION_NVP(id);
if (!Archive::is_saving::value) {
archive_graph_wrapper* agw = dynamic_cast<archive_graph_wrapper*>(&ar);
if (agw == NULL)
throw std::runtime_error("Cannot deserialize edges with a archive_graph_wrapper");
GraphComponentPtr_t gc = agw->graph->get(id).lock();
e = HPP_DYNAMIC_PTR_CAST(Edge, gc);
}
} }
template<class Archive> template<class Archive>
inline void serialize(Archive & ar, hpp::manipulation::graph::StatePtr_t& s, const unsigned int version) inline void serialize(Archive & ar, hpp::manipulation::graph::StatePtr_t& s, const unsigned int version)
{ {
using hpp::serialization::archive_graph_wrapper; hpp::serialization::serializeGraphComponent (ar, s, version);
using namespace hpp::manipulation::graph;
(void) version;
std::size_t id;
if (Archive::is_saving::value) id = (s ? s->id() : -1);
ar & BOOST_SERIALIZATION_NVP(id);
if (!Archive::is_saving::value) {
archive_graph_wrapper* agw = dynamic_cast<archive_graph_wrapper*>(&ar);
if (agw == NULL)
throw std::runtime_error("Cannot deserialize edges with a archive_graph_wrapper");
GraphComponentPtr_t gc = agw->graph->get(id).lock();
s = HPP_DYNAMIC_PTR_CAST(State, gc);
}
} }
template<class Archive> template<class Archive>
inline void serialize(Archive & ar, hpp::manipulation::graph::EdgeWkPtr_t& e, const unsigned int version) inline void serialize(Archive & ar, hpp::manipulation::graph::EdgeWkPtr_t& e, const unsigned int version)
{ {
using namespace hpp::manipulation::graph; auto e_ = e.lock();
EdgePtr_t e_ = e.lock();
serialize(ar, e_, version); serialize(ar, e_, version);
e = e_; e = e_;
} }
...@@ -116,8 +107,7 @@ inline void serialize(Archive & ar, hpp::manipulation::graph::EdgeWkPtr_t& e, co ...@@ -116,8 +107,7 @@ inline void serialize(Archive & ar, hpp::manipulation::graph::EdgeWkPtr_t& e, co
template<class Archive> template<class Archive>
inline void serialize(Archive & ar, hpp::manipulation::graph::StateWkPtr_t& s, const unsigned int version) inline void serialize(Archive & ar, hpp::manipulation::graph::StateWkPtr_t& s, const unsigned int version)
{ {
using namespace hpp::manipulation::graph; auto s_ = s.lock();
StatePtr_t s_ = s.lock();
serialize(ar, s_, version); serialize(ar, s_, version);
s = s_; s = s_;
} }
...@@ -126,18 +116,20 @@ template<class Archive> ...@@ -126,18 +116,20 @@ template<class Archive>
inline void load (Archive& ar, hpp::manipulation::DevicePtr_t& d, const unsigned int version) inline void load (Archive& ar, hpp::manipulation::DevicePtr_t& d, const unsigned int version)
{ {
load<Archive, hpp::manipulation::Device> (ar, d, version); load<Archive, hpp::manipulation::Device> (ar, d, version);
using hpp::serialization::archive_device_wrapper; auto* har = hpp::serialization::cast(&ar);
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar); if (d && har && har->contains(d->name()))
if (adw) d = boost::dynamic_pointer_cast<hpp::manipulation::Device>(adw->device); d = har->template getChildClass<hpp::pinocchio::Device, hpp::manipulation::Device>(d->name(), true)->self();
} }
template<class Archive> template<class Archive>
inline void load (Archive& ar, hpp::manipulation::DeviceWkPtr_t& d, const unsigned int version) inline void load (Archive& ar, hpp::manipulation::DeviceWkPtr_t& d, const unsigned int version)
{ {
load<Archive, hpp::manipulation::Device> (ar, d, version); load<Archive, hpp::manipulation::Device> (ar, d, version);
using hpp::serialization::archive_device_wrapper; auto* har = hpp::serialization::cast(&ar);
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar); auto dd = d.lock();
if (adw) d = boost::dynamic_pointer_cast<hpp::manipulation::Device>(adw->device); if (!dd) return;
if (har && har->contains(dd->name()))
d = har->template getChildClass<hpp::pinocchio::Device, hpp::manipulation::Device>(dd->name(), true)->self();
} }
} // namespace serialization } // namespace serialization
} // namespace boost } // namespace boost
......
...@@ -139,13 +139,16 @@ namespace hpp { ...@@ -139,13 +139,16 @@ namespace hpp {
template<class Archive> template<class Archive>
void Device::serialize(Archive & ar, const unsigned int version) void Device::serialize(Archive & ar, const unsigned int version)
{ {
using hpp::serialization::archive_device_wrapper;
using namespace boost::serialization; using namespace boost::serialization;
(void) version; (void) version;
auto* har = hpp::serialization::cast(&ar);
ar & make_nvp("base", base_object<pinocchio::HumanoidRobot>(*this)); ar & make_nvp("base", base_object<pinocchio::HumanoidRobot>(*this));
archive_device_wrapper* adw = dynamic_cast<archive_device_wrapper*>(&ar);
bool written = (adw == NULL); // TODO we should throw if a pinocchio::Device instance with name name_
// and not of type manipulation::Device is found.
bool written = (!har ||
har->template getChildClass<pinocchio::Device, Device>(name_, false) != this);
ar & BOOST_SERIALIZATION_NVP(written); ar & BOOST_SERIALIZATION_NVP(written);
if (written) { if (written) {
ar & BOOST_SERIALIZATION_NVP(self_); ar & BOOST_SERIALIZATION_NVP(self_);
......
...@@ -44,7 +44,7 @@ namespace hpp { ...@@ -44,7 +44,7 @@ namespace hpp {
GraphPtr_t Graph::create GraphPtr_t Graph::create
(const core::ProblemConstPtr_t& problem) (const core::ProblemConstPtr_t& problem)
{ {
assert(HPP_DYNAMIC_PTR_CASE (const Problem, problem)); assert(HPP_DYNAMIC_PTR_CAST (const Problem, problem));
ProblemConstPtr_t p = HPP_STATIC_PTR_CAST(const Problem, problem); ProblemConstPtr_t p = HPP_STATIC_PTR_CAST(const Problem, problem);
Graph* ptr = new Graph (p); Graph* ptr = new Graph (p);
GraphPtr_t shPtr (ptr); GraphPtr_t shPtr (ptr);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment