meta.h Source File

meta.h Source File#

Composable Kernel: meta.h Source File
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.
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#ifdef __GNUC__
21RAPIDJSON_DIAG_PUSH
22RAPIDJSON_DIAG_OFF(effc++)
23#endif
24
25#if defined(_MSC_VER) && !defined(__clang__)
26RAPIDJSON_DIAG_PUSH
27RAPIDJSON_DIAG_OFF(6334)
28#endif
29
30#if RAPIDJSON_HAS_CXX11_TYPETRAITS
31#include <type_traits>
32#endif
33
34//@cond RAPIDJSON_INTERNAL
36namespace internal {
37
38// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching
39template <typename T>
40struct Void
41{
42 typedef void Type;
43};
44
46// BoolType, TrueType, FalseType
47//
48template <bool Cond>
49struct BoolType
50{
51 static const bool Value = Cond;
52 typedef BoolType Type;
53};
54typedef BoolType<true> TrueType;
55typedef BoolType<false> FalseType;
56
58// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr
59//
60
61template <bool C>
62struct SelectIfImpl
63{
64 template <typename T1, typename T2>
65 struct Apply
66 {
67 typedef T1 Type;
68 };
69};
70template <>
71struct SelectIfImpl<false>
72{
73 template <typename T1, typename T2>
74 struct Apply
75 {
76 typedef T2 Type;
77 };
78};
79template <bool C, typename T1, typename T2>
80struct SelectIfCond : SelectIfImpl<C>::template Apply<T1, T2>
81{
82};
83template <typename C, typename T1, typename T2>
84struct SelectIf : SelectIfCond<C::Value, T1, T2>
85{
86};
87
88template <bool Cond1, bool Cond2>
89struct AndExprCond : FalseType
90{
91};
92template <>
93struct AndExprCond<true, true> : TrueType
94{
95};
96template <bool Cond1, bool Cond2>
97struct OrExprCond : TrueType
98{
99};
100template <>
101struct OrExprCond<false, false> : FalseType
102{
103};
104
105template <typename C>
106struct BoolExpr : SelectIf<C, TrueType, FalseType>::Type
107{
108};
109template <typename C>
110struct NotExpr : SelectIf<C, FalseType, TrueType>::Type
111{
112};
113template <typename C1, typename C2>
114struct AndExpr : AndExprCond<C1::Value, C2::Value>::Type
115{
116};
117template <typename C1, typename C2>
118struct OrExpr : OrExprCond<C1::Value, C2::Value>::Type
119{
120};
121
123// AddConst, MaybeAddConst, RemoveConst
124template <typename T>
125struct AddConst
126{
127 typedef const T Type;
128};
129template <bool Constify, typename T>
130struct MaybeAddConst : SelectIfCond<Constify, const T, T>
131{
132};
133template <typename T>
134struct RemoveConst
135{
136 typedef T Type;
137};
138template <typename T>
139struct RemoveConst<const T>
140{
141 typedef T Type;
142};
143
145// IsSame, IsConst, IsMoreConst, IsPointer
146//
147template <typename T, typename U>
148struct IsSame : FalseType
149{
150};
151template <typename T>
152struct IsSame<T, T> : TrueType
153{
154};
155
156template <typename T>
157struct IsConst : FalseType
158{
159};
160template <typename T>
161struct IsConst<const T> : TrueType
162{
163};
164
165template <typename CT, typename T>
166struct IsMoreConst : AndExpr<IsSame<typename RemoveConst<CT>::Type, typename RemoveConst<T>::Type>,
167 BoolType<IsConst<CT>::Value >= IsConst<T>::Value>>::Type
168{
169};
170
171template <typename T>
172struct IsPointer : FalseType
173{
174};
175template <typename T>
176struct IsPointer<T*> : TrueType
177{
178};
179
181// IsBaseOf
182//
183#if RAPIDJSON_HAS_CXX11_TYPETRAITS
184
185template <typename B, typename D>
186struct IsBaseOf : BoolType<::std::is_base_of<B, D>::value>
187{
188};
189
190#else // simplified version adopted from Boost
191
192template <typename B, typename D>
193struct 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
217template <typename B, typename D>
218struct IsBaseOf : OrExpr<IsSame<B, D>, BoolExpr<IsBaseOfImpl<B, D>>>::Type
219{
220};
221
222#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS
223
225// EnableIf / DisableIf
226//
227template <bool Condition, typename T = void>
228struct EnableIfCond
229{
230 typedef T Type;
231};
232template <typename T>
233struct EnableIfCond<false, T>
234{ /* empty */
235};
236
237template <bool Condition, typename T = void>
238struct DisableIfCond
239{
240 typedef T Type;
241};
242template <typename T>
243struct DisableIfCond<true, T>
244{ /* empty */
245};
246
247template <typename Condition, typename T = void>
248struct EnableIf : EnableIfCond<Condition::Value, T>
249{
250};
251
252template <typename Condition, typename T = void>
253struct DisableIf : DisableIfCond<Condition::Value, T>
254{
255};
256
257// SFINAE helpers
258struct SfinaeTag
259{
260};
261template <typename T>
262struct RemoveSfinaeTag;
263template <typename T>
264struct RemoveSfinaeTag<SfinaeTag& (*)(T)>
265{
266 typedef T Type;
267};
268
269#define RAPIDJSON_REMOVEFPTR_(type) \
270 typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag< \
271 ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*)type>::Type
272
273#define RAPIDJSON_ENABLEIF(cond) \
274 typename ::RAPIDJSON_NAMESPACE::internal::EnableIf<RAPIDJSON_REMOVEFPTR_(cond)>::Type* = NULL
275
276#define RAPIDJSON_DISABLEIF(cond) \
277 typename ::RAPIDJSON_NAMESPACE::internal::DisableIf<RAPIDJSON_REMOVEFPTR_(cond)>::Type* = NULL
278
279#define RAPIDJSON_ENABLEIF_RETURN(cond, returntype) \
280 typename ::RAPIDJSON_NAMESPACE::internal::EnableIf<RAPIDJSON_REMOVEFPTR_(cond), \
281 RAPIDJSON_REMOVEFPTR_(returntype)>::Type
282
283#define RAPIDJSON_DISABLEIF_RETURN(cond, returntype) \
284 typename ::RAPIDJSON_NAMESPACE::internal::DisableIf<RAPIDJSON_REMOVEFPTR_(cond), \
285 RAPIDJSON_REMOVEFPTR_(returntype)>::Type
286
287} // namespace internal
289//@endcond
290
291#if defined(_MSC_VER) && !defined(__clang__)
292RAPIDJSON_DIAG_POP
293#endif
294
295#ifdef __GNUC__
296RAPIDJSON_DIAG_POP
297#endif
298
299#endif // RAPIDJSON_INTERNAL_META_H_
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition document.h:3124
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:124
Definition allocators.h:459
common definitions and configuration
Type
Type of JSON value.
Definition rapidjson.h:760
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition rapidjson.h:500