• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2018 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 //[ guide_histogram_serialization
8 
9 #include <boost/archive/text_iarchive.hpp>
10 #include <boost/archive/text_oarchive.hpp>
11 #include <boost/histogram.hpp>
12 #include <boost/histogram/serialization.hpp> // includes serialization code
13 #include <cassert>
14 #include <sstream>
15 
main()16 int main() {
17   using namespace boost::histogram;
18 
19   auto a = make_histogram(axis::regular<>(3, -1.0, 1.0, "axis 0"),
20                           axis::integer<>(0, 2, "axis 1"));
21   a(0.5, 1);
22 
23   std::string buf; // to hold persistent representation
24 
25   // store histogram
26   {
27     std::ostringstream os;
28     boost::archive::text_oarchive oa(os);
29     oa << a;
30     buf = os.str();
31   }
32 
33   auto b = decltype(a)(); // create a default-constructed second histogram
34 
35   assert(b != a); // b is empty, a is not
36 
37   // load histogram
38   {
39     std::istringstream is(buf);
40     boost::archive::text_iarchive ia(is);
41     ia >> b;
42   }
43 
44   assert(b == a); // now b is equal to a
45 }
46 
47 //]
48