Tesseract
Motion Planning Environment
Loading...
Searching...
No Matches
serialization.h
Go to the documentation of this file.
1
26#ifndef TESSERACT_COMMON_SERIALIZATION_H
27#define TESSERACT_COMMON_SERIALIZATION_H
28
31#include <variant>
32#include <Eigen/Dense>
33#include <fstream>
34#include <sstream>
35#include <boost/archive/xml_oarchive.hpp>
36#include <boost/archive/xml_iarchive.hpp>
37#include <boost/archive/binary_oarchive.hpp>
38#include <boost/archive/binary_iarchive.hpp>
39#include <boost/serialization/tracking.hpp>
40#include <boost/serialization/tracking_enum.hpp>
42
45
46// Used to replace commas in these macros to avoid them being interpreted as multiple arguments
47// Example: TESSERACT_SERIALIZE_SAVE_LOAD_FREE_ARCHIVES_INSTANTIATE(std::variant<std::string COMMA Eigen::Isometry3d>)
48#define COMMA ,
49
50// Use this macro for serialization defined using the invasive method inside the class
51#define TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(Type) \
52 template void Type::serialize(boost::archive::xml_oarchive& ar, const unsigned int version); \
53 template void Type::serialize(boost::archive::xml_iarchive& ar, const unsigned int version); \
54 template void Type::serialize(boost::archive::binary_oarchive& ar, const unsigned int version); \
55 template void Type::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
56
57// Use this macro for serialization defined using the invasive method inside the class with custom load/save functions
58#define TESSERACT_SERIALIZE_SAVE_LOAD_ARCHIVES_INSTANTIATE(Type) \
59 template void Type::serialize(boost::archive::xml_oarchive& ar, const unsigned int version); \
60 template void Type::serialize(boost::archive::xml_iarchive& ar, const unsigned int version); \
61 template void Type::serialize(boost::archive::binary_oarchive& ar, const unsigned int version); \
62 template void Type::serialize(boost::archive::binary_iarchive& ar, const unsigned int version); \
63 template void Type::save(boost::archive::xml_oarchive&, const unsigned int version) const; \
64 template void Type::load(boost::archive::xml_iarchive& ar, const unsigned int version); \
65 template void Type::save(boost::archive::binary_oarchive&, const unsigned int version) const; \
66 template void Type::load(boost::archive::binary_iarchive& ar, const unsigned int version);
67
68// Use this macro for serialization defined using the non-invasive free function method outside the class
69#define TESSERACT_SERIALIZE_SAVE_LOAD_FREE_ARCHIVES_INSTANTIATE(Type) \
70 template void boost::serialization::serialize( \
71 boost::archive::xml_oarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
72 template void boost::serialization::serialize( \
73 boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
74 template void boost::serialization::serialize( \
75 boost::archive::binary_oarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
76 template void boost::serialization::serialize( \
77 boost::archive::binary_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
78 template void boost::serialization::save( \
79 boost::archive::xml_oarchive&, const Type& g, const unsigned int version); /* NOLINT */ \
80 template void boost::serialization::load( \
81 boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
82 template void boost::serialization::save( \
83 boost::archive::binary_oarchive&, const Type& g, const unsigned int version); /* NOLINT */ \
84 template void boost::serialization::load( \
85 boost::archive::binary_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */
86
87namespace tesseract_common
88{
90{
91 template <typename SerializableType>
92 static std::string toArchiveStringXML(const SerializableType& archive_type, const std::string& name = "")
93 {
94 std::stringstream ss;
95 { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
96 boost::archive::xml_oarchive oa(ss);
97
98 // Boost uses the same function for serialization and deserialization so it requires a non-const reference
99 // Because we are only serializing here it is safe to cast away const
100 if (name.empty())
101 oa << boost::serialization::make_nvp<SerializableType>("archive_type",
102 const_cast<SerializableType&>(archive_type)); // NOLINT
103 else
104 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
105 const_cast<SerializableType&>(archive_type)); // NOLINT
106 }
107
108 return ss.str();
109 }
110
111 template <typename SerializableType>
112 static bool toArchiveFileXML(const SerializableType& archive_type,
113 const std::string& file_path,
114 const std::string& name = "")
115 {
116 fs::path fp(file_path);
117 if (!fp.has_extension())
119
120 std::ofstream os(fp.string());
121 { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
122 boost::archive::xml_oarchive oa(os);
123 // Boost uses the same function for serialization and deserialization so it requires a non-const reference
124 // Because we are only serializing here it is safe to cast away const
125 if (name.empty())
126 oa << boost::serialization::make_nvp<SerializableType>("archive_type",
127 const_cast<SerializableType&>(archive_type)); // NOLINT
128 else
129 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
130 const_cast<SerializableType&>(archive_type)); // NOLINT
131 }
132
133 return true;
134 }
135
136 template <typename SerializableType>
137 static bool toArchiveFileBinary(const SerializableType& archive_type,
138 const std::string& file_path,
139 const std::string& name = "")
140 {
141 fs::path fp(file_path);
142 if (!fp.has_extension())
144
145 std::ofstream os(fp.string(), std::ios_base::binary);
146 { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
147 boost::archive::binary_oarchive oa(os);
148 // Boost uses the same function for serialization and deserialization so it requires a non-const reference
149 // Because we are only serializing here it is safe to cast away const
150 if (name.empty())
151 oa << boost::serialization::make_nvp<SerializableType>("archive_type",
152 const_cast<SerializableType&>(archive_type)); // NOLINT
153 else
154 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
155 const_cast<SerializableType&>(archive_type)); // NOLINT
156 }
157
158 return true;
159 }
160
161 template <typename SerializableType>
162 static SerializableType fromArchiveStringXML(const std::string& archive_xml)
163 {
164 SerializableType archive_type;
165
166 { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
167 std::stringstream ss(archive_xml);
168 boost::archive::xml_iarchive ia(ss);
169 ia >> BOOST_SERIALIZATION_NVP(archive_type);
170 }
171
172 return archive_type;
173 }
174
175 template <typename SerializableType>
176 static SerializableType fromArchiveFileXML(const std::string& file_path)
177 {
178 SerializableType archive_type;
179
180 { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
181 std::ifstream ifs(file_path);
182 assert(ifs.good());
183 boost::archive::xml_iarchive ia(ifs);
184 ia >> BOOST_SERIALIZATION_NVP(archive_type);
185 }
186
187 return archive_type;
188 }
189
190 template <typename SerializableType>
191 static SerializableType fromArchiveFileBinary(const std::string& file_path)
192 {
193 SerializableType archive_type;
194
195 { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
196 std::ifstream ifs(file_path, std::ios_base::binary);
197 assert(ifs.good());
198 boost::archive::binary_iarchive ia(ifs);
199 ia >> BOOST_SERIALIZATION_NVP(archive_type);
200 }
201
202 return archive_type;
203 }
204};
205} // namespace tesseract_common
206#endif // TESSERACT_COMMON_SERIALIZATION_H
Common Tesseract Macros.
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
Definition: create_convex_hull.cpp:36
Definition: allowed_collision_matrix.h:16
tesseract_common::fs::path file_path(__FILE__)
Boost serialization class extension macros and helpers.
Definition: serialization.h:90
static bool toArchiveFileXML(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:112
static SerializableType fromArchiveFileBinary(const std::string &file_path)
Definition: serialization.h:191
static SerializableType fromArchiveStringXML(const std::string &archive_xml)
Definition: serialization.h:162
static std::string toArchiveStringXML(const SerializableType &archive_type, const std::string &name="")
Definition: serialization.h:92
static SerializableType fromArchiveFileXML(const std::string &file_path)
Definition: serialization.h:176
static bool toArchiveFileBinary(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:137
Definition: serialization_extensions.h:61
Definition: serialization_extensions.h:43
Common Tesseract Types.
oa<< BOOST_SERIALIZATION_NVP(any_type);} tesseract_common::AnyPoly nany_type;{ std::ifstream ifs(tesseract_common::getTempPath()+"any_type_boost.xml");assert(ifs.good());boost::archive::xml_iarchive ia(ifs);ia > BOOST_SERIALIZATION_NVP(nany_type)
boost::archive::xml_oarchive oa(os)