variant.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/histogram/detail/variant_proxy.hpp>
  16. #include <boost/mp11/algorithm.hpp> // mp_contains
  17. #include <boost/mp11/list.hpp> // mp_first
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/variant2/variant.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 <typename 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) { 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 axis::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 axis::traits::inclusive(a); }, *this);
  83. }
  84. /// Return reference to const metadata or instance of null_type if axis has no
  85. /// metadata.
  86. const metadata_type& metadata() const {
  87. return visit(
  88. [](const auto& a) -> const metadata_type& {
  89. using M = decltype(traits::metadata(a));
  90. return detail::static_if<std::is_same<M, const metadata_type&>>(
  91. [](const auto& a) -> const metadata_type& { return traits::metadata(a); },
  92. [](const auto&) -> const metadata_type& {
  93. BOOST_THROW_EXCEPTION(std::runtime_error(
  94. "cannot return metadata of type " + detail::type_name<M>() +
  95. " through axis::variant interface which uses type " +
  96. detail::type_name<metadata_type>() +
  97. "; use boost::histogram::axis::get to obtain a reference "
  98. "of this axis type"));
  99. },
  100. a);
  101. },
  102. *this);
  103. }
  104. /// Return reference to metadata or instance of null_type if axis has no
  105. /// metadata.
  106. metadata_type& metadata() {
  107. return visit(
  108. [](auto& a) -> metadata_type& {
  109. using M = decltype(traits::metadata(a));
  110. return detail::static_if<std::is_same<M, metadata_type&>>(
  111. [](auto& a) -> metadata_type& { return traits::metadata(a); },
  112. [](auto&) -> metadata_type& {
  113. BOOST_THROW_EXCEPTION(std::runtime_error(
  114. "cannot return metadata of type " + detail::type_name<M>() +
  115. " through axis::variant interface which uses type " +
  116. detail::type_name<metadata_type>() +
  117. "; use boost::histogram::axis::get to obtain a reference "
  118. "of this axis type"));
  119. },
  120. a);
  121. },
  122. *this);
  123. }
  124. /** Return index for value argument.
  125. Throws std::invalid_argument if axis has incompatible call signature.
  126. */
  127. template <class U>
  128. index_type index(const U& u) const {
  129. return visit([&u](const auto& a) { return traits::index(a, u); }, *this);
  130. }
  131. /** Return value for index argument.
  132. Only works for axes with value method that returns something convertible
  133. to double and will throw a runtime_error otherwise, see
  134. axis::traits::value().
  135. */
  136. double value(real_index_type idx) const {
  137. return visit([idx](const auto& a) { return traits::value_as<double>(a, idx); },
  138. *this);
  139. }
  140. /** Return bin for index argument.
  141. Only works for axes with value method that returns something convertible
  142. to double and will throw a runtime_error otherwise, see
  143. axis::traits::value().
  144. */
  145. auto bin(index_type idx) const {
  146. return visit(
  147. [idx](const auto& a) {
  148. return detail::value_method_switch(
  149. [idx](const auto& a) { // axis is discrete
  150. const double x = traits::value_as<double>(a, idx);
  151. return polymorphic_bin<double>(x, x);
  152. },
  153. [idx](const auto& a) { // axis is continuous
  154. const double x1 = traits::value_as<double>(a, idx);
  155. const double x2 = traits::value_as<double>(a, idx + 1);
  156. return polymorphic_bin<double>(x1, x2);
  157. },
  158. a);
  159. },
  160. *this);
  161. }
  162. /** Compare two variants.
  163. Return true if the variants point to the same concrete axis type and the types compare
  164. equal. Otherwise return false.
  165. */
  166. template <class... Us>
  167. bool operator==(const variant<Us...>& u) const {
  168. return visit([&u](const auto& x) { return u == x; }, *this);
  169. }
  170. /** Compare variant with a concrete axis type.
  171. Return true if the variant point to the same concrete axis type and the types compare
  172. equal. Otherwise return false.
  173. */
  174. template <class T>
  175. bool operator==(const T& t) const {
  176. return detail::static_if_c<(mp11::mp_contains<impl_type, T>::value ||
  177. mp11::mp_contains<impl_type, T*>::value ||
  178. mp11::mp_contains<impl_type, const T*>::value)>(
  179. [&](const auto& t) {
  180. using U = std::decay_t<decltype(t)>;
  181. const U* tp = detail::variant_access::template get_if<U>(this);
  182. return tp && detail::relaxed_equal(*tp, t);
  183. },
  184. [&](const auto&) { return false; }, t);
  185. }
  186. /// The negation of operator==.
  187. template <class T>
  188. bool operator!=(const T& t) const {
  189. return !operator==(t);
  190. }
  191. template <class Archive>
  192. void serialize(Archive& ar, unsigned /* version */) {
  193. detail::variant_proxy<variant> p{*this};
  194. ar& make_nvp("variant", p);
  195. }
  196. private:
  197. impl_type impl;
  198. friend struct detail::variant_access;
  199. friend struct boost::histogram::unsafe_access;
  200. };
  201. // specialization for empty argument list, useful for meta-programming
  202. template <>
  203. class variant<> {};
  204. /// Apply visitor to variant (reference).
  205. template <class Visitor, class... Us>
  206. decltype(auto) visit(Visitor&& vis, variant<Us...>& var) {
  207. return detail::variant_access::visit(vis, var);
  208. }
  209. /// Apply visitor to variant (movable reference).
  210. template <class Visitor, class... Us>
  211. decltype(auto) visit(Visitor&& vis, variant<Us...>&& var) {
  212. return detail::variant_access::visit(vis, std::move(var));
  213. }
  214. /// Apply visitor to variant (const reference).
  215. template <class Visitor, class... Us>
  216. decltype(auto) visit(Visitor&& vis, const variant<Us...>& var) {
  217. return detail::variant_access::visit(vis, var);
  218. }
  219. /// Returns pointer to T in variant or null pointer if type does not match.
  220. template <class T, class... Us>
  221. auto get_if(variant<Us...>* v) {
  222. return detail::variant_access::template get_if<T>(v);
  223. }
  224. /// Returns pointer to const T in variant or null pointer if type does not match.
  225. template <class T, class... Us>
  226. auto get_if(const variant<Us...>* v) {
  227. return detail::variant_access::template get_if<T>(v);
  228. }
  229. /// Return reference to T, throws std::runtime_error if type does not match.
  230. template <class T, class... Us>
  231. decltype(auto) get(variant<Us...>& v) {
  232. auto tp = get_if<T>(&v);
  233. if (!tp) BOOST_THROW_EXCEPTION(std::runtime_error("T is not the held type"));
  234. return *tp;
  235. }
  236. /// Return movable reference to T, throws unspecified exception if type does not match.
  237. template <class T, class... Us>
  238. decltype(auto) get(variant<Us...>&& v) {
  239. auto tp = get_if<T>(&v);
  240. if (!tp) BOOST_THROW_EXCEPTION(std::runtime_error("T is not the held type"));
  241. return std::move(*tp);
  242. }
  243. /// Return const reference to T, throws unspecified exception if type does not match.
  244. template <class T, class... Us>
  245. decltype(auto) get(const variant<Us...>& v) {
  246. auto tp = get_if<T>(&v);
  247. if (!tp) BOOST_THROW_EXCEPTION(std::runtime_error("T is not the held type"));
  248. return *tp;
  249. }
  250. // pass-through version of visit for generic programming
  251. template <class Visitor, class T>
  252. decltype(auto) visit(Visitor&& vis, T&& var) {
  253. return std::forward<Visitor>(vis)(std::forward<T>(var));
  254. }
  255. // pass-through version of get for generic programming
  256. template <class T, class U>
  257. decltype(auto) get(U&& u) {
  258. return std::forward<U>(u);
  259. }
  260. // pass-through version of get_if for generic programming
  261. template <class T, class U>
  262. auto get_if(U* u) {
  263. return reinterpret_cast<T*>(std::is_same<T, std::decay_t<U>>::value ? u : nullptr);
  264. }
  265. // pass-through version of get_if for generic programming
  266. template <class T, class U>
  267. auto get_if(const U* u) {
  268. return reinterpret_cast<const T*>(std::is_same<T, std::decay_t<U>>::value ? u
  269. : nullptr);
  270. }
  271. } // namespace axis
  272. } // namespace histogram
  273. } // namespace boost
  274. #endif