webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
meta.h
Go to the documentation of this file.
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_INTERNAL_META_H_
22 #define RAPIDJSON_INTERNAL_META_H_
23 
24 #ifndef RAPIDJSON_RAPIDJSON_H_
25 #error <rapidjson.h> not yet included. Do not include this file directly.
26 #endif
27 
28 #ifdef __GNUC__
29 RAPIDJSON_DIAG_PUSH
30 RAPIDJSON_DIAG_OFF(effc++)
31 #endif
32 #if defined(_MSC_VER)
33 RAPIDJSON_DIAG_PUSH
34 RAPIDJSON_DIAG_OFF(6334)
35 #endif
36 
37 #if RAPIDJSON_HAS_CXX11_TYPETRAITS
38 #include <type_traits>
39 #endif
40 
41 //@cond RAPIDJSON_INTERNAL
43 namespace internal {
44 
45 // Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching
46 template <typename T> struct Void { typedef void Type; };
47 
49 // BoolType, TrueType, FalseType
50 //
51 template <bool Cond> struct BoolType {
52  static const bool Value = Cond;
53  typedef BoolType Type;
54 };
55 typedef BoolType<true> TrueType;
56 typedef BoolType<false> FalseType;
57 
58 
60 // SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr
61 //
62 
63 template <bool C> struct SelectIfImpl { template <typename T1, typename T2> struct Apply { typedef T1 Type; }; };
64 template <> struct SelectIfImpl<false> { template <typename T1, typename T2> struct Apply { typedef T2 Type; }; };
65 template <bool C, typename T1, typename T2> struct SelectIfCond : SelectIfImpl<C>::template Apply<T1,T2> {};
66 template <typename C, typename T1, typename T2> struct SelectIf : SelectIfCond<C::Value, T1, T2> {};
67 
68 template <bool Cond1, bool Cond2> struct AndExprCond : FalseType {};
69 template <> struct AndExprCond<true, true> : TrueType {};
70 template <bool Cond1, bool Cond2> struct OrExprCond : TrueType {};
71 template <> struct OrExprCond<false, false> : FalseType {};
72 
73 template <typename C> struct BoolExpr : SelectIf<C,TrueType,FalseType>::Type {};
74 template <typename C> struct NotExpr : SelectIf<C,FalseType,TrueType>::Type {};
75 template <typename C1, typename C2> struct AndExpr : AndExprCond<C1::Value, C2::Value>::Type {};
76 template <typename C1, typename C2> struct OrExpr : OrExprCond<C1::Value, C2::Value>::Type {};
77 
78 
80 // AddConst, MaybeAddConst, RemoveConst
81 template <typename T> struct AddConst { typedef const T Type; };
82 template <bool Constify, typename T> struct MaybeAddConst : SelectIfCond<Constify, const T, T> {};
83 template <typename T> struct RemoveConst { typedef T Type; };
84 template <typename T> struct RemoveConst<const T> { typedef T Type; };
85 
86 
88 // IsSame, IsConst, IsMoreConst, IsPointer
89 //
90 template <typename T, typename U> struct IsSame : FalseType {};
91 template <typename T> struct IsSame<T, T> : TrueType {};
92 
93 template <typename T> struct IsConst : FalseType {};
94 template <typename T> struct IsConst<const T> : TrueType {};
95 
96 template <typename CT, typename T>
97 struct IsMoreConst
98  : AndExpr<IsSame<typename RemoveConst<CT>::Type, typename RemoveConst<T>::Type>,
99  BoolType<IsConst<CT>::Value >= IsConst<T>::Value> >::Type {};
100 
101 template <typename T> struct IsPointer : FalseType {};
102 template <typename T> struct IsPointer<T*> : TrueType {};
103 
105 // IsBaseOf
106 //
107 #if RAPIDJSON_HAS_CXX11_TYPETRAITS
108 
109 template <typename B, typename D> struct IsBaseOf
110  : BoolType< ::std::is_base_of<B,D>::value> {};
111 
112 #else // simplified version adopted from Boost
113 
114 template<typename B, typename D> struct IsBaseOfImpl {
115  RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0);
116  RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0);
117 
118  typedef char (&Yes)[1];
119  typedef char (&No) [2];
120 
121  template <typename T>
122  static Yes Check(const D*, T);
123  static No Check(const B*, int);
124 
125  struct Host {
126  operator const B*() const;
127  operator const D*();
128  };
129 
130  enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) };
131 };
132 
133 template <typename B, typename D> struct IsBaseOf
134  : OrExpr<IsSame<B, D>, BoolExpr<IsBaseOfImpl<B, D> > >::Type {};
135 
136 #endif // RAPIDJSON_HAS_CXX11_TYPETRAITS
137 
138 
140 // EnableIf / DisableIf
141 //
142 template <bool Condition, typename T = void> struct EnableIfCond { typedef T Type; };
143 template <typename T> struct EnableIfCond<false, T> { /* empty */ };
144 
145 template <bool Condition, typename T = void> struct DisableIfCond { typedef T Type; };
146 template <typename T> struct DisableIfCond<true, T> { /* empty */ };
147 
148 template <typename Condition, typename T = void>
149 struct EnableIf : EnableIfCond<Condition::Value, T> {};
150 
151 template <typename Condition, typename T = void>
152 struct DisableIf : DisableIfCond<Condition::Value, T> {};
153 
154 // SFINAE helpers
155 struct SfinaeTag {};
156 template <typename T> struct RemoveSfinaeTag;
157 template <typename T> struct RemoveSfinaeTag<SfinaeTag&(*)(T)> { typedef T Type; };
158 
159 #define RAPIDJSON_REMOVEFPTR_(type) \
160  typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \
161  < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type
162 
163 #define RAPIDJSON_ENABLEIF(cond) \
164  typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \
165  <RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL
166 
167 #define RAPIDJSON_DISABLEIF(cond) \
168  typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \
169  <RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL
170 
171 #define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \
172  typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \
173  <RAPIDJSON_REMOVEFPTR_(cond), \
174  RAPIDJSON_REMOVEFPTR_(returntype)>::Type
175 
176 #define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \
177  typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \
178  <RAPIDJSON_REMOVEFPTR_(cond), \
179  RAPIDJSON_REMOVEFPTR_(returntype)>::Type
180 
181 } // namespace internal
183 //@endcond
184 
185 #if defined(__GNUC__) || defined(_MSC_VER)
186 RAPIDJSON_DIAG_POP
187 #endif
188 
189 #endif // RAPIDJSON_INTERNAL_META_H_
#define B(x, j)
Definition: sha512.c:425
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:346
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
string template
Definition: gen_dxgi_support_tables.py:21
TestSubObjConstructor T
Definition: TestTypedefs.idl:84
Definition: type_traits_unittest.cc:95
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
#define true
Definition: float-mm.c:6
Definition: document.h:393
#define false
Definition: float-mm.c:5
const
Definition: upload.py:398
Type
Type of JSON value.
Definition: rapidjson.h:616
#define T(a)
Definition: row_common.cc:1964