26 #ifndef TESSERACT_COMMON_SERIALIZATION_H
27 #define TESSERACT_COMMON_SERIALIZATION_H
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>
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);
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); \
58 template void boost::serialization::serialize( \
59 boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); \
60 template void boost::serialization::serialize( \
61 boost::archive::binary_oarchive& ar, Type& g, const unsigned int version); \
62 template void boost::serialization::serialize( \
63 boost::archive::binary_iarchive& ar, Type& g, const unsigned int version);
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);
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); \
80 template void boost::serialization::serialize( \
81 boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); \
82 template void boost::serialization::serialize( \
83 boost::archive::binary_oarchive& ar, Type& g, const unsigned int version); \
84 template void boost::serialization::serialize( \
85 boost::archive::binary_iarchive& ar, Type& g, const unsigned int version); \
86 template void boost::serialization::save( \
87 boost::archive::xml_oarchive&, const Type& g, const unsigned int version); \
88 template void boost::serialization::load( \
89 boost::archive::xml_iarchive& ar, Type& g, const unsigned int version); \
90 template void boost::serialization::save( \
91 boost::archive::binary_oarchive&, const Type& g, const unsigned int version); \
92 template void boost::serialization::load( \
93 boost::archive::binary_iarchive& ar, Type& g, const unsigned int version);
99 template <
typename SerializableType>
100 static std::string
toArchiveStringXML(
const SerializableType& archive_type,
const std::string& name =
"")
102 std::stringstream ss;
104 boost::archive::xml_oarchive oa(ss);
109 oa << boost::serialization::make_nvp<SerializableType>(
"archive_type",
110 const_cast<SerializableType&
>(archive_type));
112 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
113 const_cast<SerializableType&
>(archive_type));
119 template <
typename SerializableType>
121 const std::string& file_path,
122 const std::string& name =
"")
124 std::filesystem::path fp(file_path);
125 if (!fp.has_extension())
128 std::ofstream os(fp.string());
130 boost::archive::xml_oarchive oa(os);
134 oa << boost::serialization::make_nvp<SerializableType>(
"archive_type",
135 const_cast<SerializableType&
>(archive_type));
137 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
138 const_cast<SerializableType&
>(archive_type));
144 template <
typename SerializableType>
146 const std::string& file_path,
147 const std::string& name =
"")
149 std::filesystem::path fp(file_path);
150 if (!fp.has_extension())
153 std::ofstream os(fp.string(), std::ios_base::binary);
155 boost::archive::binary_oarchive oa(os);
159 oa << boost::serialization::make_nvp<SerializableType>(
"archive_type",
160 const_cast<SerializableType&
>(archive_type));
162 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
163 const_cast<SerializableType&
>(archive_type));
169 template <
typename SerializableType>
171 const std::string& file_path,
172 const std::string& name =
"")
174 std::filesystem::path fp(file_path);
176 return toArchiveFileBinary<SerializableType>(archive_type, file_path, name);
178 return toArchiveFileXML<SerializableType>(archive_type, file_path, name);
181 template <
typename SerializableType>
183 const std::string& name =
"")
185 std::stringstream ss;
187 boost::archive::binary_oarchive oa(ss);
192 oa << boost::serialization::make_nvp<SerializableType>(
"archive_type",
193 const_cast<SerializableType&
>(archive_type));
195 oa << boost::serialization::make_nvp<SerializableType>(name.c_str(),
196 const_cast<SerializableType&
>(archive_type));
199 std::string data = ss.str();
200 return { data.begin(), data.end() };
203 template <
typename SerializableType>
206 SerializableType archive_type;
209 std::stringstream ss(archive_xml);
210 boost::archive::xml_iarchive ia(ss);
211 ia >> BOOST_SERIALIZATION_NVP(archive_type);
217 template <
typename SerializableType>
220 SerializableType archive_type;
223 std::ifstream ifs(file_path);
225 boost::archive::xml_iarchive ia(ifs);
226 ia >> BOOST_SERIALIZATION_NVP(archive_type);
232 template <
typename SerializableType>
235 SerializableType archive_type;
238 std::ifstream ifs(file_path, std::ios_base::binary);
240 boost::archive::binary_iarchive ia(ifs);
241 ia >> BOOST_SERIALIZATION_NVP(archive_type);
247 template <
typename SerializableType>
250 std::filesystem::path fp(file_path);
252 return fromArchiveFileBinary<SerializableType>(file_path);
254 return fromArchiveFileXML<SerializableType>(file_path);
257 template <
typename SerializableType>
260 SerializableType archive_type;
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);
273 #endif // TESSERACT_COMMON_SERIALIZATION_H