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 <fstream>
32 #include <sstream>
33 #include <boost/archive/xml_oarchive.hpp>
34 #include <boost/archive/xml_iarchive.hpp>
35 #include <boost/archive/binary_oarchive.hpp>
36 #include <boost/archive/binary_iarchive.hpp>
37 #include <boost/serialization/tracking.hpp>
38 #include <boost/serialization/tracking_enum.hpp>
40 
41 #include <filesystem>
43 
44 // Used to replace commas in these macros to avoid them being interpreted as multiple arguments
45 // Example: TESSERACT_SERIALIZE_SAVE_LOAD_FREE_ARCHIVES_INSTANTIATE(std::variant<std::string COMMA Eigen::Isometry3d>)
46 #define COMMA ,
47 
48 // Use this macro for serialization defined using the invasive method inside the class
49 #define TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(Type) \
50  template void Type::serialize(boost::archive::xml_oarchive& ar, const unsigned int version); \
51  template void Type::serialize(boost::archive::xml_iarchive& ar, const unsigned int version); \
52  template void Type::serialize(boost::archive::binary_oarchive& ar, const unsigned int version); \
53  template void Type::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
54 
55 #define TESSERACT_SERIALIZE_FREE_ARCHIVES_INSTANTIATE(Type) \
56  template void boost::serialization::serialize( \
57  boost::archive::xml_oarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
58  template void boost::serialization::serialize( \
59  boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
60  template void boost::serialization::serialize( \
61  boost::archive::binary_oarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
62  template void boost::serialization::serialize( \
63  boost::archive::binary_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */
64 
65 // Use this macro for serialization defined using the invasive method inside the class with custom load/save functions
66 #define TESSERACT_SERIALIZE_SAVE_LOAD_ARCHIVES_INSTANTIATE(Type) \
67  template void Type::serialize(boost::archive::xml_oarchive& ar, const unsigned int version); \
68  template void Type::serialize(boost::archive::xml_iarchive& ar, const unsigned int version); \
69  template void Type::serialize(boost::archive::binary_oarchive& ar, const unsigned int version); \
70  template void Type::serialize(boost::archive::binary_iarchive& ar, const unsigned int version); \
71  template void Type::save(boost::archive::xml_oarchive&, const unsigned int version) const; \
72  template void Type::load(boost::archive::xml_iarchive& ar, const unsigned int version); \
73  template void Type::save(boost::archive::binary_oarchive&, const unsigned int version) const; \
74  template void Type::load(boost::archive::binary_iarchive& ar, const unsigned int version);
75 
76 // Use this macro for serialization defined using the non-invasive free function method outside the class
77 #define TESSERACT_SERIALIZE_SAVE_LOAD_FREE_ARCHIVES_INSTANTIATE(Type) \
78  template void boost::serialization::serialize( \
79  boost::archive::xml_oarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
80  template void boost::serialization::serialize( \
81  boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
82  template void boost::serialization::serialize( \
83  boost::archive::binary_oarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
84  template void boost::serialization::serialize( \
85  boost::archive::binary_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
86  template void boost::serialization::save( \
87  boost::archive::xml_oarchive&, const Type& g, const unsigned int version); /* NOLINT */ \
88  template void boost::serialization::load( \
89  boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */ \
90  template void boost::serialization::save( \
91  boost::archive::binary_oarchive&, const Type& g, const unsigned int version); /* NOLINT */ \
92  template void boost::serialization::load( \
93  boost::archive::binary_iarchive& ar, Type& g, const unsigned int version); /* NOLINT */
94 
95 namespace tesseract_common
96 {
98 {
99  template <typename SerializableType>
100  static std::string toArchiveStringXML(const SerializableType& archive_type, const std::string& name = "")
101  {
102  std::stringstream ss;
103  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
104  boost::archive::xml_oarchive oa(ss);
105 
106  // Boost uses the same function for serialization and deserialization so it requires a non-const reference
107  // Because we are only serializing here it is safe to cast away const
108  if (name.empty())
109  oa << boost::serialization::make_nvp<SerializableType>("archive_type",
110  const_cast<SerializableType&>(archive_type)); // NOLINT
111  else
112  oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
113  const_cast<SerializableType&>(archive_type)); // NOLINT
114  }
115 
116  return ss.str();
117  }
118 
119  template <typename SerializableType>
120  static bool toArchiveFileXML(const SerializableType& archive_type,
121  const std::string& file_path,
122  const std::string& name = "")
123  {
124  std::filesystem::path fp(file_path);
125  if (!fp.has_extension())
126  fp = std::filesystem::path(file_path + serialization::xml::extension<SerializableType>::value);
127 
128  std::ofstream os(fp.string());
129  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
130  boost::archive::xml_oarchive oa(os);
131  // Boost uses the same function for serialization and deserialization so it requires a non-const reference
132  // Because we are only serializing here it is safe to cast away const
133  if (name.empty())
134  oa << boost::serialization::make_nvp<SerializableType>("archive_type",
135  const_cast<SerializableType&>(archive_type)); // NOLINT
136  else
137  oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
138  const_cast<SerializableType&>(archive_type)); // NOLINT
139  }
140 
141  return true;
142  }
143 
144  template <typename SerializableType>
145  static bool toArchiveFileBinary(const SerializableType& archive_type,
146  const std::string& file_path,
147  const std::string& name = "")
148  {
149  std::filesystem::path fp(file_path);
150  if (!fp.has_extension())
151  fp = std::filesystem::path(file_path + serialization::binary::extension<SerializableType>::value);
152 
153  std::ofstream os(fp.string(), std::ios_base::binary);
154  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
155  boost::archive::binary_oarchive oa(os);
156  // Boost uses the same function for serialization and deserialization so it requires a non-const reference
157  // Because we are only serializing here it is safe to cast away const
158  if (name.empty())
159  oa << boost::serialization::make_nvp<SerializableType>("archive_type",
160  const_cast<SerializableType&>(archive_type)); // NOLINT
161  else
162  oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
163  const_cast<SerializableType&>(archive_type)); // NOLINT
164  }
165 
166  return true;
167  }
168 
169  template <typename SerializableType>
170  static bool toArchiveFile(const SerializableType& archive_type,
171  const std::string& file_path,
172  const std::string& name = "")
173  {
174  std::filesystem::path fp(file_path);
176  return toArchiveFileBinary<SerializableType>(archive_type, file_path, name);
177 
178  return toArchiveFileXML<SerializableType>(archive_type, file_path, name);
179  }
180 
181  template <typename SerializableType>
182  static std::vector<std::uint8_t> toArchiveBinaryData(const SerializableType& archive_type,
183  const std::string& name = "")
184  {
185  std::stringstream ss;
186  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
187  boost::archive::binary_oarchive oa(ss);
188 
189  // Boost uses the same function for serialization and deserialization so it requires a non-const reference
190  // Because we are only serializing here it is safe to cast away const
191  if (name.empty())
192  oa << boost::serialization::make_nvp<SerializableType>("archive_type",
193  const_cast<SerializableType&>(archive_type)); // NOLINT
194  else
195  oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
196  const_cast<SerializableType&>(archive_type)); // NOLINT
197  }
198 
199  std::string data = ss.str();
200  return { data.begin(), data.end() };
201  }
202 
203  template <typename SerializableType>
204  static SerializableType fromArchiveStringXML(const std::string& archive_xml)
205  {
206  SerializableType archive_type;
207 
208  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
209  std::stringstream ss(archive_xml);
210  boost::archive::xml_iarchive ia(ss);
211  ia >> BOOST_SERIALIZATION_NVP(archive_type);
212  }
213 
214  return archive_type;
215  }
216 
217  template <typename SerializableType>
218  static SerializableType fromArchiveFileXML(const std::string& file_path)
219  {
220  SerializableType archive_type;
221 
222  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
223  std::ifstream ifs(file_path);
224  assert(ifs.good());
225  boost::archive::xml_iarchive ia(ifs);
226  ia >> BOOST_SERIALIZATION_NVP(archive_type);
227  }
228 
229  return archive_type;
230  }
231 
232  template <typename SerializableType>
233  static SerializableType fromArchiveFileBinary(const std::string& file_path)
234  {
235  SerializableType archive_type;
236 
237  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
238  std::ifstream ifs(file_path, std::ios_base::binary);
239  assert(ifs.good());
240  boost::archive::binary_iarchive ia(ifs);
241  ia >> BOOST_SERIALIZATION_NVP(archive_type);
242  }
243 
244  return archive_type;
245  }
246 
247  template <typename SerializableType>
248  static SerializableType fromArchiveFile(const std::string& file_path)
249  {
250  std::filesystem::path fp(file_path);
252  return fromArchiveFileBinary<SerializableType>(file_path);
253 
254  return fromArchiveFileXML<SerializableType>(file_path);
255  }
256 
257  template <typename SerializableType>
258  static SerializableType fromArchiveBinaryData(const std::vector<std::uint8_t>& archive_binary)
259  {
260  SerializableType archive_type;
261 
262  { // Must be scoped because all data is not written until the oost::archive::xml_oarchive goes out of scope
263  std::stringstream ss;
264  std::copy(archive_binary.begin(), archive_binary.end(), std::ostreambuf_iterator<char>(ss));
265  boost::archive::binary_iarchive ia(ss);
266  ia >> BOOST_SERIALIZATION_NVP(archive_type);
267  }
268 
269  return archive_type;
270  }
271 };
272 } // namespace tesseract_common
273 #endif // TESSERACT_COMMON_SERIALIZATION_H
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::serialization::xml::extension
Definition: serialization_extensions.h:42
tesseract_common::Serialization::toArchiveFileXML
static bool toArchiveFileXML(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:120
macros.h
Common Tesseract Macros.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
tesseract_common::Serialization::fromArchiveFileBinary
static SerializableType fromArchiveFileBinary(const std::string &file_path)
Definition: serialization.h:233
tesseract_common::Serialization::fromArchiveFile
static SerializableType fromArchiveFile(const std::string &file_path)
Definition: serialization.h:248
tesseract_common::Serialization
Definition: serialization.h:97
tesseract_common::serialization::binary::extension
Definition: serialization_extensions.h:61
tesseract_common::Serialization::toArchiveStringXML
static std::string toArchiveStringXML(const SerializableType &archive_type, const std::string &name="")
Definition: serialization.h:100
tesseract_common::Serialization::toArchiveFile
static bool toArchiveFile(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:170
tesseract_common::Serialization::toArchiveFileBinary
static bool toArchiveFileBinary(const SerializableType &archive_type, const std::string &file_path, const std::string &name="")
Definition: serialization.h:145
tesseract_common::Serialization::fromArchiveStringXML
static SerializableType fromArchiveStringXML(const std::string &archive_xml)
Definition: serialization.h:204
tesseract_common::Serialization::toArchiveBinaryData
static std::vector< std::uint8_t > toArchiveBinaryData(const SerializableType &archive_type, const std::string &name="")
Definition: serialization.h:182
tesseract_common::Serialization::fromArchiveFileXML
static SerializableType fromArchiveFileXML(const std::string &file_path)
Definition: serialization.h:218
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
tesseract_common::Serialization::fromArchiveBinaryData
static SerializableType fromArchiveBinaryData(const std::vector< std::uint8_t > &archive_binary)
Definition: serialization.h:258
serialization_extensions.h
Boost serialization class extension macros and helpers.


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40