variant.hpp 11 KB

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