1 // Copyright 2019 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #ifndef BOOST_HISTOGRAM_TEST_UTILITY_SERIALIZATION_HPP
8 #define BOOST_HISTOGRAM_TEST_UTILITY_SERIALIZATION_HPP
9
10 #include <boost/archive/xml_iarchive.hpp>
11 #include <boost/archive/xml_oarchive.hpp>
12 #include <boost/config.hpp> // BOOST_WINDOWS
13 #include <boost/core/nvp.hpp>
14 #include <cassert>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18
join(const char * a,const char * b)19 std::string join(const char* a, const char* b) {
20 std::string filename = a;
21 filename +=
22 #ifdef BOOST_WINDOWS
23 "\\";
24 #else
25 "/";
26 #endif
27 filename += b;
28 return filename;
29 }
30
31 template <class T>
load_xml(const std::string & filename,T & t)32 void load_xml(const std::string& filename, T& t) {
33 std::ifstream ifs(filename);
34 assert(ifs.is_open());
35 // manually skip XML comments at the beginning of the stream, because of
36 // https://github.com/boostorg/serialization/issues/169
37 char line[128];
38 do {
39 ifs.getline(line, 128);
40 assert(std::strlen(line) < 127);
41 } while (!ifs.fail() && !ifs.eof() && std::strstr(line, "-->") == nullptr);
42 boost::archive::xml_iarchive ia(ifs);
43 ia >> boost::make_nvp("item", t);
44 }
45
46 template <class T>
print_xml(const std::string & filename,const T & t)47 void print_xml(const std::string& filename, const T& t) {
48 std::cout << filename << "\n";
49 boost::archive::xml_oarchive oa(std::cout);
50 oa << boost::make_nvp("item", t);
51 std::cout << std::flush;
52 }
53
54 #endif
55