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_const.hpp> |
| 13 | #include <ndnboost/type_traits/is_fundamental.hpp> |
| 14 | #include <ndnboost/type_traits/is_pointer.hpp> |
| 15 | #include <ndnboost/type_traits/is_same.hpp> |
| 16 | #include <ndnboost/type_traits/is_void.hpp> |
| 17 | #include <ndnboost/type_traits/remove_cv.hpp> |
| 18 | #include <ndnboost/type_traits/remove_pointer.hpp> |
| 19 | #include <ndnboost/type_traits/remove_reference.hpp> |
| 20 | |
| 21 | // should be the last #include |
| 22 | #include <ndnboost/type_traits/detail/bool_trait_def.hpp> |
| 23 | |
| 24 | // avoid warnings |
| 25 | #if defined(__GNUC__) && ((__GNUC__==3 && __GNUC_MINOR__>=1) || (__GNUC__>3)) |
| 26 | # pragma GCC system_header |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 27 | #elif defined(NDNBOOST_MSVC) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 28 | # pragma warning ( push ) |
| 29 | # pragma warning ( disable : 4244 4913 ) |
| 30 | #endif |
| 31 | |
| 32 | namespace ndnboost { |
| 33 | namespace detail { |
| 34 | |
| 35 | // 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^] | 36 | namespace NDNBOOST_JOIN(NDNBOOST_TT_TRAIT_NAME,_impl) { |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 37 | |
| 38 | // 1. a function to have an instance of type T without requiring T to be default |
| 39 | // constructible |
| 40 | template <typename T> T &make(); |
| 41 | |
| 42 | |
| 43 | // 2. we provide our operator definition for types that do not have one already |
| 44 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 45 | // 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] | 46 | // found in the type's own namespace (our own operator is used) so that we have |
| 47 | // a means to know that our operator was used |
| 48 | struct no_operator { }; |
| 49 | |
| 50 | // this class allows implicit conversions and makes the following operator |
| 51 | // definition less-preferred than any other such operators that might be found |
| 52 | // via argument-dependent name lookup |
| 53 | struct any { template <class T> any(T const&); }; |
| 54 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 55 | // when operator NDNBOOST_TT_TRAIT_OP is not available, this one is used |
| 56 | no_operator operator NDNBOOST_TT_TRAIT_OP (const any&, int); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | // 3. checks if the operator returns void or not |
| 60 | // conditions: Lhs!=void |
| 61 | |
| 62 | // we first redefine "operator," so that we have no compilation error if |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 63 | // operator NDNBOOST_TT_TRAIT_OP returns void and we can use the return type of |
| 64 | // (lhs NDNBOOST_TT_TRAIT_OP, returns_void_t()) to deduce if |
| 65 | // operator NDNBOOST_TT_TRAIT_OP returns void or not: |
| 66 | // - operator NDNBOOST_TT_TRAIT_OP returns void -> (lhs NDNBOOST_TT_TRAIT_OP, returns_void_t()) returns returns_void_t |
| 67 | // - operator NDNBOOST_TT_TRAIT_OP returns !=void -> (lhs NDNBOOST_TT_TRAIT_OP, returns_void_t()) returns int |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 68 | struct returns_void_t { }; |
| 69 | template <typename T> int operator,(const T&, returns_void_t); |
| 70 | template <typename T> int operator,(const volatile T&, returns_void_t); |
| 71 | |
| 72 | // this intermediate trait has member value of type bool: |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 73 | // - value==true -> operator NDNBOOST_TT_TRAIT_OP returns void |
| 74 | // - value==false -> operator NDNBOOST_TT_TRAIT_OP does not return void |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 75 | template < typename Lhs > |
| 76 | struct operator_returns_void { |
| 77 | // overloads of function returns_void make the difference |
| 78 | // yes_type and no_type have different size by construction |
| 79 | static ::ndnboost::type_traits::yes_type returns_void(returns_void_t); |
| 80 | static ::ndnboost::type_traits::no_type returns_void(int); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 81 | NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(::ndnboost::type_traits::yes_type)==sizeof(returns_void((make<Lhs>() NDNBOOST_TT_TRAIT_OP,returns_void_t()))))); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | |
| 85 | // 4. checks if the return type is Ret or Ret==dont_care |
| 86 | // conditions: Lhs!=void |
| 87 | |
| 88 | struct dont_care { }; |
| 89 | |
| 90 | template < typename Lhs, typename Ret, bool Returns_void > |
| 91 | struct operator_returns_Ret; |
| 92 | |
| 93 | template < typename Lhs > |
| 94 | struct operator_returns_Ret < Lhs, dont_care, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 95 | NDNBOOST_STATIC_CONSTANT(bool, value = true); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | template < typename Lhs > |
| 99 | struct operator_returns_Ret < Lhs, dont_care, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 100 | NDNBOOST_STATIC_CONSTANT(bool, value = true); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | template < typename Lhs > |
| 104 | struct operator_returns_Ret < Lhs, void, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 105 | NDNBOOST_STATIC_CONSTANT(bool, value = true); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | template < typename Lhs > |
| 109 | struct operator_returns_Ret < Lhs, void, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 110 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | template < typename Lhs, typename Ret > |
| 114 | struct operator_returns_Ret < Lhs, Ret, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 115 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // otherwise checks if it is convertible to Ret using the sizeof trick |
| 119 | // based on overload resolution |
| 120 | // condition: Ret!=void and Ret!=dont_care and the operator does not return void |
| 121 | template < typename Lhs, typename Ret > |
| 122 | struct operator_returns_Ret < Lhs, Ret, false > { |
| 123 | static ::ndnboost::type_traits::yes_type is_convertible_to_Ret(Ret); // this version is preferred for types convertible to Ret |
| 124 | static ::ndnboost::type_traits::no_type is_convertible_to_Ret(...); // this version is used otherwise |
| 125 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 126 | NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(make<Lhs>() NDNBOOST_TT_TRAIT_OP))==sizeof(::ndnboost::type_traits::yes_type))); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | |
| 130 | // 5. checks for operator existence |
| 131 | // condition: Lhs!=void |
| 132 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 133 | // 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] | 134 | // existing one; |
| 135 | // this is done with redefinition of "operator," that returns no_operator or has_operator |
| 136 | struct has_operator { }; |
| 137 | no_operator operator,(no_operator, has_operator); |
| 138 | |
| 139 | template < typename Lhs > |
| 140 | struct operator_exists { |
| 141 | static ::ndnboost::type_traits::yes_type check(has_operator); // this version is preferred when operator exists |
| 142 | static ::ndnboost::type_traits::no_type check(no_operator); // this version is used otherwise |
| 143 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 144 | NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(check(((make<Lhs>() NDNBOOST_TT_TRAIT_OP),make<has_operator>())))==sizeof(::ndnboost::type_traits::yes_type))); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | |
| 148 | // 6. main trait: to avoid any compilation error, this class behaves |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 149 | // differently when operator NDNBOOST_TT_TRAIT_OP(Lhs) is forbidden by the |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 150 | // standard. |
| 151 | // Forbidden_if is a bool that is: |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 152 | // - true when the operator NDNBOOST_TT_TRAIT_OP(Lhs) is forbidden by the standard |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 153 | // (would yield compilation error if used) |
| 154 | // - false otherwise |
| 155 | template < typename Lhs, typename Ret, bool Forbidden_if > |
| 156 | struct trait_impl1; |
| 157 | |
| 158 | template < typename Lhs, typename Ret > |
| 159 | struct trait_impl1 < Lhs, Ret, true > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 160 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | template < typename Lhs, typename Ret > |
| 164 | struct trait_impl1 < Lhs, Ret, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 165 | NDNBOOST_STATIC_CONSTANT(bool, |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 166 | value = ( |
| 167 | ::ndnboost::type_traits::ice_and< |
| 168 | operator_exists < Lhs >::value, |
| 169 | operator_returns_Ret < Lhs, Ret, operator_returns_void < Lhs >::value >::value |
| 170 | >::value |
| 171 | ) |
| 172 | ); |
| 173 | }; |
| 174 | |
| 175 | // specialization needs to be declared for the special void case |
| 176 | template < typename Ret > |
| 177 | struct trait_impl1 < void, Ret, false > { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 178 | NDNBOOST_STATIC_CONSTANT(bool, value = false); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // defines some typedef for convenience |
| 182 | template < typename Lhs, typename Ret > |
| 183 | struct trait_impl { |
| 184 | typedef typename ::ndnboost::remove_reference<Lhs>::type Lhs_noref; |
| 185 | typedef typename ::ndnboost::remove_cv<Lhs_noref>::type Lhs_nocv; |
| 186 | typedef typename ::ndnboost::remove_cv< typename ::ndnboost::remove_reference< typename ::ndnboost::remove_pointer<Lhs_noref>::type >::type >::type Lhs_noptr; |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 187 | NDNBOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Ret, NDNBOOST_TT_FORBIDDEN_IF >::value)); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace impl |
| 191 | } // namespace detail |
| 192 | |
| 193 | // this is the accessible definition of the trait to end user |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 194 | NDNBOOST_TT_AUX_BOOL_TRAIT_DEF2(NDNBOOST_TT_TRAIT_NAME, 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, Ret >::value)) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 195 | |
| 196 | } // namespace ndnboost |
| 197 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 198 | #if defined(NDNBOOST_MSVC) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 199 | # pragma warning ( pop ) |
| 200 | #endif |
| 201 | |
| 202 | #include <ndnboost/type_traits/detail/bool_trait_undef.hpp> |