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 |
| 27 | #elif defined(BOOST_MSVC) |
| 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. |
| 36 | namespace BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) { |
| 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 | |
| 45 | // a type returned from operator BOOST_TT_TRAIT_OP when no such operator is |
| 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 | |
| 55 | // when operator BOOST_TT_TRAIT_OP is not available, this one is used |
| 56 | no_operator operator BOOST_TT_TRAIT_OP (const any&, int); |
| 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 |
| 63 | // operator BOOST_TT_TRAIT_OP returns void and we can use the return type of |
| 64 | // (lhs BOOST_TT_TRAIT_OP, returns_void_t()) to deduce if |
| 65 | // operator BOOST_TT_TRAIT_OP returns void or not: |
| 66 | // - operator BOOST_TT_TRAIT_OP returns void -> (lhs BOOST_TT_TRAIT_OP, returns_void_t()) returns returns_void_t |
| 67 | // - operator BOOST_TT_TRAIT_OP returns !=void -> (lhs BOOST_TT_TRAIT_OP, returns_void_t()) returns int |
| 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: |
| 73 | // - value==true -> operator BOOST_TT_TRAIT_OP returns void |
| 74 | // - value==false -> operator BOOST_TT_TRAIT_OP does not return void |
| 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); |
| 81 | BOOST_STATIC_CONSTANT(bool, value = (sizeof(::ndnboost::type_traits::yes_type)==sizeof(returns_void((make<Lhs>() BOOST_TT_TRAIT_OP,returns_void_t()))))); |
| 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 > { |
| 95 | BOOST_STATIC_CONSTANT(bool, value = true); |
| 96 | }; |
| 97 | |
| 98 | template < typename Lhs > |
| 99 | struct operator_returns_Ret < Lhs, dont_care, false > { |
| 100 | BOOST_STATIC_CONSTANT(bool, value = true); |
| 101 | }; |
| 102 | |
| 103 | template < typename Lhs > |
| 104 | struct operator_returns_Ret < Lhs, void, true > { |
| 105 | BOOST_STATIC_CONSTANT(bool, value = true); |
| 106 | }; |
| 107 | |
| 108 | template < typename Lhs > |
| 109 | struct operator_returns_Ret < Lhs, void, false > { |
| 110 | BOOST_STATIC_CONSTANT(bool, value = false); |
| 111 | }; |
| 112 | |
| 113 | template < typename Lhs, typename Ret > |
| 114 | struct operator_returns_Ret < Lhs, Ret, true > { |
| 115 | BOOST_STATIC_CONSTANT(bool, value = false); |
| 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 | |
| 126 | BOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(make<Lhs>() BOOST_TT_TRAIT_OP))==sizeof(::ndnboost::type_traits::yes_type))); |
| 127 | }; |
| 128 | |
| 129 | |
| 130 | // 5. checks for operator existence |
| 131 | // condition: Lhs!=void |
| 132 | |
| 133 | // checks if our definition of operator BOOST_TT_TRAIT_OP is used or an other |
| 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 | |
| 144 | BOOST_STATIC_CONSTANT(bool, value = (sizeof(check(((make<Lhs>() BOOST_TT_TRAIT_OP),make<has_operator>())))==sizeof(::ndnboost::type_traits::yes_type))); |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | // 6. main trait: to avoid any compilation error, this class behaves |
| 149 | // differently when operator BOOST_TT_TRAIT_OP(Lhs) is forbidden by the |
| 150 | // standard. |
| 151 | // Forbidden_if is a bool that is: |
| 152 | // - true when the operator BOOST_TT_TRAIT_OP(Lhs) is forbidden by the standard |
| 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 > { |
| 160 | BOOST_STATIC_CONSTANT(bool, value = false); |
| 161 | }; |
| 162 | |
| 163 | template < typename Lhs, typename Ret > |
| 164 | struct trait_impl1 < Lhs, Ret, false > { |
| 165 | BOOST_STATIC_CONSTANT(bool, |
| 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 > { |
| 178 | BOOST_STATIC_CONSTANT(bool, value = false); |
| 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; |
| 187 | BOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Ret, BOOST_TT_FORBIDDEN_IF >::value)); |
| 188 | }; |
| 189 | |
| 190 | } // namespace impl |
| 191 | } // namespace detail |
| 192 | |
| 193 | // this is the accessible definition of the trait to end user |
| 194 | BOOST_TT_AUX_BOOL_TRAIT_DEF2(BOOST_TT_TRAIT_NAME, Lhs, Ret=::ndnboost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::dont_care, (::ndnboost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::trait_impl< Lhs, Ret >::value)) |
| 195 | |
| 196 | } // namespace ndnboost |
| 197 | |
| 198 | #if defined(BOOST_MSVC) |
| 199 | # pragma warning ( pop ) |
| 200 | #endif |
| 201 | |
| 202 | #include <ndnboost/type_traits/detail/bool_trait_undef.hpp> |