guide_histogram_serialization.cpp 1.1 KB

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