4 Implements traits complementary to those provided in <boost/type_traits.h>
6 * Implements `IsRelocatable` trait.
7 * Implements `IsOneOf` trait
8 * Macros to state the assumptions easily
13 `<boost/type_traits.hpp>` is the Boost type-traits library defining a
14 variety of traits such as `is_integral` or `is_floating_point`. This helps
15 to gain more information about a given type.
16 Many traits introduced by Boost have been standardized in C++11.
18 `folly/Traits.h` implements traits complementing those present in boost.
24 In C++, the default way to move an object is by
25 calling the copy constructor and destroying the old copy
26 instead of directly copying the memory contents by using memcpy().
27 The conservative approach of moving an object assumes that the copied
28 object is not relocatable.
29 The two following code sequences should be semantically equivalent for a
34 void conservativeMove(T * from, T * to) {
41 void optimizedMove(T * from, T * to) {
42 memcpy(to, from, sizeof(T));
47 Very few C++ types are non-relocatable.
48 The type defined below maintains a pointer inside an embedded buffer and
49 hence would be non-relocatable. Moving the object by simply copying its
50 memory contents would leave the internal pointer pointing to the old buffer.
53 class NonRelocatableType {
56 char * pointerToBuffer;
59 NonRelocatableType() : pointerToBuffer(buffer) {}
64 We can optimize the task of moving a relocatable type T using memcpy.
65 IsRelocatable<T>::value describes the ability of moving around memory
66 a value of type T by using memcpy.
74 template <class T1, class T2>
75 class MyParameterizedType;
80 * Declaring a type as relocatable
82 Appending the lines below after definition of My*Type
83 (`MyParameterizedType` or `MySimpleType`) will declare it as relocatable
86 /* Definition of My*Type goes here */
87 // global namespace (not inside any namespace)
89 // defining specialization of IsRelocatable for MySimpleType
91 struct IsRelocatable<MySimpleType> : boost::true_type {};
92 // defining specialization of IsRelocatable for MyParameterizedType
93 template <class T1, class T2>
94 struct IsRelocatable<MyParameterizedType<T1, T2>>
95 : ::boost::true_type {};
99 * To make it easy to state assumptions for a regular type or a family of
100 parameterized type, various macros can be used as shown below.
102 * Stating that a type is Relocatable using a macro
107 // For a Regular Type
108 FOLLY_ASSUME_RELOCATABLE(MySimpleType);
109 // For a Parameterized Type
110 FOLLY_ASSUME_RELOCATABLE(MyParameterizedType<T1, T2>);
114 * Stating that a type has no throw constructor using a macro
118 // For a Regular Type
119 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MySimpleType);
120 // For a Parameterized Type
121 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyParameterizedType<T1, T2>);
125 `fbvector` only works with relocatable objects. If assumptions are not stated
126 explicitly, `fbvector<MySimpleType>` or `fbvector<MyParameterizedType>`
127 will fail to compile due to assertion below:
130 static_assert(IsRelocatable<My*Type>::value, "");
133 FOLLY_ASSUME_FBVECTOR_COMPATIBLE*(type) macros can be used to state that type
134 is relocatable and has nothrow constructor.
136 * Stating that a type is `fbvector-compatible` using macros
137 i.e. relocatable and has nothrow default constructor
140 // at global level, i.e no namespace
141 // macro for regular type
142 FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MySimpleType);
143 // macro for types having 2 template parameters (MyParameterizedType)
144 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyParameterizedType);
149 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(MyTypeHavingOneParameter) macro is
150 for family of parameterized types having 1 parameter
152 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(MyTypeHavingThreeParameters) macro is
153 for family of parameterized types having 3 parameters
155 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(MyTypeHavingFourParameters) macro is
156 for family of parameterized types having 4 parameters
158 Few common types, namely `std::basic_string`, `std::vector`, `std::list`,
159 `std::map`, `std::deque`, `std::set`, `std::unique_ptr`, `std::shared_ptr`,
160 `std::function`, `boost::shared_ptr` which are compatible with `fbvector` are
161 already instantiated and declared compatible with `fbvector`. `fbvector` can be
162 directly used with any of these C++ types.
164 `std::pair` can be safely assumed to be compatible with `fbvector` if both of
170 `boost::is_same<T1, T2>::value` can be used to test if types of T1 and T2 are
171 same. `folly::IsOneOf<T, T1, Ts...>::value` can be used to test if type of T1
172 matches the type of one of the other template parameter, T1, T2, ...Tn.
173 Recursion is used to implement this type trait.