1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
8 // http://opensource.org/licenses/MIT
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
15 #ifndef RAPIDJSON_INTERNAL_META_H_
16 #define RAPIDJSON_INTERNAL_META_H_
18 #include "../rapidjson.h"
22 RAPIDJSON_DIAG_OFF(effc++)
26 RAPIDJSON_DIAG_OFF(6334)
29 #if RAPIDJSON_HAS_CXX11_TYPETRAITS
30 #include <type_traits>
33 //@cond RAPIDJSON_INTERNAL
34 RAPIDJSON_NAMESPACE_BEGIN
37 // Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching
38 template <typename T> struct Void { typedef void Type; };
40 ///////////////////////////////////////////////////////////////////////////////
41 // BoolType, TrueType, FalseType
43 template <bool Cond> struct BoolType {
44 static const bool Value = Cond;
45 typedef BoolType Type;
47 typedef BoolType<true> TrueType;
48 typedef BoolType<false> FalseType;
51 ///////////////////////////////////////////////////////////////////////////////
52 // SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr
55 template <bool C> struct SelectIfImpl { template <typename T1, typename T2> struct Apply { typedef T1 Type; }; };
56 template <> struct SelectIfImpl<false> { template <typename T1, typename T2> struct Apply { typedef T2 Type; }; };
57 template <bool C, typename T1, typename T2> struct SelectIfCond : SelectIfImpl<C>::template Apply<T1,T2> {};
58 template <typename C, typename T1, typename T2> struct SelectIf : SelectIfCond<C::Value, T1, T2> {};
60 template <bool Cond1, bool Cond2> struct AndExprCond : FalseType {};
61 template <> struct AndExprCond<true, true> : TrueType {};
62 template <bool Cond1, bool Cond2> struct OrExprCond : TrueType {};
63 template <> struct OrExprCond<false, false> : FalseType {};
65 template <typename C> struct BoolExpr : SelectIf<C,TrueType,FalseType>::Type {};
66 template <typename C> struct NotExpr : SelectIf<C,FalseType,TrueType>::Type {};
67 template <typename C1, typename C2> struct AndExpr : AndExprCond<C1::Value, C2::Value>::Type {};
68 template <typename C1, typename C2> struct OrExpr : OrExprCond<C1::Value, C2::Value>::Type {};
71 ///////////////////////////////////////////////////////////////////////////////
72 // AddConst, MaybeAddConst, RemoveConst
73 template <typename T> struct AddConst { typedef const T Type; };
74 template <bool Constify, typename T> struct MaybeAddConst : SelectIfCond<Constify, const T, T> {};
75 template <typename T> struct RemoveConst { typedef T Type; };
76 template <typename T> struct RemoveConst<const T> { typedef T Type; };
79 ///////////////////////////////////////////////////////////////////////////////
80 // IsSame, IsConst, IsMoreConst, IsPointer
82 template <typename T, typename U> struct IsSame : FalseType {};
83 template <typename T> struct IsSame<T, T> : TrueType {};
85 template <typename T> struct IsConst : FalseType {};
86 template <typename T> struct IsConst<const T> : TrueType {};
88 template <typename CT, typename T>
90 : AndExpr<IsSame<typename RemoveConst<CT>::Type, typename RemoveConst<T>::Type>,
91 BoolType<IsConst<CT>::Value >= IsConst<T>::Value> >::Type {};
93 template <typename T> struct IsPointer : FalseType {};
94 template <typename T> struct IsPointer<T*> : TrueType {};
96 ///////////////////////////////////////////////////////////////////////////////
99 #if RAPIDJSON_HAS_CXX11_TYPETRAITS
101 template <typename B, typename D> struct IsBaseOf
102 : BoolType< ::std::is_base_of<B,D>::value> {};
104 #else // simplified version adopted from Boost
106 template<typename B, typename D> struct IsBaseOfImpl {
107 RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0);
108 RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0);
110 typedef char (&Yes)[1];
111 typedef char (&No) [2];
113 template <typename T>
114 static Yes Check(const D*, T);
115 static No Check(const B*, int);
118 operator const B*() const;
122 enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) };
125 template <typename B, typename D> struct IsBaseOf
126 : OrExpr<IsSame<B, D>, BoolExpr<IsBaseOfImpl<B, D> > >::Type {};
128 #endif // RAPIDJSON_HAS_CXX11_TYPETRAITS
131 //////////////////////////////////////////////////////////////////////////
132 // EnableIf / DisableIf
134 template <bool Condition, typename T = void> struct EnableIfCond { typedef T Type; };
135 template <typename T> struct EnableIfCond<false, T> { /* empty */ };
137 template <bool Condition, typename T = void> struct DisableIfCond { typedef T Type; };
138 template <typename T> struct DisableIfCond<true, T> { /* empty */ };
140 template <typename Condition, typename T = void>
141 struct EnableIf : EnableIfCond<Condition::Value, T> {};
143 template <typename Condition, typename T = void>
144 struct DisableIf : DisableIfCond<Condition::Value, T> {};
148 template <typename T> struct RemoveSfinaeTag;
149 template <typename T> struct RemoveSfinaeTag<SfinaeTag&(*)(T)> { typedef T Type; };
151 #define RAPIDJSON_REMOVEFPTR_(type) \
152 typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \
153 < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type
155 #define RAPIDJSON_ENABLEIF(cond) \
156 typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \
157 <RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL
159 #define RAPIDJSON_DISABLEIF(cond) \
160 typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \
161 <RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL
163 #define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \
164 typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \
165 <RAPIDJSON_REMOVEFPTR_(cond), \
166 RAPIDJSON_REMOVEFPTR_(returntype)>::Type
168 #define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \
169 typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \
170 <RAPIDJSON_REMOVEFPTR_(cond), \
171 RAPIDJSON_REMOVEFPTR_(returntype)>::Type
173 } // namespace internal
174 RAPIDJSON_NAMESPACE_END
177 #if defined(__GNUC__) || defined(_MSC_VER)
181 #endif // RAPIDJSON_INTERNAL_META_H_