2 * Copyright 2016 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
23 #include <type_traits>
26 #include <folly/Portability.h>
28 // libc++ doesn't provide this header, nor does msvc
29 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
30 // This file appears in two locations: inside fbcode and in the
31 // libstdc++ source code (when embedding fbstring as std::string).
32 // To aid in this schizophrenic use, two macros are defined in
34 // _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to
35 // gate use inside fbcode v. libstdc++
36 #include <bits/c++config.h>
39 #include <boost/type_traits.hpp>
40 #include <boost/mpl/and.hpp>
41 #include <boost/mpl/has_xxx.hpp>
42 #include <boost/mpl/not.hpp>
47 * IsRelocatable<T>::value describes the ability of moving around
48 * memory a value of type T by using memcpy (as opposed to the
49 * conservative approach of calling the copy constructor and then
50 * destroying the old temporary. Essentially for a relocatable type,
51 * the following two sequences of code should be semantically
54 * void move1(T * from, T * to) {
59 * void move2(T * from, T * to) {
60 * memcpy(to, from, sizeof(T));
63 * Most C++ types are relocatable; the ones that aren't would include
64 * internal pointers or (very rarely) would need to update remote
65 * pointers to pointers tracking them. All C++ primitive types and
66 * type constructors are relocatable.
68 * This property can be used in a variety of optimizations. Currently
69 * fbvector uses this property intensively.
71 * The default conservatively assumes the type is not
72 * relocatable. Several specializations are defined for known
73 * types. You may want to add your own specializations. Do so in
74 * namespace folly and make sure you keep the specialization of
75 * IsRelocatable<SomeStruct> in the same header as SomeStruct.
77 * You may also declare a type to be relocatable by including
78 * `typedef std::true_type IsRelocatable;`
79 * in the class header.
81 * It may be unset in a base class by overriding the typedef to false_type.
84 * IsTriviallyCopyable describes the value semantics property. C++11 contains
85 * the type trait is_trivially_copyable; however, it is not yet implemented
86 * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
89 * IsZeroInitializable describes the property that default construction is the
90 * same as memset(dst, 0, sizeof(T)).
93 namespace traits_detail {
95 #define FOLLY_HAS_TRUE_XXX(name) \
96 BOOST_MPL_HAS_XXX_TRAIT_DEF(name); \
97 template <class T> struct name ## _is_true \
98 : std::is_same<typename T::name, std::true_type> {}; \
99 template <class T> struct has_true_ ## name \
100 : std::conditional< \
101 has_ ## name <T>::value, \
102 name ## _is_true<T>, \
106 FOLLY_HAS_TRUE_XXX(IsRelocatable)
107 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
108 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
110 #undef FOLLY_HAS_TRUE_XXX
113 template <class T> struct IsTriviallyCopyable
114 : std::integral_constant<bool,
115 !std::is_class<T>::value ||
116 // TODO: add alternate clause is_trivially_copyable, when available
117 traits_detail::has_true_IsTriviallyCopyable<T>::value
120 template <class T> struct IsRelocatable
121 : std::integral_constant<bool,
122 !std::is_class<T>::value ||
123 // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
124 //std::is_trivially_move_constructible<T>::value ||
125 IsTriviallyCopyable<T>::value ||
126 traits_detail::has_true_IsRelocatable<T>::value
129 template <class T> struct IsZeroInitializable
130 : std::integral_constant<bool,
131 !std::is_class<T>::value ||
132 traits_detail::has_true_IsZeroInitializable<T>::value
138 * Use this macro ONLY inside namespace folly. When using it with a
139 * regular type, use it like this:
141 * // Make sure you're at namespace ::folly scope
142 * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
144 * When using it with a template type, use it like this:
146 * // Make sure you're at namespace ::folly scope
147 * template<class T1, class T2>
148 * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
150 #define FOLLY_ASSUME_RELOCATABLE(...) \
151 struct IsRelocatable< __VA_ARGS__ > : std::true_type {};
154 * Use this macro ONLY inside namespace boost. When using it with a
155 * regular type, use it like this:
157 * // Make sure you're at namespace ::boost scope
158 * template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType)
160 * When using it with a template type, use it like this:
162 * // Make sure you're at namespace ::boost scope
163 * template<class T1, class T2>
164 * FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType<T1, T2>)
166 #define FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(...) \
167 struct has_nothrow_constructor< __VA_ARGS__ > : ::boost::true_type {};
170 * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode two
171 * assumptions: first, that the type is relocatable per IsRelocatable
172 * above, and that it has a nothrow constructor. Most types can be
173 * assumed to satisfy both conditions, but it is the responsibility of
174 * the user to state that assumption. User-defined classes will not
175 * work with fbvector (see FBVector.h) unless they state this
176 * combination of properties.
178 * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
180 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
182 * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
183 * allow using the macro for describing templatized classes with 1, 2,
184 * 3, and 4 template parameters respectively. For template classes
185 * just use the macro with the appropriate number and pass the name of
186 * the template to it. Example:
188 * template <class T1, class T2> class MyType { ... };
190 * // Make sure you're at global scope
191 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
194 // Use this macro ONLY at global level (no namespace)
195 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
196 namespace folly { template<> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) } \
198 template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__) }
199 // Use this macro ONLY at global level (no namespace)
200 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
202 template <class T1> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) } \
204 template <class T1> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1>) }
205 // Use this macro ONLY at global level (no namespace)
206 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
208 template <class T1, class T2> \
209 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) } \
211 template <class T1, class T2> \
212 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2>) }
213 // Use this macro ONLY at global level (no namespace)
214 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \
216 template <class T1, class T2, class T3> \
217 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) } \
219 template <class T1, class T2, class T3> \
220 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3>) }
221 // Use this macro ONLY at global level (no namespace)
222 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \
224 template <class T1, class T2, class T3, class T4> \
225 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) } \
227 template <class T1, class T2, class T3, class T4> \
228 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3, T4>) }
231 * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
232 * safe to assume that pair is compatible if both of its components
233 * are. Furthermore, all STL containers can be assumed to comply,
234 * although that is not guaranteed by the standard.
237 FOLLY_NAMESPACE_STD_BEGIN
239 template <class T, class U>
241 #ifndef _GLIBCXX_USE_FB
242 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
243 template <class T, class R, class A>
245 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
247 template <class T, class R, class A, class S>
250 template <class T, class A>
252 template <class T, class A>
254 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
255 template <class T, class A>
257 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
258 template <class T, class C, class A>
260 template <class K, class V, class C, class A>
265 FOLLY_NAMESPACE_STD_END
269 template <class T> class shared_ptr;
271 template <class T, class U>
272 struct has_nothrow_constructor< std::pair<T, U> >
273 : ::boost::mpl::and_< has_nothrow_constructor<T>,
274 has_nothrow_constructor<U> > {};
280 // STL commonly-used types
281 template <class T, class U>
282 struct IsRelocatable< std::pair<T, U> >
283 : ::boost::mpl::and_< IsRelocatable<T>, IsRelocatable<U> > {};
285 // Is T one of T1, T2, ..., Tn?
286 template <class T, class... Ts>
288 enum { value = false };
291 template <class T, class T1, class... Ts>
292 struct IsOneOf<T, T1, Ts...> {
293 enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
297 * Complementary type traits for integral comparisons.
299 * For instance, `if(x < 0)` yields an error in clang for unsigned types
300 * when -Werror is used due to -Wtautological-compare
303 * @author: Marcelo Juchem <marcelo@fb.com>
308 template <typename T, bool>
309 struct is_negative_impl {
310 constexpr static bool check(T x) { return x < 0; }
313 template <typename T>
314 struct is_negative_impl<T, false> {
315 constexpr static bool check(T) { return false; }
318 // folly::to integral specializations can end up generating code
319 // inside what are really static ifs (not executed because of the templated
320 // types) that violate -Wsign-compare so suppress them in order to not prevent
321 // all calling code from using it.
322 #pragma GCC diagnostic push
323 #pragma GCC diagnostic ignored "-Wsign-compare"
325 template <typename RHS, RHS rhs, typename LHS>
326 bool less_than_impl(LHS const lhs) {
328 rhs > std::numeric_limits<LHS>::max() ? true :
329 rhs <= std::numeric_limits<LHS>::min() ? false :
333 #pragma GCC diagnostic pop
335 template <typename RHS, RHS rhs, typename LHS>
336 bool greater_than_impl(LHS const lhs) {
338 rhs > std::numeric_limits<LHS>::max() ? false :
339 rhs < std::numeric_limits<LHS>::min() ? true :
343 } // namespace detail {
346 template <typename T>
347 constexpr bool is_negative(T x) {
348 return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
352 template <typename T>
353 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
356 template <typename T>
357 constexpr bool is_positive(T x) { return !is_non_positive(x); }
360 template <typename T>
361 constexpr bool is_non_negative(T x) {
362 return !x || is_positive(x);
365 template <typename RHS, RHS rhs, typename LHS>
366 bool less_than(LHS const lhs) {
367 return detail::less_than_impl<
368 RHS, rhs, typename std::remove_reference<LHS>::type
372 template <typename RHS, RHS rhs, typename LHS>
373 bool greater_than(LHS const lhs) {
374 return detail::greater_than_impl<
375 RHS, rhs, typename std::remove_reference<LHS>::type
380 * Like std::piecewise_construct, a tag type & instance used for in-place
381 * construction of non-movable contained types, e.g. by Synchronized.
383 struct construct_in_place_t {};
384 constexpr construct_in_place_t construct_in_place{};
388 // gcc-5.0 changed string's implementation in libgcc to be non-relocatable
390 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string);
392 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector);
393 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list);
394 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque);
395 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr);
396 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr);
397 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function);
400 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr);
402 #define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \
403 template <typename T> \
405 template <typename C> \
406 constexpr static bool test(typename C::type_name*) { return true; } \
407 template <typename> \
408 constexpr static bool test(...) { return false; } \
409 constexpr static bool value = test<T>(nullptr); \
412 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
413 template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
414 class classname<TTheClass_, RTheReturn_(TTheArgs_...) cv_qual> { \
416 typename UTheClass_, RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual \
417 > struct sfinae {}; \
418 template <typename UTheClass_> \
419 constexpr static bool test(sfinae<UTheClass_, &UTheClass_::func_name>*) \
421 template <typename> \
422 constexpr static bool test(...) { return false; } \
424 constexpr static bool value = test<TTheClass_>(nullptr); \
428 * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
429 * classes that check for the existence of a member function with
430 * a given name and signature. It currently does not support
431 * checking for inherited members.
433 * Such classes receive two template parameters: the class to be checked
434 * and the signature of the member function. A static boolean field
435 * named `value` (which is also constexpr) tells whether such member
438 * Each traits class created is bound only to the member name, not to
439 * its signature nor to the type of the class containing it.
441 * Say you need to know if a given class has a member function named
442 * `test` with the following signature:
446 * You'd need this macro to create a traits class to check for a member
447 * named `test`, and then use this traits class to check for the signature:
451 * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
453 * } // unnamed-namespace
456 * cout << "Does class Foo have a member int test() const? "
457 * << boolalpha << has_test_traits<Foo, int() const>::value;
460 * You can use the same traits class to test for a completely different
461 * signature, on a completely different class, as long as the member name
465 * cout << "Does class Foo have a member int test()? "
466 * << boolalpha << has_test_traits<Foo, int()>::value;
467 * cout << "Does class Foo have a member int test() const? "
468 * << boolalpha << has_test_traits<Foo, int() const>::value;
469 * cout << "Does class Bar have a member double test(const string&, long)? "
470 * << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
473 * @author: Marcelo Juchem <marcelo@fb.com>
475 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
476 template <typename, typename> class classname; \
477 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
478 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
479 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
480 classname, func_name, /* nolint */ volatile); \
481 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
482 classname, func_name, /* nolint */ volatile const)