carl  24.04
Computer ARithmetic Library
variant_util.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/variant.hpp>
4 
5 #include <type_traits>
6 
7 namespace carl {
8 
9 namespace detail {
10 template<typename T>
11 struct variant_is_type_visitor : boost::static_visitor<bool> {
12  template<typename TT>
13  constexpr bool operator()(const TT& /*unused*/) const noexcept {
14  return std::is_same<T, TT>::value;
15  }
16 };
17 } // namespace detail
18 /**
19  * Checks whether a variant contains a value of a fiven type
20  */
21 template<typename T, typename Variant>
22 bool variant_is_type(const Variant& variant) noexcept {
23  return boost::apply_visitor(detail::variant_is_type_visitor<T>(), variant);
24 }
25 
26 namespace detail {
27 template<typename Target>
28 struct variant_extend_visitor : boost::static_visitor<Target> {
29  template<typename T>
30  Target operator()(const T& t) const {
31  return Target(t);
32  }
33 };
34 } // namespace detail
35 template<typename Target, typename... Args>
36 Target variant_extend(const boost::variant<Args...>& variant) {
37  return boost::apply_visitor(detail::variant_extend_visitor<Target>(), variant);
38 }
39 
40 namespace detail {
41 struct variant_hash : boost::static_visitor<std::size_t> {
42  template<class T>
43  std::size_t operator()(const T& val) const {
44  return std::hash<T>()(val);
45  }
46 };
47 } // namespace detail
48 
49 template<typename... T>
50 inline std::size_t variant_hash(const boost::variant<T...>& value) {
51  return boost::apply_visitor(detail::variant_hash(), value);
52 }
53 
54 namespace detail {
55  template<template<typename...> class Check, typename T, typename Variant>
57  template<template<typename...> class Check, typename T, template<typename...> class Variant, typename... Args>
58  struct is_from_variant_wrapper<Check, T, Variant<Args...>> {
59  static constexpr bool value = std::disjunction<Check<T,Args>...>::value;
60  };
61 }
62 
63 template<typename T, typename Variant>
66 };
67 template<typename T, typename Variant>
70 };
71 
72 } // namespace carl
carl is the main namespace for the library.
std::size_t variant_hash(const boost::variant< T... > &value)
Definition: variant_util.h:50
Target variant_extend(const boost::variant< Args... > &variant)
Definition: variant_util.h:36
bool variant_is_type(const Variant &variant) noexcept
Checks whether a variant contains a value of a fiven type.
Definition: variant_util.h:22
constexpr bool operator()(const TT &) const noexcept
Definition: variant_util.h:13
Target operator()(const T &t) const
Definition: variant_util.h:30
std::size_t operator()(const T &val) const
Definition: variant_util.h:43
static constexpr bool value
Definition: variant_util.h:65
static constexpr bool value
Definition: variant_util.h:69