variant.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright 2015-2019 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. #ifndef BOOST_HISTOGRAM_AXIS_VARIANT_HPP
  7. #define BOOST_HISTOGRAM_AXIS_VARIANT_HPP
  8. #include <boost/core/nvp.hpp>
  9. #include <boost/histogram/axis/iterator.hpp>
  10. #include <boost/histogram/axis/polymorphic_bin.hpp>
  11. #include <boost/histogram/axis/traits.hpp>
  12. #include <boost/histogram/detail/relaxed_equal.hpp>
  13. #include <boost/histogram/detail/static_if.hpp>
  14. #include <boost/histogram/detail/type_name.hpp>
  15. #include <boost/variant2/variant.hpp>
  16. #include <boost/histogram/detail/variant_proxy.hpp>
  17. #include <boost/mp11/algorithm.hpp> // mp_contains
  18. #include <boost/mp11/list.hpp> // mp_first
  19. #include <boost/throw_exception.hpp>
  20. #include <stdexcept>
  21. #include <type_traits>
  22. #include <utility>
  23. namespace boost {
  24. namespace histogram {
  25. namespace axis {
  26. /// Polymorphic axis type
  27. template <class... Ts>
  28. class variant : public iterator_mixin<variant<Ts...>> {
  29. using impl_type = boost::variant2::variant<Ts...>;
  30. template <class T>
  31. using is_bounded_type = mp11::mp_contains<variant, std::decay_t<T>>;
  32. template <class T>
  33. using requires_bounded_type = std::enable_if_t<is_bounded_type<T>::value>;
  34. // maybe metadata_type or const metadata_type, if bounded type is const
  35. using metadata_type = std::remove_reference_t<decltype(
  36. traits::metadata(std::declval<std::remove_pointer_t<mp11::mp_first<variant>>>()))>;
  37. public:
  38. // cannot import ctors with using directive, it breaks gcc and msvc
  39. variant() = default;
  40. variant(const variant&) = default;
  41. variant& operator=(const variant&) = default;
  42. variant(variant&&) = default;
  43. variant& operator=(variant&&) = default;
  44. template <class T, class = requires_bounded_type<T>>
  45. variant(T&& t) : impl(std::forward<T>(t)) {}
  46. template <class T, class = requires_bounded_type<T>>
  47. variant& operator=(T&& t) {
  48. impl = std::forward<T>(t);
  49. return *this;
  50. }
  51. template <class... Us>
  52. variant(const variant<Us...>& u) {
  53. this->operator=(u);
  54. }
  55. template <class... Us>
  56. variant& operator=(const variant<Us...>& u) {
  57. visit(
  58. [this](const auto& u) {
  59. using U = std::decay_t<decltype(u)>;
  60. detail::static_if<is_bounded_type<U>>(
  61. [this](const auto& u) { this->operator=(u); },
  62. [](const auto&) {
  63. BOOST_THROW_EXCEPTION(std::runtime_error(
  64. detail::type_name<U>() + " is not convertible to a bounded type of " +
  65. detail::type_name<variant>()));
  66. },
  67. u);
  68. },
  69. u);
  70. return *this;
  71. }
  72. /// Return size of axis.
  73. index_type size() const {
  74. return visit([](const auto& a) -> index_type { return a.size(); }, *this);
  75. }
  76. /// Return options of axis or option::none_t if axis has no options.
  77. unsigned options() const {
  78. return visit([](const auto& a) { return traits::options(a); }, *this);
  79. }
  80. /// Returns true if the axis is inclusive or false.
  81. bool inclusive() const {
  82. return visit([](const auto& a) { return traits::inclusive(a); }, *this);
  83. }
  84. /// Returns true if the axis is ordered or false.
  85. bool ordered() const {
  86. return visit([](const auto& a) { return traits::ordered(a); }, *this);
  87. }
  88. /// Return reference to const metadata or instance of null_type if axis has no
  89. /// metadata.
  90. const metadata_type& metadata() const {
  91. return visit(
  92. [](const auto& a) -> const metadata_type& {
  93. using M = decltype(traits::metadata(a));
  94. return detail::static_if<std::is_same<M, const metadata_type&>>(
  95. [](const auto& a) -> const metadata_type& { return traits::metadata(a); },
  96. [](const auto&) -> const metadata_type& {
  97. BOOST_THROW_EXCEPTION(std::runtime_error(
  98. "cannot return metadata of type " + detail::type_name<M>() +
  99. " through axis::variant interface which uses type " +
  100. detail::type_name<metadata_type>() +
  101. "; use boost::histogram::axis::get to obtain a reference "
  102. "of this axis type"));
  103. },
  104. a);
  105. },
  106. *this);
  107. }
  108. /// Return reference to metadata or instance of null_type if axis has no
  109. /// metadata.
  110. metadata_type& metadata() {
  111. return visit(
  112. [](auto& a) -> metadata_type& {
  113. using M = decltype(traits::metadata(a));
  114. return detail::static_if<std::is_same<M, metadata_type&>>(
  115. [](auto& a) -> metadata_type& { return traits::metadata(a); },
  116. [](auto&) -> metadata_type& {
  117. BOOST_THROW_EXCEPTION(std::runtime_error(
  118. "cannot return metadata of type " + detail::type_name<M>() +
  119. " through axis::variant interface which uses type " +
  120. detail::type_name<metadata_type>() +
  121. "; use boost::histogram::axis::get to obtain a reference "
  122. "of this axis type"));
  123. },
  124. a);
  125. },
  126. *this);
  127. }
  128. /** Return index for value argument.
  129. Throws std::invalid_argument if axis has incompatible call signature.
  130. */
  131. template <class U>
  132. index_type index(const U& u) const {
  133. return visit([&u](const auto& a) { return traits::index(a, u); }, *this);
  134. }
  135. /** Return value for index argument.
  136. Only works for axes with value method that returns something convertible
  137. to double and will throw a runtime_error otherwise, see
  138. axis::traits::value().
  139. */
  140. double value(real_index_type idx) const {
  141. return visit([idx](const auto& a) { return traits::value_as<double>(a, idx); },
  142. *this);
  143. }
  144. /** Return bin for index argument.
  145. Only works for axes with value method that returns something convertible
  146. to double and will throw a runtime_error otherwise, see
  147. axis::traits::value().
  148. */
  149. auto bin(index_type idx) const {
  150. return visit(
  151. [idx](const auto& a) {
  152. return detail::value_method_switch(
  153. [idx](const auto& a) { // axis is discrete
  154. const double x = traits::value_as<double>(a, idx);
  155. return polymorphic_bin<double>(x, x);
  156. },
  157. [idx](const auto& a) { // axis is continuous
  158. const double x1 = traits::value_as<double>(a, idx);
  159. const double x2 = traits::value_as<double>(a, idx + 1);
  160. return polymorphic_bin<double>(x1, x2);
  161. },
  162. a, detail::priority<1>{});
  163. },
  164. *this);
  165. }
  166. /** Compare two variants.
  167. Return true if the variants point to the same concrete axis type and the types compare
  168. equal. Otherwise return false.
  169. */
  170. template <class... Us>
  171. bool operator==(const variant<Us...>& u) const {
  172. return visit([&u](const auto& x) { return u == x; }, *this);
  173. }
  174. /** Compare variant with a concrete axis type.
  175. Return true if the variant point to the same concrete axis type and the types compare
  176. equal. Otherwise return false.
  177. */
  178. template <class T>
  179. bool operator==(const T& t) const {
  180. return detail::static_if_c<(mp11::mp_contains<impl_type, T>::value ||
  181. mp11::mp_contains<impl_type, T*>::value ||
  182. mp11::mp_contains<impl_type, const T*>::value)>(
  183. [&](const auto& t) {
  184. using U = std::decay_t<decltype(t)>;
  185. const U* tp = detail::variant_access::template get_if<U>(this);
  186. return tp && detail::relaxed_equal(*tp, t);
  187. },
  188. [&](const auto&) { return false; }, t);
  189. }
  190. /// The negation of operator==.
  191. template <class T>
  192. bool operator!=(const T& t) const {
  193. return !operator==(t);
  194. }
  195. template <class Archive>
  196. void serialize(Archive& ar, unsigned /* version */) {
  197. detail::variant_proxy<variant> p{*this};
  198. ar& make_nvp("variant", p);
  199. }
  200. private:
  201. impl_type impl;
  202. friend struct detail::variant_access;
  203. friend struct boost::histogram::unsafe_access;
  204. };
  205. // specialization for empty argument list, useful for meta-programming
  206. template <>
  207. class variant<> {};
  208. /// Apply visitor to variant (reference).
  209. template <class Visitor, class... Us>
  210. decltype(auto) visit(Visitor&& vis, variant<Us...>& var) {
  211. return detail::variant_access::visit(vis, var);
  212. }
  213. /// Apply visitor to variant (movable reference).
  214. template <class Visitor, class... Us>
  215. decltype(auto) visit(Visitor&& vis, variant<Us...>&& var) {
  216. return detail::variant_access::visit(vis, std::move(var));
  217. }
  218. /// Apply visitor to variant (const reference).
  219. template <class Visitor, class... Us>
  220. decltype(auto) visit(Visitor&& vis, const variant<Us...>& var) {
  221. return detail::variant_access::visit(vis, var);
  222. }
  223. /// Returns pointer to T in variant or null pointer if type does not match.
  224. template <class T, class... Us>
  225. auto get_if(variant<Us...>* v) {
  226. return detail::variant_access::template get_if<T>(v);
  227. }
  228. /// Returns pointer to const T in variant or null pointer if type does not match.
  229. template <class T, class... Us>
  230. auto get_if(const variant<Us...>* v) {
  231. return detail::variant_access::template get_if<T>(v);
  232. }
  233. /// Return reference to T, throws std::runtime_error if type does not match.
  234. template <class T, class... Us>
  235. decltype(auto) get(variant<Us...>& v) {
  236. auto tp = get_if<T>(&v);
  237. if (!tp) BOOST_THROW_EXCEPTION(std::runtime_error("T is not the held type"));
  238. return *tp;
  239. }
  240. /// Return movable reference to T, throws unspecified exception if type does not match.
  241. template <class T, class... Us>
  242. decltype(auto) get(variant<Us...>&& v) {
  243. auto tp = get_if<T>(&v);
  244. if (!tp) BOOST_THROW_EXCEPTION(std::runtime_error("T is not the held type"));
  245. return std::move(*tp);
  246. }
  247. /// Return const reference to T, throws unspecified exception if type does not match.
  248. template <class T, class... Us>
  249. decltype(auto) get(const variant<Us...>& v) {
  250. auto tp = get_if<T>(&v);
  251. if (!tp) BOOST_THROW_EXCEPTION(std::runtime_error("T is not the held type"));
  252. return *tp;
  253. }
  254. // pass-through version of visit for generic programming
  255. template <class Visitor, class T>
  256. decltype(auto) visit(Visitor&& vis, T&& var) {
  257. return std::forward<Visitor>(vis)(std::forward<T>(var));
  258. }
  259. // pass-through version of get for generic programming
  260. template <class T, class U>
  261. decltype(auto) get(U&& u) {
  262. return std::forward<U>(u);
  263. }
  264. // pass-through version of get_if for generic programming
  265. template <class T, class U>
  266. auto get_if(U* u) {
  267. return reinterpret_cast<T*>(std::is_same<T, std::decay_t<U>>::value ? u : nullptr);
  268. }
  269. // pass-through version of get_if for generic programming
  270. template <class T, class U>
  271. auto get_if(const U* u) {
  272. return reinterpret_cast<const T*>(std::is_same<T, std::decay_t<U>>::value ? u
  273. : nullptr);
  274. }
  275. } // namespace axis
  276. } // namespace histogram
  277. } // namespace boost
  278. #endif