• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_in_api
8 
9 #include <boost/histogram.hpp>
10 #include <cassert>
11 
12 // function accepts any histogram and returns a copy
13 template <class Axes, class Storage>
any_histogram(boost::histogram::histogram<Axes,Storage> & h)14 boost::histogram::histogram<Axes, Storage> any_histogram(
15     boost::histogram::histogram<Axes, Storage>& h) {
16   return h;
17 }
18 
19 // function only accepts histograms with fixed axis types and returns a copy
20 template <class Storage, class... Axes>
only_static_histogram(boost::histogram::histogram<std::tuple<Axes...>,Storage> & h)21 boost::histogram::histogram<std::tuple<Axes...>, Storage> only_static_histogram(
22     boost::histogram::histogram<std::tuple<Axes...>, Storage>& h) {
23   return h;
24 }
25 
26 // function only accepts histograms with dynamic axis types and returns a copy
27 template <class Storage, class... Axes>
28 boost::histogram::histogram<std::vector<boost::histogram::axis::variant<Axes...>>,
29                             Storage>
only_dynamic_histogram(boost::histogram::histogram<std::vector<boost::histogram::axis::variant<Axes...>>,Storage> & h)30 only_dynamic_histogram(
31     boost::histogram::histogram<std::vector<boost::histogram::axis::variant<Axes...>>,
32                                 Storage>& h) {
33   return h;
34 }
35 
main()36 int main() {
37   using namespace boost::histogram;
38 
39   auto histogram_with_static_axes = make_histogram(axis::regular<>(10, 0, 1));
40 
41   using axis_variant = axis::variant<axis::regular<>, axis::integer<>>;
42   std::vector<axis_variant> axes;
43   axes.emplace_back(axis::regular<>(5, 0, 1));
44   axes.emplace_back(axis::integer<>(0, 1));
45   auto histogram_with_dynamic_axes = make_histogram(axes);
46 
47   assert(any_histogram(histogram_with_static_axes) == histogram_with_static_axes);
48   assert(any_histogram(histogram_with_dynamic_axes) == histogram_with_dynamic_axes);
49   assert(only_static_histogram(histogram_with_static_axes) == histogram_with_static_axes);
50   assert(only_dynamic_histogram(histogram_with_dynamic_axes) ==
51          histogram_with_dynamic_axes);
52 
53   // does not compile: only_static_histogram(histogram_with_dynamic_axes)
54   // does not compile: only_dynamic_histogram(histogram_with_static_axes)
55 }
56 
57 //]
58