| 1 | // Boost.Geometry |
| 2 | |
| 3 | // Copyright (c) 2020, Oracle and/or its affiliates. |
| 4 | |
| 5 | // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle |
| 6 | |
| 7 | // Licensed under the Boost Software License version 1.0. |
| 8 | // http://www.boost.org/users/license.html |
| 9 | |
| 10 | #ifndef BOOST_GEOMETRY_UTIL_TYPE_TRAITS_STD_HPP |
| 11 | #define BOOST_GEOMETRY_UTIL_TYPE_TRAITS_STD_HPP |
| 12 | |
| 13 | |
| 14 | #include <cstddef> |
| 15 | #include <type_traits> |
| 16 | |
| 17 | |
| 18 | namespace boost { namespace geometry |
| 19 | { |
| 20 | |
| 21 | |
| 22 | namespace util |
| 23 | { |
| 24 | |
| 25 | |
| 26 | // C++17 |
| 27 | template <bool B> |
| 28 | using bool_constant = std::integral_constant<bool, B>; |
| 29 | |
| 30 | // non-standard |
| 31 | template <int I> |
| 32 | using int_constant = std::integral_constant<int, I>; |
| 33 | |
| 34 | // non-standard |
| 35 | template <std::size_t I> |
| 36 | using index_constant = std::integral_constant<std::size_t, I>; |
| 37 | |
| 38 | // non-standard |
| 39 | template <std::size_t S> |
| 40 | using size_constant = std::integral_constant<std::size_t, S>; |
| 41 | |
| 42 | |
| 43 | // C++17 |
| 44 | template <typename ...> |
| 45 | struct conjunction |
| 46 | : std::true_type |
| 47 | {}; |
| 48 | template<typename Trait> |
| 49 | struct conjunction<Trait> |
| 50 | : Trait |
| 51 | {}; |
| 52 | template <typename Trait, typename ...Traits> |
| 53 | struct conjunction<Trait, Traits...> |
| 54 | : std::conditional_t<Trait::value, conjunction<Traits...>, Trait> |
| 55 | {}; |
| 56 | |
| 57 | // C++17 |
| 58 | template <typename ...> |
| 59 | struct disjunction |
| 60 | : std::false_type |
| 61 | {}; |
| 62 | template <typename Trait> |
| 63 | struct disjunction<Trait> |
| 64 | : Trait |
| 65 | {}; |
| 66 | template <typename Trait, typename ...Traits> |
| 67 | struct disjunction<Trait, Traits...> |
| 68 | : std::conditional_t<Trait::value, Trait, disjunction<Traits...>> |
| 69 | {}; |
| 70 | |
| 71 | // C++17 |
| 72 | template <typename Trait> |
| 73 | struct negation |
| 74 | : bool_constant<!Trait::value> |
| 75 | {}; |
| 76 | |
| 77 | |
| 78 | // non-standard |
| 79 | /* |
| 80 | template <typename ...Traits> |
| 81 | using and_ = conjunction<Traits...>; |
| 82 | |
| 83 | template <typename ...Traits> |
| 84 | using or_ = disjunction<Traits...>; |
| 85 | |
| 86 | template <typename Trait> |
| 87 | using not_ = negation<Trait>; |
| 88 | */ |
| 89 | |
| 90 | |
| 91 | // C++20 |
| 92 | template <typename T> |
| 93 | struct remove_cvref |
| 94 | { |
| 95 | using type = std::remove_cv_t<std::remove_reference_t<T>>; |
| 96 | }; |
| 97 | |
| 98 | template <typename T> |
| 99 | using remove_cvref_t = typename remove_cvref<T>::type; |
| 100 | |
| 101 | // non-standard |
| 102 | template <typename T> |
| 103 | struct remove_cref |
| 104 | { |
| 105 | using type = std::remove_const_t<std::remove_reference_t<T>>; |
| 106 | }; |
| 107 | |
| 108 | template <typename T> |
| 109 | using remove_cref_t = typename remove_cref<T>::type; |
| 110 | |
| 111 | // non-standard |
| 112 | template <typename T> |
| 113 | struct remove_cptrref |
| 114 | { |
| 115 | using type = std::remove_const_t |
| 116 | < |
| 117 | std::remove_pointer_t<std::remove_reference_t<T>> |
| 118 | >; |
| 119 | }; |
| 120 | |
| 121 | template <typename T> |
| 122 | using remove_cptrref_t = typename remove_cptrref<T>::type; |
| 123 | |
| 124 | |
| 125 | // non-standard |
| 126 | template <typename From, typename To> |
| 127 | struct transcribe_const |
| 128 | { |
| 129 | using type = std::conditional_t |
| 130 | < |
| 131 | std::is_const<std::remove_reference_t<From>>::value, |
| 132 | std::add_const_t<To>, |
| 133 | To |
| 134 | >; |
| 135 | }; |
| 136 | |
| 137 | template <typename From, typename To> |
| 138 | using transcribe_const_t = typename transcribe_const<From, To>::type; |
| 139 | |
| 140 | |
| 141 | // non-standard |
| 142 | template <typename From, typename To> |
| 143 | struct transcribe_reference |
| 144 | { |
| 145 | using type = std::remove_reference_t<To>; |
| 146 | }; |
| 147 | |
| 148 | template <typename From, typename To> |
| 149 | struct transcribe_reference<From &, To> |
| 150 | { |
| 151 | using type = std::remove_reference_t<To> &; |
| 152 | }; |
| 153 | |
| 154 | template <typename From, typename To> |
| 155 | struct transcribe_reference<From &&, To> |
| 156 | { |
| 157 | using type = std::remove_reference_t<To> &&; |
| 158 | }; |
| 159 | |
| 160 | template <typename From, typename To> |
| 161 | using transcribe_reference_t = typename transcribe_reference<From, To>::type; |
| 162 | |
| 163 | |
| 164 | // non-standard |
| 165 | template <typename From, typename To> |
| 166 | struct transcribe_cref |
| 167 | { |
| 168 | using type = transcribe_reference_t<From, transcribe_const_t<From, To>>; |
| 169 | }; |
| 170 | |
| 171 | template <typename From, typename To> |
| 172 | using transcribe_cref_t = typename transcribe_cref<From, To>::type; |
| 173 | |
| 174 | |
| 175 | } // namespace util |
| 176 | |
| 177 | |
| 178 | // Deprecated utilities, defined for backward compatibility but might be |
| 179 | // removed in the future. |
| 180 | |
| 181 | |
| 182 | /*! |
| 183 | \brief Meta-function to define a const or non const type |
| 184 | \ingroup utility |
| 185 | \details If the boolean template parameter is true, the type parameter |
| 186 | will be defined as const, otherwise it will be defined as it was. |
| 187 | This meta-function is used to have one implementation for both |
| 188 | const and non const references |
| 189 | \note This traits class is completely independant from Boost.Geometry |
| 190 | and might be a separate addition to Boost |
| 191 | \note Used in a.o. for_each, interior_rings, exterior_ring |
| 192 | \par Example |
| 193 | \code |
| 194 | void foo(typename add_const_if_c<IsConst, Point>::type& point) |
| 195 | \endcode |
| 196 | */ |
| 197 | template <bool IsConst, typename Type> |
| 198 | struct add_const_if_c |
| 199 | { |
| 200 | typedef std::conditional_t |
| 201 | < |
| 202 | IsConst, |
| 203 | Type const, |
| 204 | Type |
| 205 | > type; |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | namespace util |
| 210 | { |
| 211 | |
| 212 | template <typename T> |
| 213 | using bare_type = remove_cptrref<T>; |
| 214 | |
| 215 | } // namespace util |
| 216 | |
| 217 | |
| 218 | }} // namespace boost::geometry |
| 219 | |
| 220 | #endif // BOOST_GEOMETRY_UTIL_TYPE_TRAITS_STD_HPP |
| 221 | |