2 * Copyright 2013 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 };
281 * Complementary type traits to check for a negative value.
283 * if(x < 0) yields an error in clang for unsigned types when -Werror is used
288 template <typename T, bool>
289 struct is_negative_impl {
290 constexpr static bool check(T x) { return x < 0; }
293 template <typename T>
294 struct is_negative_impl<T, false> {
295 constexpr static bool check(T x) { return false; }
298 } // namespace detail {
300 template <typename T>
301 constexpr bool is_negative(T x) {
302 return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
307 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string);
308 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector);
309 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list);
310 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque);
311 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(std::map);
312 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::set);
313 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr);
314 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr);
315 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function);
318 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr);
320 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
321 template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
322 class classname<TTheClass_, RTheReturn_(TTheArgs_...) cv_qual> { \
324 typename UTheClass_, RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual \
325 > struct sfinae {}; \
326 template <typename UTheClass_> \
327 constexpr static bool test(sfinae<UTheClass_, &UTheClass_::func_name>*) \
329 template <typename> \
330 constexpr static bool test(...) { return false; } \
332 constexpr static bool value = test<TTheClass_>(nullptr); \
336 * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
337 * classes that check for the existence of a member function with
338 * a given name and signature. It currently does not support
339 * checking for inherited members.
341 * Such classes receive two template parameters: the class to be checked
342 * and the signature of the member function. A static boolean field
343 * named `value` (which is also constexpr) tells whether such member
346 * Each traits class created is bound only to the member name, not to
347 * its signature nor to the type of the class containing it.
349 * Say you need to know if a given class has a member function named
350 * `test` with the following signature:
354 * You'd need this macro to create a traits class to check for a member
355 * named `test`, and then use this traits class to check for the signature:
359 * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
361 * } // unnamed-namespace
364 * cout << "Does class Foo have a member int test() const? "
365 * << boolalpha << has_test_traits<Foo, int() const>::value;
368 * You can use the same traits class to test for a completely different
369 * signature, on a completely different class, as long as the member name
373 * cout << "Does class Foo have a member int test()? "
374 * << boolalpha << has_test_traits<Foo, int()>::value;
375 * cout << "Does class Foo have a member int test() const? "
376 * << boolalpha << has_test_traits<Foo, int() const>::value;
377 * cout << "Does class Bar have a member double test(const string&, long)? "
378 * << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
381 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
382 template <typename, typename> class classname; \
383 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
384 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
385 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, volatile); \
386 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, volatile const)
388 #endif //FOLLY_BASE_TRAITS_H_