blob: c10ef1022fe2abae43cc2cf74daeb0bbbdc8b652 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001// (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 Thompson3d613fd2013-10-15 15:39:04 -070041#elif defined(NDNBOOST_MSVC)
Jeff Thompsona28eed82013-08-22 16:21:10 -070042# pragma warning ( push )
43# pragma warning ( disable : 4018 4244 4547 4800 4804 4805 4913 )
44#endif
45
46namespace ndnboost {
47namespace detail {
48
49// This namespace ensures that argument-dependent name lookup does not mess things up.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070050namespace NDNBOOST_JOIN(NDNBOOST_TT_TRAIT_NAME,_impl) {
Jeff Thompsona28eed82013-08-22 16:21:10 -070051
52// 1. a function to have an instance of type T without requiring T to be default
53// constructible
54template <typename T> T &make();
55
56
57// 2. we provide our operator definition for types that do not have one already
58
Jeff Thompson3d613fd2013-10-15 15:39:04 -070059// a type returned from operator NDNBOOST_TT_TRAIT_OP when no such operator is
Jeff Thompsona28eed82013-08-22 16:21:10 -070060// 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
62struct 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
67struct any { template <class T> any(T const&); };
68
Jeff Thompson3d613fd2013-10-15 15:39:04 -070069// when operator NDNBOOST_TT_TRAIT_OP is not available, this one is used
70no_operator operator NDNBOOST_TT_TRAIT_OP (const any&, const any&);
Jeff Thompsona28eed82013-08-22 16:21:10 -070071
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 Thompson3d613fd2013-10-15 15:39:04 -070077// 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 Thompsona28eed82013-08-22 16:21:10 -070082struct returns_void_t { };
83template <typename T> int operator,(const T&, returns_void_t);
84template <typename T> int operator,(const volatile T&, returns_void_t);
85
86// this intermediate trait has member value of type bool:
Jeff Thompson3d613fd2013-10-15 15:39:04 -070087// - value==true -> operator NDNBOOST_TT_TRAIT_OP returns void
88// - value==false -> operator NDNBOOST_TT_TRAIT_OP does not return void
Jeff Thompsona28eed82013-08-22 16:21:10 -070089template < typename Lhs, typename Rhs >
90struct 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 Thompson3d613fd2013-10-15 15:39:04 -070095 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 Thompsona28eed82013-08-22 16:21:10 -070096};
97
98
99// 4. checks if the return type is Ret or Ret==dont_care
100// conditions: Lhs!=void and Rhs!=void
101
102struct dont_care { };
103
104template < typename Lhs, typename Rhs, typename Ret, bool Returns_void >
105struct operator_returns_Ret;
106
107template < typename Lhs, typename Rhs >
108struct operator_returns_Ret < Lhs, Rhs, dont_care, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700109 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700110};
111
112template < typename Lhs, typename Rhs >
113struct operator_returns_Ret < Lhs, Rhs, dont_care, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700114 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700115};
116
117template < typename Lhs, typename Rhs >
118struct operator_returns_Ret < Lhs, Rhs, void, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700119 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700120};
121
122template < typename Lhs, typename Rhs >
123struct operator_returns_Ret < Lhs, Rhs, void, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700124 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700125};
126
127template < typename Lhs, typename Rhs, typename Ret >
128struct operator_returns_Ret < Lhs, Rhs, Ret, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700129 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700130};
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
135template < typename Lhs, typename Rhs, typename Ret >
136struct 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 Thompson3d613fd2013-10-15 15:39:04 -0700140 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 Thompsona28eed82013-08-22 16:21:10 -0700141};
142
143
144// 5. checks for operator existence
145// condition: Lhs!=void and Rhs!=void
146
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700147// checks if our definition of operator NDNBOOST_TT_TRAIT_OP is used or an other
Jeff Thompsona28eed82013-08-22 16:21:10 -0700148// existing one;
149// this is done with redefinition of "operator," that returns no_operator or has_operator
150struct has_operator { };
151no_operator operator,(no_operator, has_operator);
152
153template < typename Lhs, typename Rhs >
154struct 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 Thompson3d613fd2013-10-15 15:39:04 -0700158 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 Thompsona28eed82013-08-22 16:21:10 -0700159};
160
161
162// 6. main trait: to avoid any compilation error, this class behaves
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700163// differently when operator NDNBOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the
Jeff Thompsona28eed82013-08-22 16:21:10 -0700164// standard.
165// Forbidden_if is a bool that is:
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700166// - true when the operator NDNBOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the standard
Jeff Thompsona28eed82013-08-22 16:21:10 -0700167// (would yield compilation error if used)
168// - false otherwise
169template < typename Lhs, typename Rhs, typename Ret, bool Forbidden_if >
170struct trait_impl1;
171
172template < typename Lhs, typename Rhs, typename Ret >
173struct trait_impl1 < Lhs, Rhs, Ret, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700174 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700175};
176
177template < typename Lhs, typename Rhs, typename Ret >
178struct trait_impl1 < Lhs, Rhs, Ret, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700179 NDNBOOST_STATIC_CONSTANT(bool,
Jeff Thompsona28eed82013-08-22 16:21:10 -0700180 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
190template < typename Rhs, typename Ret >
191struct trait_impl1 < void, Rhs, Ret, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700192 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700193};
194
195template < typename Lhs, typename Ret >
196struct trait_impl1 < Lhs, void, Ret, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700197 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700198};
199
200template < typename Ret >
201struct trait_impl1 < void, void, Ret, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700202 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700203};
204
205// defines some typedef for convenience
206template < typename Lhs, typename Rhs, typename Ret >
207struct 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 Thompson3d613fd2013-10-15 15:39:04 -0700214 NDNBOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Rhs_noref, Ret, NDNBOOST_TT_FORBIDDEN_IF >::value));
Jeff Thompsona28eed82013-08-22 16:21:10 -0700215};
216
217} // namespace impl
218} // namespace detail
219
220// this is the accessible definition of the trait to end user
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700221NDNBOOST_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 Thompsona28eed82013-08-22 16:21:10 -0700222
223} // namespace ndnboost
224
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700225#if defined(NDNBOOST_MSVC)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700226# pragma warning ( pop )
227#endif
228
229#include <ndnboost/type_traits/detail/bool_trait_undef.hpp>