array.h - Engine C API Reference

array.h
  1. #pragma once
  2. #include "allocator.h"
  3. #include <algorithm>
  4. namespace stingray_plugin_foundation {
  5. struct NoAllocator;
  6. // An Array<> is a simplified Vector<> for POD types that does not call
  7. // constructors or destructors for its elements (it doesn't even zero the
  8. // memory). It also assumes that the members can be moved and copied with
  9. // a memmove operation and compared with memcmp.
  10. template <class T>
  11. class Array
  12. {
  13. public:
  14. // Type definitions
  15. typedef Array<T> this_type;
  16. typedef T value_type;
  17. typedef T* pointer;
  18. typedef const T* const_pointer;
  19. typedef T& reference;
  20. typedef const T& const_reference;
  21. typedef pointer iterator;
  22. typedef const_pointer const_iterator;
  23. // Constructors
  24. // Creates an array that uses the specified allocator to allocate memory.
  25. Array(Allocator &allocator);
  26. // Creates an array and presizes it to the specified size.
  27. Array(unsigned size, Allocator &allocator);
  28. // Creates an array without an allocator, one must be provided later with
  29. // set_allocator() before the array is resized.
  30. Array(const NoAllocator &);
  31. // Sets the allocator of the array. You can only call this before any
  32. // allocations have been made by the array.
  33. void set_allocator(Allocator &allocator);
  34. Array( const Array<T> &o );
  35. ~Array() {reset();}
  36. // Asignment operator -- note that we do not support chained assignments.
  37. void operator=(const Array<T> &o);
  38. // std::vector interface
  39. iterator begin() {return _data;}
  40. const_iterator begin() const {return _data;}
  41. iterator end() {return _data + _size;}
  42. const_iterator end() const {return _data + _size;}
  43. unsigned size() const {return _size;}
  44. unsigned capacity() const {return _capacity;}
  45. bool any() const {return _size != 0;}
  46. bool empty() const {return _size == 0;}
  47. reference operator[](unsigned i);
  48. const_reference operator[](unsigned i) const;
  49. void reserve(unsigned capacity);
  50. reference front() {return _data[0];}
  51. const_reference front() const {return _data[0];}
  52. reference back() {return _data[_size-1];}
  53. const_reference back() const {return _data[_size-1];}
  54. template <class ASSIGNABLE> void push_back(const ASSIGNABLE &item);
  55. void pop_back();
  56. void swap(Array<T> &o);
  57. template <class ASSIGNABLE> iterator insert(iterator pos, const ASSIGNABLE & x);
  58. iterator insert(iterator pos);
  59. void insert(iterator pos, const_iterator from, const_iterator to);
  60. iterator erase(iterator pos);
  61. iterator erase(iterator first, iterator last);
  62. void clear() {resize(0);}
  63. void resize(unsigned size);
  64. // Serializes the array to the stream.
  65. template <class STREAM> void serialize(STREAM &stream);
  66. bool operator==(const Array<T> &o) const;
  67. bool operator<(const Array<T> &o) const;
  68. // Array extensions
  69. // Resets the array to initial state (no memory allocated).
  70. void reset() {set_capacity(0);}
  71. // Extends the array with the specified number of elements and returns a pointer to the first new element.
  72. pointer extend(unsigned elements) { resize(size() + elements); return _data + _size - elements; }
  73. // Extends the array with one elements and returns a reference to the newly created element.
  74. reference extend() { resize(size() + 1); return back(); }
  75. // Returns the allocator of the array.
  76. Allocator &allocator() const {return *_allocator;}
  77. // Sets the capacity of the array to exactly the specified ammount.
  78. // This operation always reallocates the memory of the array, no
  79. // swap trick is necessary to enforce reallocation.
  80. void set_capacity(unsigned capacity);
  81. // Trims the array so that the capacity corresponds to its current size.
  82. void trim() {set_capacity(size());}
  83. // Finds the first occurrence of x in the array and returns it.
  84. template <typename EQUATABLE> iterator find(const EQUATABLE &x) {return std::find(begin(), end(), x);}
  85. // Finds the first occurrence of x in the array and returns it.
  86. template <typename EQUATABLE> const_iterator find(const EQUATABLE &x) const {return std::find(begin(), end(), x);}
  87. // Returns the index of x in the array (or size() if it is not in the array).
  88. template <typename EQUATABLE> unsigned index_of(const EQUATABLE &x) const {return (unsigned)(find(x) - begin());}
  89. // Returns if x is in the array
  90. template <typename EQUATABLE> bool has(const EQUATABLE &x) const {return (find(x) != end());}
  91. // Erases the first occurrence of x in the array.
  92. template <typename EQUATABLE> void erase(const EQUATABLE &item);
  93. // "Steals" the data buffer from the array. The array will be empty after this operation. The "stealer" is
  94. // responsible for deallocating the stolen buffer.
  95. pointer steal();
  96. private:
  97. void move(pointer to, pointer from, unsigned n);
  98. void copy(pointer to, const_pointer from, unsigned n);
  99. void grow(unsigned min_capacity = 0);
  100. private:
  101. unsigned _size;
  102. unsigned _capacity;
  103. pointer _data;
  104. Allocator * _allocator;
  105. };
  106. } // namespace stingray_plugin_foundation
  107. #include "array.inl"