meta.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
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
7 //
8 // http://opensource.org/licenses/MIT
9 //
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.
14 
15 #ifndef RAPIDJSON_INTERNAL_META_H_
16 #define RAPIDJSON_INTERNAL_META_H_
17 
18 #include "../rapidjson.h"
19 
20 #if __GNUC__
21 RAPIDJSON_DIAG_PUSH
22 RAPIDJSON_DIAG_OFF(effc++)
23 #endif
24 
25 #if defined(_MSC_VER) && !defined(__clang__)
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(6334)
28 #endif
29 
30 #if RAPIDJSON_HAS_CXX11_TYPETRAITS
31 #include <type_traits>
32 #endif
33 
34 //@cond RAPIDJSON_INTERNAL
36 namespace internal
37 {
38 // Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching
39 template <typename T>
40 struct Void
41 {
42  typedef void Type;
43 };
44 
46 // BoolType, TrueType, FalseType
47 //
48 template <bool Cond>
49 struct BoolType
50 {
51  static const bool Value = Cond;
52  typedef BoolType Type;
53 };
54 typedef BoolType<true> TrueType;
55 typedef BoolType<false> FalseType;
56 
58 // SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr
59 //
60 
61 template <bool C>
62 struct SelectIfImpl
63 {
64  template <typename T1, typename T2>
65  struct Apply
66  {
67  typedef T1 Type;
68  };
69 };
70 template <>
71 struct SelectIfImpl<false>
72 {
73  template <typename T1, typename T2>
74  struct Apply
75  {
76  typedef T2 Type;
77  };
78 };
79 template <bool C, typename T1, typename T2>
80 struct SelectIfCond : SelectIfImpl<C>::template Apply<T1, T2>
81 {
82 };
83 template <typename C, typename T1, typename T2>
84 struct SelectIf : SelectIfCond<C::Value, T1, T2>
85 {
86 };
87 
88 template <bool Cond1, bool Cond2>
89 struct AndExprCond : FalseType
90 {
91 };
92 template <>
93 struct AndExprCond<true, true> : TrueType
94 {
95 };
96 template <bool Cond1, bool Cond2>
97 struct OrExprCond : TrueType
98 {
99 };
100 template <>
101 struct OrExprCond<false, false> : FalseType
102 {
103 };
104 
105 template <typename C>
106 struct BoolExpr : SelectIf<C, TrueType, FalseType>::Type
107 {
108 };
109 template <typename C>
110 struct NotExpr : SelectIf<C, FalseType, TrueType>::Type
111 {
112 };
113 template <typename C1, typename C2>
114 struct AndExpr : AndExprCond<C1::Value, C2::Value>::Type
115 {
116 };
117 template <typename C1, typename C2>
118 struct OrExpr : OrExprCond<C1::Value, C2::Value>::Type
119 {
120 };
121 
123 // AddConst, MaybeAddConst, RemoveConst
124 template <typename T>
125 struct AddConst
126 {
127  typedef const T Type;
128 };
129 template <bool Constify, typename T>
130 struct MaybeAddConst : SelectIfCond<Constify, const T, T>
131 {
132 };
133 template <typename T>
134 struct RemoveConst
135 {
136  typedef T Type;
137 };
138 template <typename T>
139 struct RemoveConst<const T>
140 {
141  typedef T Type;
142 };
143 
145 // IsSame, IsConst, IsMoreConst, IsPointer
146 //
147 template <typename T, typename U>
148 struct IsSame : FalseType
149 {
150 };
151 template <typename T>
152 struct IsSame<T, T> : TrueType
153 {
154 };
155 
156 template <typename T>
157 struct IsConst : FalseType
158 {
159 };
160 template <typename T>
161 struct IsConst<const T> : TrueType
162 {
163 };
164 
165 template <typename CT, typename T>
166 struct IsMoreConst : AndExpr<IsSame<typename RemoveConst<CT>::Type, typename RemoveConst<T>::Type>,
167  BoolType<IsConst<CT>::Value >= IsConst<T>::Value> >::Type
168 {
169 };
170 
171 template <typename T>
172 struct IsPointer : FalseType
173 {
174 };
175 template <typename T>
176 struct IsPointer<T*> : TrueType
177 {
178 };
179 
181 // IsBaseOf
182 //
183 #if RAPIDJSON_HAS_CXX11_TYPETRAITS
184 
185 template <typename B, typename D>
186 struct IsBaseOf : BoolType< ::std::is_base_of<B, D>::value>
187 {
188 };
189 
190 #else // simplified version adopted from Boost
191 
192 template <typename B, typename D>
193 struct IsBaseOfImpl
194 {
195  RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0);
196  RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0);
197 
198  typedef char (&Yes)[1];
199  typedef char (&No)[2];
200 
201  template <typename T>
202  static Yes Check(const D*, T);
203  static No Check(const B*, int);
204 
205  struct Host
206  {
207  operator const B*() const;
208  operator const D*();
209  };
210 
211  enum
212  {
213  Value = (sizeof(Check(Host(), 0)) == sizeof(Yes))
214  };
215 };
216 
217 template <typename B, typename D>
218 struct IsBaseOf : OrExpr<IsSame<B, D>, BoolExpr<IsBaseOfImpl<B, D> > >::Type
219 {
220 };
221 
222 #endif // RAPIDJSON_HAS_CXX11_TYPETRAITS
223 
225 // EnableIf / DisableIf
226 //
227 template <bool Condition, typename T = void>
228 struct EnableIfCond
229 {
230  typedef T Type;
231 };
232 template <typename T>
233 struct EnableIfCond<false, T>
234 { /* empty */
235 };
236 
237 template <bool Condition, typename T = void>
238 struct DisableIfCond
239 {
240  typedef T Type;
241 };
242 template <typename T>
243 struct DisableIfCond<true, T>
244 { /* empty */
245 };
246 
247 template <typename Condition, typename T = void>
248 struct EnableIf : EnableIfCond<Condition::Value, T>
249 {
250 };
251 
252 template <typename Condition, typename T = void>
253 struct DisableIf : DisableIfCond<Condition::Value, T>
254 {
255 };
256 
257 // SFINAE helpers
258 struct SfinaeTag
259 {
260 };
261 template <typename T>
262 struct RemoveSfinaeTag;
263 template <typename T>
264 struct RemoveSfinaeTag<SfinaeTag& (*)(T)>
265 {
266  typedef T Type;
267 };
268 
269 #define RAPIDJSON_REMOVEFPTR_(type) \
270  typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag< ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*)type>::Type
271 
272 #define RAPIDJSON_ENABLEIF(cond) \
273  typename ::RAPIDJSON_NAMESPACE::internal::EnableIf<RAPIDJSON_REMOVEFPTR_(cond)>::Type* = NULL
274 
275 #define RAPIDJSON_DISABLEIF(cond) \
276  typename ::RAPIDJSON_NAMESPACE::internal::DisableIf<RAPIDJSON_REMOVEFPTR_(cond)>::Type* = NULL
277 
278 #define RAPIDJSON_ENABLEIF_RETURN(cond, returntype) \
279  typename ::RAPIDJSON_NAMESPACE::internal::EnableIf<RAPIDJSON_REMOVEFPTR_(cond), \
280  RAPIDJSON_REMOVEFPTR_(returntype)>::Type
281 
282 #define RAPIDJSON_DISABLEIF_RETURN(cond, returntype) \
283  typename ::RAPIDJSON_NAMESPACE::internal::DisableIf<RAPIDJSON_REMOVEFPTR_(cond), \
284  RAPIDJSON_REMOVEFPTR_(returntype)>::Type
285 
286 } // namespace internal
288 //@endcond
289 
290 #if defined(_MSC_VER) && !defined(__clang__)
291 RAPIDJSON_DIAG_POP
292 #endif
293 
294 #if __GNUC__
295 RAPIDJSON_DIAG_POP
296 #endif
297 
298 #endif // RAPIDJSON_INTERNAL_META_H_
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:54
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:465
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
Type
Type of JSON value.
Definition: rapidjson.h:666


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53