A few fixes for clang support
[folly.git] / folly / Traits.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author: Andrei Alexandrescu
18
19 #ifndef FOLLY_BASE_TRAITS_H_
20 #define FOLLY_BASE_TRAITS_H_
21
22 #include <memory>
23 #include <type_traits>
24
25 #include <bits/c++config.h>
26
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>
31
32 namespace folly {
33
34 /**
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
40  * equivalent:
41  *
42  * void move1(T * from, T * to) {
43  *   new(to) T(from);
44  *   (*from).~T();
45  * }
46  *
47  * void move2(T * from, T * to) {
48  *   memcpy(to, from, sizeof(T));
49  * }
50  *
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.
55  *
56  * This property can be used in a variety of optimizations. Currently
57  * fbvector uses this property intensively.
58  *
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.
64  *
65  * You may also declare a type to be relocatable by including
66  *    `typedef std::true_type IsRelocatable;`
67  * in the class header.
68  *
69  * It may be unset in a base class by overriding the typedef to false_type.
70  */
71 /*
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.
75  */
76 /*
77  * IsZeroInitializable describes the property that default construction is the
78  * same as memset(dst, 0, sizeof(T)).
79  */
80
81 namespace traits_detail {
82
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             \
88     : std::conditional<                                   \
89         has_ ## name <T>::value,                          \
90         name ## _is_true<T>,                              \
91         std::false_type                                   \
92       >:: type {};
93
94 FOLLY_HAS_TRUE_XXX(IsRelocatable)
95 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
96 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
97
98 #undef FOLLY_HAS_TRUE_XXX
99 }
100
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
106     > {};
107
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
115     > {};
116
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
121     > {};
122
123 } // namespace folly
124
125 /**
126  * Use this macro ONLY inside namespace folly. When using it with a
127  * regular type, use it like this:
128  *
129  * // Make sure you're at namespace ::folly scope
130  * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
131  *
132  * When using it with a template type, use it like this:
133  *
134  * // Make sure you're at namespace ::folly scope
135  * template<class T1, class T2>
136  * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
137  */
138 #define FOLLY_ASSUME_RELOCATABLE(...) \
139   struct IsRelocatable<  __VA_ARGS__ > : std::true_type {};
140
141 /**
142  * Use this macro ONLY inside namespace boost. When using it with a
143  * regular type, use it like this:
144  *
145  * // Make sure you're at namespace ::boost scope
146  * template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType)
147  *
148  * When using it with a template type, use it like this:
149  *
150  * // Make sure you're at namespace ::boost scope
151  * template<class T1, class T2>
152  * FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType<T1, T2>)
153  */
154 #define FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(...) \
155   struct has_nothrow_constructor<  __VA_ARGS__ > : ::boost::true_type {};
156
157 /**
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.
165  *
166  * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
167  *
168  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
169  *
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:
175  *
176  * template <class T1, class T2> class MyType { ... };
177  * ...
178  * // Make sure you're at global scope
179  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
180  */
181
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__) }   \
185   namespace boost { \
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(...)                         \
189   namespace folly {                                                     \
190   template <class T1> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) }       \
191     namespace boost {                                                   \
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(...)                 \
195   namespace folly {                                             \
196   template <class T1, class T2>                                 \
197   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) }               \
198     namespace boost {                                           \
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(...)                         \
203   namespace folly {                                                     \
204   template <class T1, class T2, class T3>                               \
205   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) }                   \
206     namespace boost {                                                   \
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(...)                         \
211   namespace folly {                                                     \
212   template <class T1, class T2, class T3, class T4>                     \
213   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) }               \
214     namespace boost {                                                   \
215     template <class T1, class T2, class T3, class T4>                   \
216     FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3, T4>) }
217
218 /**
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.
223  */
224
225 namespace std {
226
227 template <class T, class U>
228   struct pair;
229 #ifndef _GLIBCXX_USE_FB
230 template <class T, class R, class A>
231   class basic_string;
232 #else
233 template <class T, class R, class A, class S>
234   class basic_string;
235 #endif
236 template <class T, class A>
237   class vector;
238 template <class T, class A>
239   class deque;
240 template <class T, class A>
241   class list;
242 template <class T, class C, class A>
243   class set;
244 template <class K, class V, class C, class A>
245   class map;
246 template <class T>
247   class shared_ptr;
248
249 }
250
251 namespace boost {
252
253 template <class T> class shared_ptr;
254
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> > {};
259
260 } // namespace boost
261
262 namespace folly {
263
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> > {};
268
269 // Is T one of T1, T2, ..., Tn?
270 template <class T, class... Ts>
271 struct IsOneOf {
272   enum { value = false };
273 };
274
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 };
278 };
279
280 /*
281  * Complementary type traits to check for a negative value.
282  *
283  * if(x < 0) yields an error in clang for unsigned types when -Werror is used
284  */
285
286 namespace detail {
287
288 template <typename T, bool>
289 struct is_negative_impl {
290   constexpr static bool check(T x) { return x < 0; }
291 };
292
293 template <typename T>
294 struct is_negative_impl<T, false> {
295   constexpr static bool check(T x) { return false; }
296 };
297
298 } // namespace detail {
299
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);
303 }
304
305 } // namespace folly
306
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);
316
317 // Boost
318 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr);
319
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> { \
323     template < \
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>*) \
328     { return true; } \
329     template <typename> \
330     constexpr static bool test(...) { return false; } \
331   public: \
332     constexpr static bool value = test<TTheClass_>(nullptr); \
333   }
334
335 /*
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.
340  *
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
344  * function exists.
345  *
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.
348  *
349  * Say you need to know if a given class has a member function named
350  * `test` with the following signature:
351  *
352  *    int test() const;
353  *
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:
356  *
357  * namespace {
358  *
359  * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
360  *
361  * } // unnamed-namespace
362  *
363  * void some_func() {
364  *   cout << "Does class Foo have a member int test() const? "
365  *     << boolalpha << has_test_traits<Foo, int() const>::value;
366  * }
367  *
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
370  * is the same:
371  *
372  * void some_func() {
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;
379  * }
380  */
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)
387
388 #endif //FOLLY_BASE_TRAITS_H_