OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
7 | 7 |
8 #include <new> | 8 #include <new> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 | 273 |
274 // UTF-8 encoded | 274 // UTF-8 encoded |
275 typedef Array_Data<char> String_Data; | 275 typedef Array_Data<char> String_Data; |
276 | 276 |
277 template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {}; | 277 template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {}; |
278 | 278 |
279 template <typename T> struct ArrayTraits<T, false> { | 279 template <typename T> struct ArrayTraits<T, false> { |
280 typedef T StorageType; | 280 typedef T StorageType; |
281 typedef typename std::vector<T>::reference RefType; | 281 typedef typename std::vector<T>::reference RefType; |
282 typedef typename std::vector<T>::const_reference ConstRefType; | 282 typedef typename std::vector<T>::const_reference ConstRefType; |
283 typedef ConstRefType SinkType; | |
283 static inline void Initialize(std::vector<T>* vec) { | 284 static inline void Initialize(std::vector<T>* vec) { |
284 } | 285 } |
285 static inline void Finalize(std::vector<T>* vec) { | 286 static inline void Finalize(std::vector<T>* vec) { |
286 } | 287 } |
287 static inline ConstRefType at(const std::vector<T>* vec, size_t offset) { | 288 static inline ConstRefType at(const std::vector<T>* vec, size_t offset) { |
288 return vec->at(offset); | 289 return vec->at(offset); |
289 } | 290 } |
290 static inline RefType at(std::vector<T>* vec, size_t offset) { | 291 static inline RefType at(std::vector<T>* vec, size_t offset) { |
291 return vec->at(offset); | 292 return vec->at(offset); |
292 } | 293 } |
294 static inline void Resize(std::vector<T>* vec, size_t size) { | |
295 vec->resize(size); | |
296 } | |
297 static inline void PushBack(std::vector<T>* vec, SinkType value) { | |
darin (slow to review)
2014/06/10 22:33:13
I don't quite understand the use of the term "Sink
Sam McNally
2014/06/10 23:55:17
Done.
| |
298 vec->push_back(value); | |
299 } | |
293 }; | 300 }; |
294 | 301 |
295 template <typename T> struct ArrayTraits<T, true> { | 302 template <typename T> struct ArrayTraits<T, true> { |
296 struct StorageType { | 303 struct StorageType { |
297 char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned. | 304 char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned. |
298 }; | 305 }; |
299 typedef T& RefType; | 306 typedef T& RefType; |
300 typedef const T& ConstRefType; | 307 typedef const T& ConstRefType; |
308 typedef T SinkType; | |
301 static inline void Initialize(std::vector<StorageType>* vec) { | 309 static inline void Initialize(std::vector<StorageType>* vec) { |
302 for (size_t i = 0; i < vec->size(); ++i) | 310 for (size_t i = 0; i < vec->size(); ++i) |
303 new (vec->at(i).buf) T(); | 311 new (vec->at(i).buf) T(); |
304 } | 312 } |
305 static inline void Finalize(std::vector<StorageType>* vec) { | 313 static inline void Finalize(std::vector<StorageType>* vec) { |
306 for (size_t i = 0; i < vec->size(); ++i) | 314 for (size_t i = 0; i < vec->size(); ++i) |
307 reinterpret_cast<T*>(vec->at(i).buf)->~T(); | 315 reinterpret_cast<T*>(vec->at(i).buf)->~T(); |
308 } | 316 } |
309 static inline ConstRefType at(const std::vector<StorageType>* vec, | 317 static inline ConstRefType at(const std::vector<StorageType>* vec, |
310 size_t offset) { | 318 size_t offset) { |
311 return *reinterpret_cast<const T*>(vec->at(offset).buf); | 319 return *reinterpret_cast<const T*>(vec->at(offset).buf); |
312 } | 320 } |
313 static inline RefType at(std::vector<StorageType>* vec, size_t offset) { | 321 static inline RefType at(std::vector<StorageType>* vec, size_t offset) { |
314 return *reinterpret_cast<T*>(vec->at(offset).buf); | 322 return *reinterpret_cast<T*>(vec->at(offset).buf); |
315 } | 323 } |
324 static inline void Resize(std::vector<StorageType>* vec, size_t size) { | |
325 size_t old_size = vec->size(); | |
326 for (size_t i = size; i < old_size; i++) | |
327 reinterpret_cast<T*>(vec->at(i).buf)->~T(); | |
328 ResizeStorage(vec, size); | |
329 for (size_t i = old_size; i < vec->size(); i++) | |
330 new (vec->at(i).buf) T(); | |
331 } | |
332 static inline void PushBack(std::vector<StorageType>* vec, RefType value) { | |
333 size_t old_size = vec->size(); | |
334 ResizeStorage(vec, old_size + 1); | |
335 new (vec->at(old_size).buf) T(value.Pass()); | |
336 } | |
337 static inline void ResizeStorage(std::vector<StorageType>* vec, size_t size) { | |
338 if (size <= vec->capacity()) { | |
339 vec->resize(size); | |
340 return; | |
341 } | |
342 std::vector<StorageType> new_storage(size); | |
343 for (size_t i = 0; i < vec->size(); i++) | |
344 new (new_storage.at(i).buf) T(at(vec, i).Pass()); | |
345 vec->swap(new_storage); | |
346 Finalize(&new_storage); | |
347 } | |
316 }; | 348 }; |
317 | 349 |
318 template <> struct WrapperTraits<String, false> { | 350 template <> struct WrapperTraits<String, false> { |
319 typedef String_Data* DataType; | 351 typedef String_Data* DataType; |
320 }; | 352 }; |
321 | 353 |
322 } // namespace internal | 354 } // namespace internal |
323 } // namespace mojo | 355 } // namespace mojo |
324 | 356 |
325 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 357 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
OLD | NEW |