Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | // (C) Copyright 2009-2011 Frederic Bron, Robert Stewart, Steven Watanabe & Roman Perepelitsa. |
| 2 | // |
| 3 | // Use, modification and distribution are subject to the Boost Software License, |
| 4 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt). |
| 6 | // |
| 7 | // See http://www.boost.org/libs/type_traits for most recent version including documentation. |
| 8 | |
| 9 | #include <ndnboost/config.hpp> |
| 10 | #include <ndnboost/type_traits/ice.hpp> |
| 11 | #include <ndnboost/type_traits/integral_constant.hpp> |
| 12 | #include <ndnboost/type_traits/is_base_of.hpp> |
| 13 | #include <ndnboost/type_traits/is_const.hpp> |
| 14 | #include <ndnboost/type_traits/is_convertible.hpp> |
| 15 | #include <ndnboost/type_traits/is_fundamental.hpp> |
| 16 | #include <ndnboost/type_traits/is_integral.hpp> |
| 17 | #include <ndnboost/type_traits/is_pointer.hpp> |
| 18 | #include <ndnboost/type_traits/is_same.hpp> |
| 19 | #include <ndnboost/type_traits/is_void.hpp> |
| 20 | #include <ndnboost/type_traits/remove_cv.hpp> |
| 21 | #include <ndnboost/type_traits/remove_pointer.hpp> |
| 22 | #include <ndnboost/type_traits/remove_reference.hpp> |
| 23 | |
| 24 | // should be the last #include |
| 25 | #include <ndnboost/type_traits/detail/bool_trait_def.hpp> |
| 26 | |
| 27 | // cannot include this header without getting warnings of the kind: |
| 28 | // gcc: |
| 29 | // warning: value computed is not used |
| 30 | // warning: comparison between signed and unsigned integer expressions |
| 31 | // msvc: |
| 32 | // warning C4018: '<' : signed/unsigned mismatch |
| 33 | // warning C4244: '+=' : conversion from 'double' to 'char', possible loss of data |
| 34 | // warning C4547: '*' : operator before comma has no effect; expected operator with side-effect |
| 35 | // warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) |
| 36 | // warning C4804: '<' : unsafe use of type 'bool' in operation |
| 37 | // warning C4805: '==' : unsafe mix of type 'bool' and type 'char' in operation |
| 38 | // cannot find another implementation -> declared as system header to suppress these warnings. |
| 39 | #if defined(__GNUC__) && ((__GNUC__==3 && __GNUC_MINOR__>=1) || (__GNUC__>3)) |
| 40 | # pragma GCC system_header |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 41 | #elif defined(NDNBOOST_MSVC) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 42 | # pragma warning ( push ) |
| 43 | # pragma warning ( disable : 4018 4244 4547 4800 4804 4805 4913 ) |
| 44 | #endif |
| 45 | |
| 46 | namespace ndnboost { |
| 47 | namespace detail { |
| 48 | |
| 49 | // This namespace ensures that argument-dependent name lookup does not mess things up. |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 50 | namespace NDNBOOST_JOIN(NDNBOOST_TT_TRAIT_NAME,_impl) { |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 51 | |
| 52 | // 1. a function to have an instance of type T without requiring T to be default |
| 53 | // constructible |
| 54 | template <typename T> T &make(); |
| 55 | |
| 56 | |
| 57 | // 2. we provide our operator definition for types that do not have one already |
| 58 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 59 | // a type returned from operator NDNBOOST_TT_TRAIT_OP when no such operator is |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 60 | // found in the type's own namespace (our own operator is used) so that we have |
| 61 | // a means to know that our operator was used |
| 62 | struct no_operator { }; |
| 63 | |
| 64 | // this class allows implicit conversions and makes the following operator |
| 65 | // definition less-preferred than any other such operators that might be found |
| 66 | // via argument-dependent name lookup |
| 67 | struct any { template <class T> any(T const&); }; |
| 68 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 69 | // when operator NDNBOOST_TT_TRAIT_OP is not available, this one is used |
| 70 | no_operator operator NDNBOOST_TT_TRAIT_OP (const any&, const any&); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 71 | |
| 72 | |
| 73 | // 3. checks if the operator returns void or not |
| 74 | // conditions: Lhs!=void and Rhs!=void |
| 75 | |
| 76 | // we first redefine "operator," so that we have no compilation error if |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 77 | // operator NDNBOOST_TT_TRAIT_OP returns void and we can use the return type of |
| 78 | // (lhs NDNBOOST_TT_TRAIT_OP rhs, returns_void_t()) to deduce if |
| 79 | // operator NDNBOOST_TT_TRAIT_OP returns void or not: |
| 80 | // - operator NDNBOOST_TT_TRAIT_OP returns void -> (lhs NDNBOOST_TT_TRAIT_OP rhs, returns_void_t()) returns returns_void_t |
| 81 | // - operator NDNBOOST_TT_TRAIT_OP returns !=void -> (lhs NDNBOOST_TT_TRAIT_OP rhs, returns_void_t()) returns int |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 82 | struct returns_void_t { }; |
| 83 | template <typename T> int operator,(const T&, returns_void_t); |
| 84 | template <typename T> int operator,(const volatile T&, returns_void_t); |
| 85 | |
| 86 | // this intermediate trait has member value of type bool: |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 87 | // - value==true -> operator NDNBOOST_TT_TRAIT_OP returns void |
| 88 | // - value==false -> operator NDNBOOST_TT_TRAIT_OP does not return void |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 89 | template < typename Lhs, typename Rhs > |
| 90 | struct operator_returns_void { |
| 91 | // overloads of function returns_void make the difference |
| 92 | // yes_type and no_type have different size by construction |
| 93 | static ::ndnboost::type_traits::yes_type returns_void(returns_void_t); |
| 94 | static ::ndnboost::type_traits::no_type returns_void(int); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 95 | NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(::ndnboost::type_traits::yes_type)==sizeof(returns_void((make<Lhs>() NDNBOOST_TT_TRAIT_OP make<Rhs>(),returns_void_t()))))); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
| 99 | // 4. checks if the return type is Ret or Ret==dont_care |
| 100 | // conditions: Lhs!=void and Rhs!=void |
| 101 | |
| 102 | struct dont_care { }; |
| 103 | |
| 104 | template < typename Lhs, typename Rhs, typename Ret, bool Returns_void > |
| 105 | struct operator_returns_Ret; |
| 106 | |
| 107 | template < typename Lhs, typename Rhs > |
| 108 | struct operator_returns_Ret < Lhs, Rhs, dont_care, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 109 | NDNBOOST_STATIC_CONSTANT(bool, value = true); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | template < typename Lhs, typename Rhs > |
| 113 | struct operator_returns_Ret < Lhs, Rhs, dont_care, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 114 | NDNBOOST_STATIC_CONSTANT(bool, value = true); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | template < typename Lhs, typename Rhs > |
| 118 | struct operator_returns_Ret < Lhs, Rhs, void, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 119 | NDNBOOST_STATIC_CONSTANT(bool, value = true); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | template < typename Lhs, typename Rhs > |
| 123 | struct operator_returns_Ret < Lhs, Rhs, void, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 124 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | template < typename Lhs, typename Rhs, typename Ret > |
| 128 | struct operator_returns_Ret < Lhs, Rhs, Ret, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 129 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | // otherwise checks if it is convertible to Ret using the sizeof trick |
| 133 | // based on overload resolution |
| 134 | // condition: Ret!=void and Ret!=dont_care and the operator does not return void |
| 135 | template < typename Lhs, typename Rhs, typename Ret > |
| 136 | struct operator_returns_Ret < Lhs, Rhs, Ret, false > { |
| 137 | static ::ndnboost::type_traits::yes_type is_convertible_to_Ret(Ret); // this version is preferred for types convertible to Ret |
| 138 | static ::ndnboost::type_traits::no_type is_convertible_to_Ret(...); // this version is used otherwise |
| 139 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 140 | NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(make<Lhs>() NDNBOOST_TT_TRAIT_OP make<Rhs>()))==sizeof(::ndnboost::type_traits::yes_type))); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | |
| 144 | // 5. checks for operator existence |
| 145 | // condition: Lhs!=void and Rhs!=void |
| 146 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 147 | // checks if our definition of operator NDNBOOST_TT_TRAIT_OP is used or an other |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 148 | // existing one; |
| 149 | // this is done with redefinition of "operator," that returns no_operator or has_operator |
| 150 | struct has_operator { }; |
| 151 | no_operator operator,(no_operator, has_operator); |
| 152 | |
| 153 | template < typename Lhs, typename Rhs > |
| 154 | struct operator_exists { |
| 155 | static ::ndnboost::type_traits::yes_type check(has_operator); // this version is preferred when operator exists |
| 156 | static ::ndnboost::type_traits::no_type check(no_operator); // this version is used otherwise |
| 157 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 158 | NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(check(((make<Lhs>() NDNBOOST_TT_TRAIT_OP make<Rhs>()),make<has_operator>())))==sizeof(::ndnboost::type_traits::yes_type))); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | // 6. main trait: to avoid any compilation error, this class behaves |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 163 | // differently when operator NDNBOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 164 | // standard. |
| 165 | // Forbidden_if is a bool that is: |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 166 | // - true when the operator NDNBOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the standard |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 167 | // (would yield compilation error if used) |
| 168 | // - false otherwise |
| 169 | template < typename Lhs, typename Rhs, typename Ret, bool Forbidden_if > |
| 170 | struct trait_impl1; |
| 171 | |
| 172 | template < typename Lhs, typename Rhs, typename Ret > |
| 173 | struct trait_impl1 < Lhs, Rhs, Ret, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 174 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | template < typename Lhs, typename Rhs, typename Ret > |
| 178 | struct trait_impl1 < Lhs, Rhs, Ret, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 179 | NDNBOOST_STATIC_CONSTANT(bool, |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 180 | value = ( |
| 181 | ::ndnboost::type_traits::ice_and< |
| 182 | operator_exists < Lhs, Rhs >::value, |
| 183 | operator_returns_Ret < Lhs, Rhs, Ret, operator_returns_void < Lhs, Rhs >::value >::value |
| 184 | >::value |
| 185 | ) |
| 186 | ); |
| 187 | }; |
| 188 | |
| 189 | // some specializations needs to be declared for the special void case |
| 190 | template < typename Rhs, typename Ret > |
| 191 | struct trait_impl1 < void, Rhs, Ret, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 192 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | template < typename Lhs, typename Ret > |
| 196 | struct trait_impl1 < Lhs, void, Ret, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 197 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | template < typename Ret > |
| 201 | struct trait_impl1 < void, void, Ret, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 202 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | // defines some typedef for convenience |
| 206 | template < typename Lhs, typename Rhs, typename Ret > |
| 207 | struct trait_impl { |
| 208 | typedef typename ::ndnboost::remove_reference<Lhs>::type Lhs_noref; |
| 209 | typedef typename ::ndnboost::remove_reference<Rhs>::type Rhs_noref; |
| 210 | typedef typename ::ndnboost::remove_cv<Lhs_noref>::type Lhs_nocv; |
| 211 | typedef typename ::ndnboost::remove_cv<Rhs_noref>::type Rhs_nocv; |
| 212 | typedef typename ::ndnboost::remove_cv< typename ::ndnboost::remove_reference< typename ::ndnboost::remove_pointer<Lhs_noref>::type >::type >::type Lhs_noptr; |
| 213 | typedef typename ::ndnboost::remove_cv< typename ::ndnboost::remove_reference< typename ::ndnboost::remove_pointer<Rhs_noref>::type >::type >::type Rhs_noptr; |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 214 | NDNBOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Rhs_noref, Ret, NDNBOOST_TT_FORBIDDEN_IF >::value)); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | } // namespace impl |
| 218 | } // namespace detail |
| 219 | |
| 220 | // this is the accessible definition of the trait to end user |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 221 | NDNBOOST_TT_AUX_BOOL_TRAIT_DEF3(NDNBOOST_TT_TRAIT_NAME, Lhs, Rhs=Lhs, Ret=::ndnboost::detail::NDNBOOST_JOIN(NDNBOOST_TT_TRAIT_NAME,_impl)::dont_care, (::ndnboost::detail::NDNBOOST_JOIN(NDNBOOST_TT_TRAIT_NAME,_impl)::trait_impl < Lhs, Rhs, Ret >::value)) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 222 | |
| 223 | } // namespace ndnboost |
| 224 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 225 | #if defined(NDNBOOST_MSVC) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 226 | # pragma warning ( pop ) |
| 227 | #endif |
| 228 | |
| 229 | #include <ndnboost/type_traits/detail/bool_trait_undef.hpp> |