blob: 5d23573864ef99e5d9677dafb3ce838c72ce7de2 [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_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 Thompson3d613fd2013-10-15 15:39:04 -070027#elif defined(NDNBOOST_MSVC)
Jeff Thompsona28eed82013-08-22 16:21:10 -070028# pragma warning ( push )
29# pragma warning ( disable : 4244 4913 )
30#endif
31
32namespace ndnboost {
33namespace detail {
34
35// This namespace ensures that argument-dependent name lookup does not mess things up.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070036namespace NDNBOOST_JOIN(NDNBOOST_TT_TRAIT_NAME,_impl) {
Jeff Thompsona28eed82013-08-22 16:21:10 -070037
38// 1. a function to have an instance of type T without requiring T to be default
39// constructible
40template <typename T> T &make();
41
42
43// 2. we provide our operator definition for types that do not have one already
44
Jeff Thompson3d613fd2013-10-15 15:39:04 -070045// a type returned from operator NDNBOOST_TT_TRAIT_OP when no such operator is
Jeff Thompsona28eed82013-08-22 16:21:10 -070046// 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
48struct 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
53struct any { template <class T> any(T const&); };
54
Jeff Thompson3d613fd2013-10-15 15:39:04 -070055// when operator NDNBOOST_TT_TRAIT_OP is not available, this one is used
56no_operator operator NDNBOOST_TT_TRAIT_OP (const any&, int);
Jeff Thompsona28eed82013-08-22 16:21:10 -070057
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 Thompson3d613fd2013-10-15 15:39:04 -070063// 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 Thompsona28eed82013-08-22 16:21:10 -070068struct returns_void_t { };
69template <typename T> int operator,(const T&, returns_void_t);
70template <typename T> int operator,(const volatile T&, returns_void_t);
71
72// this intermediate trait has member value of type bool:
Jeff Thompson3d613fd2013-10-15 15:39:04 -070073// - value==true -> operator NDNBOOST_TT_TRAIT_OP returns void
74// - value==false -> operator NDNBOOST_TT_TRAIT_OP does not return void
Jeff Thompsona28eed82013-08-22 16:21:10 -070075template < typename Lhs >
76struct 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 Thompson3d613fd2013-10-15 15:39:04 -070081 NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(::ndnboost::type_traits::yes_type)==sizeof(returns_void((make<Lhs>() NDNBOOST_TT_TRAIT_OP,returns_void_t())))));
Jeff Thompsona28eed82013-08-22 16:21:10 -070082};
83
84
85// 4. checks if the return type is Ret or Ret==dont_care
86// conditions: Lhs!=void
87
88struct dont_care { };
89
90template < typename Lhs, typename Ret, bool Returns_void >
91struct operator_returns_Ret;
92
93template < typename Lhs >
94struct operator_returns_Ret < Lhs, dont_care, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070095 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsona28eed82013-08-22 16:21:10 -070096};
97
98template < typename Lhs >
99struct operator_returns_Ret < Lhs, dont_care, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700100 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700101};
102
103template < typename Lhs >
104struct operator_returns_Ret < Lhs, void, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700105 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700106};
107
108template < typename Lhs >
109struct operator_returns_Ret < Lhs, void, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700110 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700111};
112
113template < typename Lhs, typename Ret >
114struct operator_returns_Ret < Lhs, Ret, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700115 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700116};
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
121template < typename Lhs, typename Ret >
122struct 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 Thompson3d613fd2013-10-15 15:39:04 -0700126 NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(make<Lhs>() NDNBOOST_TT_TRAIT_OP))==sizeof(::ndnboost::type_traits::yes_type)));
Jeff Thompsona28eed82013-08-22 16:21:10 -0700127};
128
129
130// 5. checks for operator existence
131// condition: Lhs!=void
132
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700133// checks if our definition of operator NDNBOOST_TT_TRAIT_OP is used or an other
Jeff Thompsona28eed82013-08-22 16:21:10 -0700134// existing one;
135// this is done with redefinition of "operator," that returns no_operator or has_operator
136struct has_operator { };
137no_operator operator,(no_operator, has_operator);
138
139template < typename Lhs >
140struct 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 Thompson3d613fd2013-10-15 15:39:04 -0700144 NDNBOOST_STATIC_CONSTANT(bool, value = (sizeof(check(((make<Lhs>() NDNBOOST_TT_TRAIT_OP),make<has_operator>())))==sizeof(::ndnboost::type_traits::yes_type)));
Jeff Thompsona28eed82013-08-22 16:21:10 -0700145};
146
147
148// 6. main trait: to avoid any compilation error, this class behaves
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700149// differently when operator NDNBOOST_TT_TRAIT_OP(Lhs) is forbidden by the
Jeff Thompsona28eed82013-08-22 16:21:10 -0700150// standard.
151// Forbidden_if is a bool that is:
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700152// - true when the operator NDNBOOST_TT_TRAIT_OP(Lhs) is forbidden by the standard
Jeff Thompsona28eed82013-08-22 16:21:10 -0700153// (would yield compilation error if used)
154// - false otherwise
155template < typename Lhs, typename Ret, bool Forbidden_if >
156struct trait_impl1;
157
158template < typename Lhs, typename Ret >
159struct trait_impl1 < Lhs, Ret, true > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700160 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700161};
162
163template < typename Lhs, typename Ret >
164struct trait_impl1 < Lhs, Ret, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700165 NDNBOOST_STATIC_CONSTANT(bool,
Jeff Thompsona28eed82013-08-22 16:21:10 -0700166 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
176template < typename Ret >
177struct trait_impl1 < void, Ret, false > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700178 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700179};
180
181// defines some typedef for convenience
182template < typename Lhs, typename Ret >
183struct 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 Thompson3d613fd2013-10-15 15:39:04 -0700187 NDNBOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Ret, NDNBOOST_TT_FORBIDDEN_IF >::value));
Jeff Thompsona28eed82013-08-22 16:21:10 -0700188};
189
190} // namespace impl
191} // namespace detail
192
193// this is the accessible definition of the trait to end user
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700194NDNBOOST_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 Thompsona28eed82013-08-22 16:21:10 -0700195
196} // namespace ndnboost
197
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700198#if defined(NDNBOOST_MSVC)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700199# pragma warning ( pop )
200#endif
201
202#include <ndnboost/type_traits/detail/bool_trait_undef.hpp>