1 // Copyright 2020 Hans Dembinski
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)
7 //[ guide_histogram_in_api
9 #include <boost/histogram.hpp>
12 // function accepts any histogram and returns a copy
13 template <class Axes
, class Storage
>
14 boost::histogram::histogram
<Axes
, Storage
> any_histogram(
15 boost::histogram::histogram
<Axes
, Storage
>& h
) {
19 // function only accepts histograms with fixed axis types and returns a copy
20 template <class Storage
, class... Axes
>
21 boost::histogram::histogram
<std::tuple
<Axes
...>, Storage
> only_static_histogram(
22 boost::histogram::histogram
<std::tuple
<Axes
...>, Storage
>& h
) {
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
...>>,
30 only_dynamic_histogram(
31 boost::histogram::histogram
<std::vector
<boost::histogram::axis::variant
<Axes
...>>,
37 using namespace boost::histogram
;
39 auto histogram_with_static_axes
= make_histogram(axis::regular
<>(10, 0, 1));
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
);
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
);
53 // does not compile: only_static_histogram(histogram_with_dynamic_axes)
54 // does not compile: only_dynamic_histogram(histogram_with_static_axes)