blob: 1554f52ee176a2a9a7ffbb31d9e083c02bfec2b3 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
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#ifndef ANDROID_TYPE_HELPERS_H
18#define ANDROID_TYPE_HELPERS_H
19
20#include <new>
Colin Cross17b5b822016-09-15 18:15:37 -070021#include <type_traits>
22
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <stdint.h>
24#include <string.h>
25#include <sys/types.h>
26
27// ---------------------------------------------------------------------------
28
29namespace android {
30
31/*
32 * Types traits
33 */
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034
Mathias Agopiana33bd162009-06-22 01:17:46 -070035template <typename T> struct trait_trivial_ctor { enum { value = false }; };
36template <typename T> struct trait_trivial_dtor { enum { value = false }; };
37template <typename T> struct trait_trivial_copy { enum { value = false }; };
38template <typename T> struct trait_trivial_move { enum { value = false }; };
Vishwath Mohanfc3f57a2017-03-15 22:46:53 -070039template <typename T> struct trait_pointer { enum { value = false }; };
Mathias Agopiana33bd162009-06-22 01:17:46 -070040template <typename T> struct trait_pointer<T*> { enum { value = true }; };
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080041
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080042template <typename TYPE>
43struct traits {
44 enum {
Mathias Agopiana33bd162009-06-22 01:17:46 -070045 // whether this type is a pointer
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080046 is_pointer = trait_pointer<TYPE>::value,
Mathias Agopiana33bd162009-06-22 01:17:46 -070047 // whether this type's constructor is a no-op
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080048 has_trivial_ctor = is_pointer || trait_trivial_ctor<TYPE>::value,
Mathias Agopiana33bd162009-06-22 01:17:46 -070049 // whether this type's destructor is a no-op
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050 has_trivial_dtor = is_pointer || trait_trivial_dtor<TYPE>::value,
Mathias Agopiana33bd162009-06-22 01:17:46 -070051 // whether this type type can be copy-constructed with memcpy
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052 has_trivial_copy = is_pointer || trait_trivial_copy<TYPE>::value,
Mathias Agopiana33bd162009-06-22 01:17:46 -070053 // whether this type can be moved with memmove
54 has_trivial_move = is_pointer || trait_trivial_move<TYPE>::value
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055 };
56};
57
58template <typename T, typename U>
59struct aggregate_traits {
60 enum {
61 is_pointer = false,
Vishwath Mohanfc3f57a2017-03-15 22:46:53 -070062 has_trivial_ctor =
Mathias Agopiana33bd162009-06-22 01:17:46 -070063 traits<T>::has_trivial_ctor && traits<U>::has_trivial_ctor,
Vishwath Mohanfc3f57a2017-03-15 22:46:53 -070064 has_trivial_dtor =
Mathias Agopiana33bd162009-06-22 01:17:46 -070065 traits<T>::has_trivial_dtor && traits<U>::has_trivial_dtor,
Vishwath Mohanfc3f57a2017-03-15 22:46:53 -070066 has_trivial_copy =
Mathias Agopiana33bd162009-06-22 01:17:46 -070067 traits<T>::has_trivial_copy && traits<U>::has_trivial_copy,
Vishwath Mohanfc3f57a2017-03-15 22:46:53 -070068 has_trivial_move =
Mathias Agopiana33bd162009-06-22 01:17:46 -070069 traits<T>::has_trivial_move && traits<U>::has_trivial_move
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 };
71};
72
Jeff Brown9a0a76d2012-03-16 14:45:49 -070073#define ANDROID_TRIVIAL_CTOR_TRAIT( T ) \
74 template<> struct trait_trivial_ctor< T > { enum { value = true }; };
75
76#define ANDROID_TRIVIAL_DTOR_TRAIT( T ) \
77 template<> struct trait_trivial_dtor< T > { enum { value = true }; };
78
79#define ANDROID_TRIVIAL_COPY_TRAIT( T ) \
80 template<> struct trait_trivial_copy< T > { enum { value = true }; };
81
82#define ANDROID_TRIVIAL_MOVE_TRAIT( T ) \
Mathias Agopiana33bd162009-06-22 01:17:46 -070083 template<> struct trait_trivial_move< T > { enum { value = true }; };
84
Jeff Brown9a0a76d2012-03-16 14:45:49 -070085#define ANDROID_BASIC_TYPES_TRAITS( T ) \
86 ANDROID_TRIVIAL_CTOR_TRAIT( T ) \
87 ANDROID_TRIVIAL_DTOR_TRAIT( T ) \
88 ANDROID_TRIVIAL_COPY_TRAIT( T ) \
89 ANDROID_TRIVIAL_MOVE_TRAIT( T )
90
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080091// ---------------------------------------------------------------------------
92
93/*
94 * basic types traits
95 */
Mathias Agopiana33bd162009-06-22 01:17:46 -070096
97ANDROID_BASIC_TYPES_TRAITS( void )
98ANDROID_BASIC_TYPES_TRAITS( bool )
99ANDROID_BASIC_TYPES_TRAITS( char )
100ANDROID_BASIC_TYPES_TRAITS( unsigned char )
101ANDROID_BASIC_TYPES_TRAITS( short )
102ANDROID_BASIC_TYPES_TRAITS( unsigned short )
103ANDROID_BASIC_TYPES_TRAITS( int )
104ANDROID_BASIC_TYPES_TRAITS( unsigned int )
105ANDROID_BASIC_TYPES_TRAITS( long )
106ANDROID_BASIC_TYPES_TRAITS( unsigned long )
107ANDROID_BASIC_TYPES_TRAITS( long long )
108ANDROID_BASIC_TYPES_TRAITS( unsigned long long )
109ANDROID_BASIC_TYPES_TRAITS( float )
110ANDROID_BASIC_TYPES_TRAITS( double )
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800111
112// ---------------------------------------------------------------------------
113
Mathias Agopiana33bd162009-06-22 01:17:46 -0700114
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800115/*
116 * compare and order types
117 */
118
119template<typename TYPE> inline
120int strictly_order_type(const TYPE& lhs, const TYPE& rhs) {
121 return (lhs < rhs) ? 1 : 0;
122}
123
124template<typename TYPE> inline
125int compare_type(const TYPE& lhs, const TYPE& rhs) {
126 return strictly_order_type(rhs, lhs) - strictly_order_type(lhs, rhs);
127}
128
129/*
Mathias Agopiana33bd162009-06-22 01:17:46 -0700130 * create, destroy, copy and move types...
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 */
Mathias Agopiana33bd162009-06-22 01:17:46 -0700132
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800133template<typename TYPE> inline
134void construct_type(TYPE* p, size_t n) {
135 if (!traits<TYPE>::has_trivial_ctor) {
Nick Kralevich58bf5722015-06-13 15:28:53 -0700136 while (n > 0) {
137 n--;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 new(p++) TYPE;
139 }
140 }
141}
142
143template<typename TYPE> inline
144void destroy_type(TYPE* p, size_t n) {
145 if (!traits<TYPE>::has_trivial_dtor) {
Nick Kralevich58bf5722015-06-13 15:28:53 -0700146 while (n > 0) {
147 n--;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800148 p->~TYPE();
149 p++;
150 }
151 }
152}
153
Colin Cross1811d152016-09-26 14:24:48 -0700154template<typename TYPE>
155typename std::enable_if<traits<TYPE>::has_trivial_copy>::type
156inline
157copy_type(TYPE* d, const TYPE* s, size_t n) {
158 memcpy(d,s,n*sizeof(TYPE));
159}
160
161template<typename TYPE>
162typename std::enable_if<!traits<TYPE>::has_trivial_copy>::type
163inline
164copy_type(TYPE* d, const TYPE* s, size_t n) {
165 while (n > 0) {
166 n--;
167 new(d) TYPE(*s);
168 d++, s++;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800169 }
170}
171
172template<typename TYPE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800173void splat_type(TYPE* where, const TYPE* what, size_t n) {
174 if (!traits<TYPE>::has_trivial_copy) {
Nick Kralevich58bf5722015-06-13 15:28:53 -0700175 while (n > 0) {
176 n--;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800177 new(where) TYPE(*what);
178 where++;
179 }
180 } else {
Nick Kralevich58bf5722015-06-13 15:28:53 -0700181 while (n > 0) {
182 n--;
Mathias Agopiana33bd162009-06-22 01:17:46 -0700183 *where++ = *what;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800184 }
185 }
186}
187
Colin Cross17b5b822016-09-15 18:15:37 -0700188template<typename TYPE>
189struct use_trivial_move : public std::integral_constant<bool,
190 (traits<TYPE>::has_trivial_dtor && traits<TYPE>::has_trivial_copy)
191 || traits<TYPE>::has_trivial_move
192> {};
193
194template<typename TYPE>
195typename std::enable_if<use_trivial_move<TYPE>::value>::type
196inline
197move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
198 memmove(d, s, n*sizeof(TYPE));
199}
200
201template<typename TYPE>
202typename std::enable_if<!use_trivial_move<TYPE>::value>::type
203inline
204move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
205 d += n;
206 s += n;
207 while (n > 0) {
208 n--;
209 --d, --s;
210 if (!traits<TYPE>::has_trivial_copy) {
211 new(d) TYPE(*s);
212 } else {
213 *d = *s;
214 }
215 if (!traits<TYPE>::has_trivial_dtor) {
216 s->~TYPE();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800217 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800218 }
219}
220
Colin Cross17b5b822016-09-15 18:15:37 -0700221template<typename TYPE>
222typename std::enable_if<use_trivial_move<TYPE>::value>::type
223inline
224move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
225 memmove(d, s, n*sizeof(TYPE));
226}
227
228template<typename TYPE>
229typename std::enable_if<!use_trivial_move<TYPE>::value>::type
230inline
231move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
232 while (n > 0) {
233 n--;
234 if (!traits<TYPE>::has_trivial_copy) {
235 new(d) TYPE(*s);
236 } else {
237 *d = *s;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800238 }
Colin Cross17b5b822016-09-15 18:15:37 -0700239 if (!traits<TYPE>::has_trivial_dtor) {
240 s->~TYPE();
241 }
242 d++, s++;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800243 }
244}
Mathias Agopiana33bd162009-06-22 01:17:46 -0700245
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800246// ---------------------------------------------------------------------------
247
248/*
249 * a key/value pair
250 */
251
252template <typename KEY, typename VALUE>
253struct key_value_pair_t {
Jeff Browne735f232011-11-14 18:29:15 -0800254 typedef KEY key_t;
255 typedef VALUE value_t;
256
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800257 KEY key;
258 VALUE value;
259 key_value_pair_t() { }
260 key_value_pair_t(const key_value_pair_t& o) : key(o.key), value(o.value) { }
Colin Cross17b5b822016-09-15 18:15:37 -0700261 key_value_pair_t& operator=(const key_value_pair_t& o) {
262 key = o.key;
263 value = o.value;
264 return *this;
265 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800266 key_value_pair_t(const KEY& k, const VALUE& v) : key(k), value(v) { }
Chih-Hung Hsieh2a929962016-08-02 12:14:47 -0700267 explicit key_value_pair_t(const KEY& k) : key(k) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800268 inline bool operator < (const key_value_pair_t& o) const {
269 return strictly_order_type(key, o.key);
270 }
Jeff Browne735f232011-11-14 18:29:15 -0800271 inline const KEY& getKey() const {
272 return key;
273 }
274 inline const VALUE& getValue() const {
275 return value;
276 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800277};
278
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800279template <typename K, typename V>
280struct trait_trivial_ctor< key_value_pair_t<K, V> >
281{ enum { value = aggregate_traits<K,V>::has_trivial_ctor }; };
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800282template <typename K, typename V>
283struct trait_trivial_dtor< key_value_pair_t<K, V> >
284{ enum { value = aggregate_traits<K,V>::has_trivial_dtor }; };
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800285template <typename K, typename V>
286struct trait_trivial_copy< key_value_pair_t<K, V> >
287{ enum { value = aggregate_traits<K,V>::has_trivial_copy }; };
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800288template <typename K, typename V>
Mathias Agopiana33bd162009-06-22 01:17:46 -0700289struct trait_trivial_move< key_value_pair_t<K, V> >
290{ enum { value = aggregate_traits<K,V>::has_trivial_move }; };
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800291
292// ---------------------------------------------------------------------------
293
Jeff Browne735f232011-11-14 18:29:15 -0800294/*
295 * Hash codes.
296 */
297typedef uint32_t hash_t;
298
299template <typename TKey>
300hash_t hash_type(const TKey& key);
301
Colin Cross17b5b822016-09-15 18:15:37 -0700302/* Built-in hash code specializations */
Jeff Browne735f232011-11-14 18:29:15 -0800303#define ANDROID_INT32_HASH(T) \
304 template <> inline hash_t hash_type(const T& value) { return hash_t(value); }
305#define ANDROID_INT64_HASH(T) \
306 template <> inline hash_t hash_type(const T& value) { \
307 return hash_t((value >> 32) ^ value); }
308#define ANDROID_REINTERPRET_HASH(T, R) \
309 template <> inline hash_t hash_type(const T& value) { \
Colin Cross17b5b822016-09-15 18:15:37 -0700310 R newValue; \
311 static_assert(sizeof(newValue) == sizeof(value), "size mismatch"); \
312 memcpy(&newValue, &value, sizeof(newValue)); \
313 return hash_type(newValue); \
314 }
Jeff Browne735f232011-11-14 18:29:15 -0800315
316ANDROID_INT32_HASH(bool)
Jeff Brown142dbcd2011-11-24 11:54:21 -0800317ANDROID_INT32_HASH(int8_t)
318ANDROID_INT32_HASH(uint8_t)
319ANDROID_INT32_HASH(int16_t)
320ANDROID_INT32_HASH(uint16_t)
321ANDROID_INT32_HASH(int32_t)
322ANDROID_INT32_HASH(uint32_t)
323ANDROID_INT64_HASH(int64_t)
324ANDROID_INT64_HASH(uint64_t)
Jeff Browne735f232011-11-14 18:29:15 -0800325ANDROID_REINTERPRET_HASH(float, uint32_t)
326ANDROID_REINTERPRET_HASH(double, uint64_t)
327
Raph Levienb6ea1752012-10-25 23:11:13 -0700328template <typename T> inline hash_t hash_type(T* const & value) {
Jeff Browne735f232011-11-14 18:29:15 -0800329 return hash_type(uintptr_t(value));
330}
331
Pirama Arumuga Nainareab48ce2018-04-10 14:31:29 -0700332} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800333
334// ---------------------------------------------------------------------------
335
336#endif // ANDROID_TYPE_HELPERS_H