VectorBase.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/Swap.h"
  24. namespace Atomic
  25. {
  26. /// Random access iterator.
  27. template <class T> struct RandomAccessIterator
  28. {
  29. /// Construct.
  30. RandomAccessIterator() :
  31. ptr_(0)
  32. {
  33. }
  34. /// Construct with an object pointer.
  35. explicit RandomAccessIterator(T* ptr) :
  36. ptr_(ptr)
  37. {
  38. }
  39. /// Point to the object.
  40. T* operator ->() const { return ptr_; }
  41. /// Dereference the object.
  42. T& operator *() const { return *ptr_; }
  43. /// Preincrement the pointer.
  44. RandomAccessIterator<T>& operator ++()
  45. {
  46. ++ptr_;
  47. return *this;
  48. }
  49. /// Postincrement the pointer.
  50. RandomAccessIterator<T> operator ++(int)
  51. {
  52. RandomAccessIterator<T> it = *this;
  53. ++ptr_;
  54. return it;
  55. }
  56. /// Predecrement the pointer.
  57. RandomAccessIterator<T>& operator --()
  58. {
  59. --ptr_;
  60. return *this;
  61. }
  62. /// Postdecrement the pointer.
  63. RandomAccessIterator<T> operator --(int)
  64. {
  65. RandomAccessIterator<T> it = *this;
  66. --ptr_;
  67. return it;
  68. }
  69. /// Add an offset to the pointer.
  70. RandomAccessIterator<T>& operator +=(int value)
  71. {
  72. ptr_ += value;
  73. return *this;
  74. }
  75. /// Subtract an offset from the pointer.
  76. RandomAccessIterator<T>& operator -=(int value)
  77. {
  78. ptr_ -= value;
  79. return *this;
  80. }
  81. /// Add an offset to the pointer.
  82. RandomAccessIterator<T> operator +(int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  83. /// Subtract an offset from the pointer.
  84. RandomAccessIterator<T> operator -(int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  85. /// Calculate offset to another iterator.
  86. int operator -(const RandomAccessIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  87. /// Test for equality with another iterator.
  88. bool operator ==(const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  89. /// Test for inequality with another iterator.
  90. bool operator !=(const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  91. /// Test for less than with another iterator.
  92. bool operator <(const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  93. /// Test for greater than with another iterator.
  94. bool operator >(const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  95. /// Test for less than or equal with another iterator.
  96. bool operator <=(const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  97. /// Test for greater than or equal with another iterator.
  98. bool operator >=(const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  99. /// Pointer.
  100. T* ptr_;
  101. };
  102. /// Random access const iterator.
  103. template <class T> struct RandomAccessConstIterator
  104. {
  105. /// Construct.
  106. RandomAccessConstIterator() :
  107. ptr_(0)
  108. {
  109. }
  110. /// Construct with an object pointer.
  111. explicit RandomAccessConstIterator(T* ptr) :
  112. ptr_(ptr)
  113. {
  114. }
  115. /// Construct from a non-const iterator.
  116. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  117. ptr_(rhs.ptr_)
  118. {
  119. }
  120. /// Assign from a non-const iterator.
  121. RandomAccessConstIterator<T>& operator =(const RandomAccessIterator<T>& rhs)
  122. {
  123. ptr_ = rhs.ptr_;
  124. return *this;
  125. }
  126. /// Point to the object.
  127. const T* operator ->() const { return ptr_; }
  128. /// Dereference the object.
  129. const T& operator *() const { return *ptr_; }
  130. /// Preincrement the pointer.
  131. RandomAccessConstIterator<T>& operator ++()
  132. {
  133. ++ptr_;
  134. return *this;
  135. }
  136. /// Postincrement the pointer.
  137. RandomAccessConstIterator<T> operator ++(int)
  138. {
  139. RandomAccessConstIterator<T> it = *this;
  140. ++ptr_;
  141. return it;
  142. }
  143. /// Predecrement the pointer.
  144. RandomAccessConstIterator<T>& operator --()
  145. {
  146. --ptr_;
  147. return *this;
  148. }
  149. /// Postdecrement the pointer.
  150. RandomAccessConstIterator<T> operator --(int)
  151. {
  152. RandomAccessConstIterator<T> it = *this;
  153. --ptr_;
  154. return it;
  155. }
  156. /// Add an offset to the pointer.
  157. RandomAccessConstIterator<T>& operator +=(int value)
  158. {
  159. ptr_ += value;
  160. return *this;
  161. }
  162. /// Subtract an offset from the pointer.
  163. RandomAccessConstIterator<T>& operator -=(int value)
  164. {
  165. ptr_ -= value;
  166. return *this;
  167. }
  168. /// Add an offset to the pointer.
  169. RandomAccessConstIterator<T> operator +(int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  170. /// Subtract an offset from the pointer.
  171. RandomAccessConstIterator<T> operator -(int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  172. /// Calculate offset to another iterator.
  173. int operator -(const RandomAccessConstIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  174. /// Test for equality with another iterator.
  175. bool operator ==(const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  176. /// Test for inequality with another iterator.
  177. bool operator !=(const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  178. /// Test for less than with another iterator.
  179. bool operator <(const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  180. /// Test for greater than with another iterator.
  181. bool operator >(const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  182. /// Test for less than or equal with another iterator.
  183. bool operator <=(const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  184. /// Test for greater than or equal with another iterator.
  185. bool operator >=(const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  186. /// Pointer.
  187. T* ptr_;
  188. };
  189. /// %Vector base class.
  190. /** Note that to prevent extra memory use due to vtable pointer, %VectorBase intentionally does not declare a virtual destructor
  191. and therefore %VectorBase pointers should never be used.
  192. */
  193. class ATOMIC_API VectorBase
  194. {
  195. public:
  196. /// Construct.
  197. VectorBase() :
  198. size_(0),
  199. capacity_(0),
  200. buffer_(0)
  201. {
  202. }
  203. /// Swap with another vector.
  204. void Swap(VectorBase& rhs)
  205. {
  206. Atomic::Swap(size_, rhs.size_);
  207. Atomic::Swap(capacity_, rhs.capacity_);
  208. Atomic::Swap(buffer_, rhs.buffer_);
  209. }
  210. protected:
  211. static unsigned char* AllocateBuffer(unsigned size);
  212. /// Size of vector.
  213. unsigned size_;
  214. /// Buffer capacity.
  215. unsigned capacity_;
  216. /// Buffer.
  217. unsigned char* buffer_;
  218. };
  219. }