blob: 8dc35afad34c914f0ce1d1c5c347bb9acfe032a2 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001
2// (C) Copyright Rani Sharoni 2003.
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -07009#ifndef NDNBOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED
10#define NDNBOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED
Jeff Thompsona28eed82013-08-22 16:21:10 -070011
12#include <ndnboost/type_traits/intrinsics.hpp>
Jeff Thompson3d613fd2013-10-15 15:39:04 -070013#ifndef NDNBOOST_IS_BASE_OF
Jeff Thompsona28eed82013-08-22 16:21:10 -070014#include <ndnboost/type_traits/is_class.hpp>
15#include <ndnboost/type_traits/is_same.hpp>
16#include <ndnboost/type_traits/is_convertible.hpp>
17#include <ndnboost/type_traits/detail/ice_and.hpp>
18#include <ndnboost/config.hpp>
19#include <ndnboost/static_assert.hpp>
20#endif
21#include <ndnboost/type_traits/remove_cv.hpp>
22
23// should be the last #include
24#include <ndnboost/type_traits/detail/bool_trait_def.hpp>
25
26namespace ndnboost {
27
28namespace detail {
29
Jeff Thompson3d613fd2013-10-15 15:39:04 -070030#ifndef NDNBOOST_IS_BASE_OF
31#if !NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x581)) \
32 && !NDNBOOST_WORKAROUND(__SUNPRO_CC , <= 0x540) \
33 && !NDNBOOST_WORKAROUND(__EDG_VERSION__, <= 243) \
34 && !NDNBOOST_WORKAROUND(__DMC__, NDNBOOST_TESTED_AT(0x840))
Jeff Thompsona28eed82013-08-22 16:21:10 -070035
36 // The EDG version number is a lower estimate.
37 // It is not currently known which EDG version
38 // exactly fixes the problem.
39
40/*************************************************************************
41
42This version detects ambiguous base classes and private base classes
43correctly, and was devised by Rani Sharoni.
44
45Explanation by Terje Slettebo and Rani Sharoni.
46
47Let's take the multiple base class below as an example, and the following
48will also show why there's not a problem with private or ambiguous base
49class:
50
51struct B {};
52struct B1 : B {};
53struct B2 : B {};
54struct D : private B1, private B2 {};
55
56is_base_and_derived<B, D>::value;
57
58First, some terminology:
59
60SC - Standard conversion
61UDC - User-defined conversion
62
63A user-defined conversion sequence consists of an SC, followed by an UDC,
64followed by another SC. Either SC may be the identity conversion.
65
66When passing the default-constructed Host object to the overloaded check_sig()
67functions (initialization 8.5/14/4/3), we have several viable implicit
68conversion sequences:
69
70For "static no_type check_sig(B const volatile *, int)" we have the conversion
71sequences:
72
73C -> C const (SC - Qualification Adjustment) -> B const volatile* (UDC)
74C -> D const volatile* (UDC) -> B1 const volatile* / B2 const volatile* ->
75 B const volatile* (SC - Conversion)
76
77For "static yes_type check_sig(D const volatile *, T)" we have the conversion
78sequence:
79
80C -> D const volatile* (UDC)
81
82According to 13.3.3.1/4, in context of user-defined conversion only the
83standard conversion sequence is considered when selecting the best viable
84function, so it only considers up to the user-defined conversion. For the
85first function this means choosing between C -> C const and C -> C, and it
86chooses the latter, because it's a proper subset (13.3.3.2/3/2) of the
87former. Therefore, we have:
88
89C -> D const volatile* (UDC) -> B1 const volatile* / B2 const volatile* ->
90 B const volatile* (SC - Conversion)
91C -> D const volatile* (UDC)
92
93Here, the principle of the "shortest subsequence" applies again, and it
94chooses C -> D const volatile*. This shows that it doesn't even need to
95consider the multiple paths to B, or accessibility, as that possibility is
96eliminated before it could possibly cause ambiguity or access violation.
97
98If D is not derived from B, it has to choose between C -> C const -> B const
99volatile* for the first function, and C -> D const volatile* for the second
100function, which are just as good (both requires a UDC, 13.3.3.2), had it not
101been for the fact that "static no_type check_sig(B const volatile *, int)" is
102not templated, which makes C -> C const -> B const volatile* the best choice
103(13.3.3/1/4), resulting in "no".
104
105Also, if Host::operator B const volatile* hadn't been const, the two
106conversion sequences for "static no_type check_sig(B const volatile *, int)", in
107the case where D is derived from B, would have been ambiguous.
108
109See also
110http://groups.google.com/groups?selm=df893da6.0301280859.522081f7%40posting.
111google.com and links therein.
112
113*************************************************************************/
114
115template <typename B, typename D>
116struct bd_helper
117{
118 //
119 // This VC7.1 specific workaround stops the compiler from generating
120 // an internal compiler error when compiling with /vmg (thanks to
121 // Aleksey Gurtovoy for figuring out the workaround).
122 //
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700123#if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, == 1310)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700124 template <typename T>
125 static type_traits::yes_type check_sig(D const volatile *, T);
126 static type_traits::no_type check_sig(B const volatile *, int);
127#else
128 static type_traits::yes_type check_sig(D const volatile *, long);
129 static type_traits::no_type check_sig(B const volatile * const&, int);
130#endif
131};
132
133template<typename B, typename D>
134struct is_base_and_derived_impl2
135{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700136#if NDNBOOST_WORKAROUND(NDNBOOST_MSVC_FULL_VER, >= 140050000)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700137#pragma warning(push)
138#pragma warning(disable:6334)
139#endif
140 //
141 // May silently do the wrong thing with incomplete types
142 // unless we trap them here:
143 //
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700144 NDNBOOST_STATIC_ASSERT(sizeof(B) != 0);
145 NDNBOOST_STATIC_ASSERT(sizeof(D) != 0);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700146
147 struct Host
148 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700149#if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, == 1310)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700150 operator B const volatile *() const;
151#else
152 operator B const volatile * const&() const;
153#endif
154 operator D const volatile *();
155 };
156
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700157 NDNBOOST_STATIC_CONSTANT(bool, value =
Jeff Thompsona28eed82013-08-22 16:21:10 -0700158 sizeof(bd_helper<B,D>::check_sig(Host(), 0)) == sizeof(type_traits::yes_type));
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700159#if NDNBOOST_WORKAROUND(NDNBOOST_MSVC_FULL_VER, >= 140050000)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700160#pragma warning(pop)
161#endif
162};
163
164#else
165
166//
167// broken version:
168//
169template<typename B, typename D>
170struct is_base_and_derived_impl2
171{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700172 NDNBOOST_STATIC_CONSTANT(bool, value =
Jeff Thompsona28eed82013-08-22 16:21:10 -0700173 (::ndnboost::is_convertible<D*,B*>::value));
174};
175
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700176#define NDNBOOST_BROKEN_IS_BASE_AND_DERIVED
Jeff Thompsona28eed82013-08-22 16:21:10 -0700177
178#endif
179
180template <typename B, typename D>
181struct is_base_and_derived_impl3
182{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700183 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700184};
185
186template <bool ic1, bool ic2, bool iss>
187struct is_base_and_derived_select
188{
189 template <class T, class U>
190 struct rebind
191 {
192 typedef is_base_and_derived_impl3<T,U> type;
193 };
194};
195
196template <>
197struct is_base_and_derived_select<true,true,false>
198{
199 template <class T, class U>
200 struct rebind
201 {
202 typedef is_base_and_derived_impl2<T,U> type;
203 };
204};
205
206template <typename B, typename D>
207struct is_base_and_derived_impl
208{
209 typedef typename remove_cv<B>::type ncvB;
210 typedef typename remove_cv<D>::type ncvD;
211
212 typedef is_base_and_derived_select<
213 ::ndnboost::is_class<B>::value,
214 ::ndnboost::is_class<D>::value,
215 ::ndnboost::is_same<ncvB,ncvD>::value> selector;
216 typedef typename selector::template rebind<ncvB,ncvD> binder;
217 typedef typename binder::type bound_type;
218
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700219 NDNBOOST_STATIC_CONSTANT(bool, value = bound_type::value);
Jeff Thompsona28eed82013-08-22 16:21:10 -0700220};
221#else
222template <typename B, typename D>
223struct is_base_and_derived_impl
224{
225 typedef typename remove_cv<B>::type ncvB;
226 typedef typename remove_cv<D>::type ncvD;
227
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700228 NDNBOOST_STATIC_CONSTANT(bool, value = (NDNBOOST_IS_BASE_OF(B,D) && ! ::ndnboost::is_same<ncvB,ncvD>::value));
Jeff Thompsona28eed82013-08-22 16:21:10 -0700229};
230#endif
231} // namespace detail
232
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700233NDNBOOST_TT_AUX_BOOL_TRAIT_DEF2(
Jeff Thompsona28eed82013-08-22 16:21:10 -0700234 is_base_and_derived
235 , Base
236 , Derived
237 , (::ndnboost::detail::is_base_and_derived_impl<Base,Derived>::value)
238 )
239
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700240#ifndef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
241NDNBOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC2_2(typename Base,typename Derived,is_base_and_derived,Base&,Derived,false)
242NDNBOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC2_2(typename Base,typename Derived,is_base_and_derived,Base,Derived&,false)
243NDNBOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC2_2(typename Base,typename Derived,is_base_and_derived,Base&,Derived&,false)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700244#endif
245
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700246#if NDNBOOST_WORKAROUND(__CODEGEARC__, NDNBOOST_TESTED_AT(0x610))
247NDNBOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC2_1(typename Base,is_base_and_derived,Base,Base,false)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700248#endif
249
250} // namespace ndnboost
251
252#include <ndnboost/type_traits/detail/bool_trait_undef.hpp>
253
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700254#endif // NDNBOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED