guide_custom_modified_axis.cpp 1.2 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_custom_modified_axis
  7. #include <boost/histogram.hpp>
  8. #include <cassert>
  9. #include <iostream>
  10. #include <sstream>
  11. int main() {
  12. using namespace boost::histogram;
  13. // custom axis, which adapts builtin integer axis
  14. struct custom_axis : public axis::integer<> {
  15. using value_type = const char*; // type that is fed to the axis
  16. using integer::integer; // inherit ctors of base
  17. // the customization point
  18. // - accept const char* and convert to int
  19. // - then call index method of base class
  20. axis::index_type index(value_type s) const { return integer::index(std::atoi(s)); }
  21. };
  22. auto h = make_histogram(custom_axis(3, 6));
  23. h("-10");
  24. h("3");
  25. h("4");
  26. h("9");
  27. std::ostringstream os;
  28. for (auto&& b : indexed(h)) {
  29. os << "bin " << b.index() << " [" << b.bin() << "] " << *b << "\n";
  30. }
  31. std::cout << os.str() << std::endl;
  32. assert(os.str() == "bin 0 [3] 1\n"
  33. "bin 1 [4] 1\n"
  34. "bin 2 [5] 0\n");
  35. }
  36. //]