2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author: Andrei Alexandrescu
19 #ifndef FOLLY_BASE_TRAITS_H_
20 #define FOLLY_BASE_TRAITS_H_
23 #include <type_traits>
25 #include <bits/c++config.h>
27 #include <boost/type_traits.hpp>
28 #include <boost/mpl/and.hpp>
29 #include <boost/mpl/has_xxx.hpp>
30 #include <boost/mpl/not.hpp>
35 * IsRelocatable<T>::value describes the ability of moving around
36 * memory a value of type T by using memcpy (as opposed to the
37 * conservative approach of calling the copy constructor and then
38 * destroying the old temporary. Essentially for a relocatable type,
39 * the following two sequences of code should be semantically
42 * void move1(T * from, T * to) {
47 * void move2(T * from, T * to) {
48 * memcpy(to, from, sizeof(T));
51 * Most C++ types are relocatable; the ones that aren't would include
52 * internal pointers or (very rarely) would need to update remote
53 * pointers to pointers tracking them. All C++ primitive types and
54 * type constructors are relocatable.
56 * This property can be used in a variety of optimizations. Currently
57 * fbvector uses this property intensively.
59 * The default conservatively assumes the type is not
60 * relocatable. Several specializations are defined for known
61 * types. You may want to add your own specializations. Do so in
62 * namespace folly and make sure you keep the specialization of
63 * IsRelocatable<SomeStruct> in the same header as SomeStruct.
65 * You may also declare a type to be relocatable by including
66 * `typedef std::true_type IsRelocatable;`
67 * in the class header.
69 * It may be unset in a base class by overriding the typedef to false_type.
72 * IsTriviallyCopyable describes the value semantics property. C++11 contains
73 * the type trait is_trivially_copyable; however, it is not yet implemented
74 * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
77 * IsZeroInitializable describes the property that default construction is the
78 * same as memset(dst, 0, sizeof(T)).
81 namespace traits_detail {
83 #define FOLLY_HAS_TRUE_XXX(name) \
84 BOOST_MPL_HAS_XXX_TRAIT_DEF(name); \
85 template <class T> struct name ## _is_true \
86 : std::is_same<typename T::name, std::true_type> {}; \
87 template <class T> struct has_true_ ## name \
89 has_ ## name <T>::value, \
90 name ## _is_true<T>, \
94 FOLLY_HAS_TRUE_XXX(IsRelocatable)
95 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
96 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
98 #undef FOLLY_HAS_TRUE_XXX
101 template <class T> struct IsTriviallyCopyable
102 : std::integral_constant<bool,
103 !std::is_class<T>::value ||
104 // TODO: add alternate clause is_trivially_copyable, when available
105 traits_detail::has_true_IsTriviallyCopyable<T>::value
108 template <class T> struct IsRelocatable
109 : std::integral_constant<bool,
110 !std::is_class<T>::value ||
111 // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
112 //std::is_trivially_move_constructible<T>::value ||
113 IsTriviallyCopyable<T>::value ||
114 traits_detail::has_true_IsRelocatable<T>::value
117 template <class T> struct IsZeroInitializable
118 : std::integral_constant<bool,
119 !std::is_class<T>::value ||
120 traits_detail::has_true_IsZeroInitializable<T>::value
126 * Use this macro ONLY inside namespace folly. When using it with a
127 * regular type, use it like this:
129 * // Make sure you're at namespace ::folly scope
130 * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
132 * When using it with a template type, use it like this:
134 * // Make sure you're at namespace ::folly scope
135 * template<class T1, class T2>
136 * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
138 #define FOLLY_ASSUME_RELOCATABLE(...) \
139 struct IsRelocatable< __VA_ARGS__ > : std::true_type {};
142 * Use this macro ONLY inside namespace boost. When using it with a
143 * regular type, use it like this:
145 * // Make sure you're at namespace ::boost scope
146 * template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType)
148 * When using it with a template type, use it like this:
150 * // Make sure you're at namespace ::boost scope
151 * template<class T1, class T2>
152 * FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType<T1, T2>)
154 #define FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(...) \
155 struct has_nothrow_constructor< __VA_ARGS__ > : ::boost::true_type {};
158 * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode two
159 * assumptions: first, that the type is relocatable per IsRelocatable
160 * above, and that it has a nothrow constructor. Most types can be
161 * assumed to satisfy both conditions, but it is the responsibility of
162 * the user to state that assumption. User-defined classes will not
163 * work with fbvector (see FBVector.h) unless they state this
164 * combination of properties.
166 * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
168 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
170 * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
171 * allow using the macro for describing templatized classes with 1, 2,
172 * 3, and 4 template parameters respectively. For template classes
173 * just use the macro with the appropriate number and pass the name of
174 * the template to it. Example:
176 * template <class T1, class T2> class MyType { ... };
178 * // Make sure you're at global scope
179 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
182 // Use this macro ONLY at global level (no namespace)
183 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
184 namespace folly { template<> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) } \
186 template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__) }
187 // Use this macro ONLY at global level (no namespace)
188 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
190 template <class T1> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) } \
192 template <class T1> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1>) }
193 // Use this macro ONLY at global level (no namespace)
194 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
196 template <class T1, class T2> \
197 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) } \
199 template <class T1, class T2> \
200 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2>) }
201 // Use this macro ONLY at global level (no namespace)
202 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \
204 template <class T1, class T2, class T3> \
205 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) } \
207 template <class T1, class T2, class T3> \
208 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3>) }
209 // Use this macro ONLY at global level (no namespace)
210 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \
212 template <class T1, class T2, class T3, class T4> \
213 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) } \
215 template <class T1, class T2, class T3, class T4> \
216 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3, T4>) }
219 * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
220 * safe to assume that pair is compatible if both of its components
221 * are. Furthermore, all STL containers can be assumed to comply,
222 * although that is not guaranteed by the standard.
227 template <class T, class U>
229 #ifndef _GLIBCXX_USE_FB
230 template <class T, class R, class A>
233 template <class T, class R, class A, class S>
236 template <class T, class A>
238 template <class T, class A>
240 template <class T, class A>
242 template <class T, class C, class A>
244 template <class K, class V, class C, class A>
253 template <class T> class shared_ptr;
255 template <class T, class U>
256 struct has_nothrow_constructor< std::pair<T, U> >
257 : ::boost::mpl::and_< has_nothrow_constructor<T>,
258 has_nothrow_constructor<U> > {};
264 // STL commonly-used types
265 template <class T, class U>
266 struct IsRelocatable< std::pair<T, U> >
267 : ::boost::mpl::and_< IsRelocatable<T>, IsRelocatable<U> > {};
269 // Is T one of T1, T2, ..., Tn?
270 template <class T, class... Ts>
272 enum { value = false };
275 template <class T, class T1, class... Ts>
276 struct IsOneOf<T, T1, Ts...> {
277 enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
282 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string);
283 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector);
284 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list);
285 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque);
286 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(std::map);
287 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::set);
288 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr);
289 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr);
290 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function);
293 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr);
295 #endif //FOLLY_BASE_TRAITS_H_