The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 21 | #include <type_traits> |
| 22 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | // --------------------------------------------------------------------------- |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | /* |
| 32 | * Types traits |
| 33 | */ |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 35 | template <typename T> struct trait_trivial_ctor { enum { value = false }; }; |
| 36 | template <typename T> struct trait_trivial_dtor { enum { value = false }; }; |
| 37 | template <typename T> struct trait_trivial_copy { enum { value = false }; }; |
| 38 | template <typename T> struct trait_trivial_move { enum { value = false }; }; |
Vishwath Mohan | fc3f57a | 2017-03-15 22:46:53 -0700 | [diff] [blame] | 39 | template <typename T> struct trait_pointer { enum { value = false }; }; |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 40 | template <typename T> struct trait_pointer<T*> { enum { value = true }; }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | template <typename TYPE> |
| 43 | struct traits { |
| 44 | enum { |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 45 | // whether this type is a pointer |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | is_pointer = trait_pointer<TYPE>::value, |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 47 | // whether this type's constructor is a no-op |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | has_trivial_ctor = is_pointer || trait_trivial_ctor<TYPE>::value, |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 49 | // whether this type's destructor is a no-op |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | has_trivial_dtor = is_pointer || trait_trivial_dtor<TYPE>::value, |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 51 | // whether this type type can be copy-constructed with memcpy |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | has_trivial_copy = is_pointer || trait_trivial_copy<TYPE>::value, |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 53 | // whether this type can be moved with memmove |
| 54 | has_trivial_move = is_pointer || trait_trivial_move<TYPE>::value |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | }; |
| 56 | }; |
| 57 | |
| 58 | template <typename T, typename U> |
| 59 | struct aggregate_traits { |
| 60 | enum { |
| 61 | is_pointer = false, |
Vishwath Mohan | fc3f57a | 2017-03-15 22:46:53 -0700 | [diff] [blame] | 62 | has_trivial_ctor = |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 63 | traits<T>::has_trivial_ctor && traits<U>::has_trivial_ctor, |
Vishwath Mohan | fc3f57a | 2017-03-15 22:46:53 -0700 | [diff] [blame] | 64 | has_trivial_dtor = |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 65 | traits<T>::has_trivial_dtor && traits<U>::has_trivial_dtor, |
Vishwath Mohan | fc3f57a | 2017-03-15 22:46:53 -0700 | [diff] [blame] | 66 | has_trivial_copy = |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 67 | traits<T>::has_trivial_copy && traits<U>::has_trivial_copy, |
Vishwath Mohan | fc3f57a | 2017-03-15 22:46:53 -0700 | [diff] [blame] | 68 | has_trivial_move = |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 69 | traits<T>::has_trivial_move && traits<U>::has_trivial_move |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | }; |
| 71 | }; |
| 72 | |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 73 | #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 Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 83 | template<> struct trait_trivial_move< T > { enum { value = true }; }; |
| 84 | |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 85 | #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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | // --------------------------------------------------------------------------- |
| 92 | |
| 93 | /* |
| 94 | * basic types traits |
| 95 | */ |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 96 | |
| 97 | ANDROID_BASIC_TYPES_TRAITS( void ) |
| 98 | ANDROID_BASIC_TYPES_TRAITS( bool ) |
| 99 | ANDROID_BASIC_TYPES_TRAITS( char ) |
| 100 | ANDROID_BASIC_TYPES_TRAITS( unsigned char ) |
| 101 | ANDROID_BASIC_TYPES_TRAITS( short ) |
| 102 | ANDROID_BASIC_TYPES_TRAITS( unsigned short ) |
| 103 | ANDROID_BASIC_TYPES_TRAITS( int ) |
| 104 | ANDROID_BASIC_TYPES_TRAITS( unsigned int ) |
| 105 | ANDROID_BASIC_TYPES_TRAITS( long ) |
| 106 | ANDROID_BASIC_TYPES_TRAITS( unsigned long ) |
| 107 | ANDROID_BASIC_TYPES_TRAITS( long long ) |
| 108 | ANDROID_BASIC_TYPES_TRAITS( unsigned long long ) |
| 109 | ANDROID_BASIC_TYPES_TRAITS( float ) |
| 110 | ANDROID_BASIC_TYPES_TRAITS( double ) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
| 112 | // --------------------------------------------------------------------------- |
| 113 | |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | /* |
| 116 | * compare and order types |
| 117 | */ |
| 118 | |
| 119 | template<typename TYPE> inline |
| 120 | int strictly_order_type(const TYPE& lhs, const TYPE& rhs) { |
| 121 | return (lhs < rhs) ? 1 : 0; |
| 122 | } |
| 123 | |
| 124 | template<typename TYPE> inline |
| 125 | int compare_type(const TYPE& lhs, const TYPE& rhs) { |
| 126 | return strictly_order_type(rhs, lhs) - strictly_order_type(lhs, rhs); |
| 127 | } |
| 128 | |
| 129 | /* |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 130 | * create, destroy, copy and move types... |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | */ |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 132 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | template<typename TYPE> inline |
| 134 | void construct_type(TYPE* p, size_t n) { |
| 135 | if (!traits<TYPE>::has_trivial_ctor) { |
Nick Kralevich | 58bf572 | 2015-06-13 15:28:53 -0700 | [diff] [blame] | 136 | while (n > 0) { |
| 137 | n--; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | new(p++) TYPE; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | template<typename TYPE> inline |
| 144 | void destroy_type(TYPE* p, size_t n) { |
| 145 | if (!traits<TYPE>::has_trivial_dtor) { |
Nick Kralevich | 58bf572 | 2015-06-13 15:28:53 -0700 | [diff] [blame] | 146 | while (n > 0) { |
| 147 | n--; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | p->~TYPE(); |
| 149 | p++; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 154 | template<typename TYPE> |
| 155 | typename std::enable_if<traits<TYPE>::has_trivial_copy>::type |
| 156 | inline |
| 157 | copy_type(TYPE* d, const TYPE* s, size_t n) { |
| 158 | memcpy(d,s,n*sizeof(TYPE)); |
| 159 | } |
| 160 | |
| 161 | template<typename TYPE> |
| 162 | typename std::enable_if<!traits<TYPE>::has_trivial_copy>::type |
| 163 | inline |
| 164 | copy_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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | template<typename TYPE> inline |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | void splat_type(TYPE* where, const TYPE* what, size_t n) { |
| 174 | if (!traits<TYPE>::has_trivial_copy) { |
Nick Kralevich | 58bf572 | 2015-06-13 15:28:53 -0700 | [diff] [blame] | 175 | while (n > 0) { |
| 176 | n--; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | new(where) TYPE(*what); |
| 178 | where++; |
| 179 | } |
| 180 | } else { |
Nick Kralevich | 58bf572 | 2015-06-13 15:28:53 -0700 | [diff] [blame] | 181 | while (n > 0) { |
| 182 | n--; |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 183 | *where++ = *what; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 188 | template<typename TYPE> |
| 189 | struct 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 | |
| 194 | template<typename TYPE> |
| 195 | typename std::enable_if<use_trivial_move<TYPE>::value>::type |
| 196 | inline |
| 197 | move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) { |
| 198 | memmove(d, s, n*sizeof(TYPE)); |
| 199 | } |
| 200 | |
| 201 | template<typename TYPE> |
| 202 | typename std::enable_if<!use_trivial_move<TYPE>::value>::type |
| 203 | inline |
| 204 | move_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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 221 | template<typename TYPE> |
| 222 | typename std::enable_if<use_trivial_move<TYPE>::value>::type |
| 223 | inline |
| 224 | move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) { |
| 225 | memmove(d, s, n*sizeof(TYPE)); |
| 226 | } |
| 227 | |
| 228 | template<typename TYPE> |
| 229 | typename std::enable_if<!use_trivial_move<TYPE>::value>::type |
| 230 | inline |
| 231 | move_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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | } |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 239 | if (!traits<TYPE>::has_trivial_dtor) { |
| 240 | s->~TYPE(); |
| 241 | } |
| 242 | d++, s++; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 245 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | // --------------------------------------------------------------------------- |
| 247 | |
| 248 | /* |
| 249 | * a key/value pair |
| 250 | */ |
| 251 | |
| 252 | template <typename KEY, typename VALUE> |
| 253 | struct key_value_pair_t { |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 254 | typedef KEY key_t; |
| 255 | typedef VALUE value_t; |
| 256 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | 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 Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 261 | 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | key_value_pair_t(const KEY& k, const VALUE& v) : key(k), value(v) { } |
Chih-Hung Hsieh | 2a92996 | 2016-08-02 12:14:47 -0700 | [diff] [blame] | 267 | explicit key_value_pair_t(const KEY& k) : key(k) { } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | inline bool operator < (const key_value_pair_t& o) const { |
| 269 | return strictly_order_type(key, o.key); |
| 270 | } |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 271 | inline const KEY& getKey() const { |
| 272 | return key; |
| 273 | } |
| 274 | inline const VALUE& getValue() const { |
| 275 | return value; |
| 276 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | template <typename K, typename V> |
| 280 | struct trait_trivial_ctor< key_value_pair_t<K, V> > |
| 281 | { enum { value = aggregate_traits<K,V>::has_trivial_ctor }; }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | template <typename K, typename V> |
| 283 | struct trait_trivial_dtor< key_value_pair_t<K, V> > |
| 284 | { enum { value = aggregate_traits<K,V>::has_trivial_dtor }; }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | template <typename K, typename V> |
| 286 | struct trait_trivial_copy< key_value_pair_t<K, V> > |
| 287 | { enum { value = aggregate_traits<K,V>::has_trivial_copy }; }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | template <typename K, typename V> |
Mathias Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 289 | struct trait_trivial_move< key_value_pair_t<K, V> > |
| 290 | { enum { value = aggregate_traits<K,V>::has_trivial_move }; }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | |
| 292 | // --------------------------------------------------------------------------- |
| 293 | |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 294 | /* |
| 295 | * Hash codes. |
| 296 | */ |
| 297 | typedef uint32_t hash_t; |
| 298 | |
| 299 | template <typename TKey> |
| 300 | hash_t hash_type(const TKey& key); |
| 301 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 302 | /* Built-in hash code specializations */ |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 303 | #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 Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 310 | R newValue; \ |
| 311 | static_assert(sizeof(newValue) == sizeof(value), "size mismatch"); \ |
| 312 | memcpy(&newValue, &value, sizeof(newValue)); \ |
| 313 | return hash_type(newValue); \ |
| 314 | } |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 315 | |
| 316 | ANDROID_INT32_HASH(bool) |
Jeff Brown | 142dbcd | 2011-11-24 11:54:21 -0800 | [diff] [blame] | 317 | ANDROID_INT32_HASH(int8_t) |
| 318 | ANDROID_INT32_HASH(uint8_t) |
| 319 | ANDROID_INT32_HASH(int16_t) |
| 320 | ANDROID_INT32_HASH(uint16_t) |
| 321 | ANDROID_INT32_HASH(int32_t) |
| 322 | ANDROID_INT32_HASH(uint32_t) |
| 323 | ANDROID_INT64_HASH(int64_t) |
| 324 | ANDROID_INT64_HASH(uint64_t) |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 325 | ANDROID_REINTERPRET_HASH(float, uint32_t) |
| 326 | ANDROID_REINTERPRET_HASH(double, uint64_t) |
| 327 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 328 | template <typename T> inline hash_t hash_type(T* const & value) { |
Jeff Brown | e735f23 | 2011-11-14 18:29:15 -0800 | [diff] [blame] | 329 | return hash_type(uintptr_t(value)); |
| 330 | } |
| 331 | |
Pirama Arumuga Nainar | eab48ce | 2018-04-10 14:31:29 -0700 | [diff] [blame] | 332 | } // namespace android |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | |
| 334 | // --------------------------------------------------------------------------- |
| 335 | |
| 336 | #endif // ANDROID_TYPE_HELPERS_H |