VectorBase.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright (c) 2008-2014 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 ++ () { ++ptr_; return *this; }
  45. /// Postincrement the pointer.
  46. RandomAccessIterator<T> operator ++ (int) { RandomAccessIterator<T> it = *this; ++ptr_; return it; }
  47. /// Predecrement the pointer.
  48. RandomAccessIterator<T>& operator -- () { --ptr_; return *this; }
  49. /// Postdecrement the pointer.
  50. RandomAccessIterator<T> operator -- (int) { RandomAccessIterator<T> it = *this; --ptr_; return it; }
  51. /// Add an offset to the pointer.
  52. RandomAccessIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  53. /// Subtract an offset from the pointer.
  54. RandomAccessIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  55. /// Add an offset to the pointer.
  56. RandomAccessIterator<T> operator + (int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  57. /// Subtract an offset from the pointer.
  58. RandomAccessIterator<T> operator - (int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  59. /// Calculate offset to another iterator.
  60. int operator - (const RandomAccessIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  61. /// Test for equality with another iterator.
  62. bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  63. /// Test for inequality with another iterator.
  64. bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  65. /// Test for less than with another iterator.
  66. bool operator < (const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  67. /// Test for greater than with another iterator.
  68. bool operator > (const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  69. /// Test for less than or equal with another iterator.
  70. bool operator <= (const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  71. /// Test for greater than or equal with another iterator.
  72. bool operator >= (const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  73. /// Pointer.
  74. T* ptr_;
  75. };
  76. /// Random access const iterator.
  77. template <class T> struct RandomAccessConstIterator
  78. {
  79. /// Construct.
  80. RandomAccessConstIterator() :
  81. ptr_(0)
  82. {
  83. }
  84. /// Construct with an object pointer.
  85. explicit RandomAccessConstIterator(T* ptr) :
  86. ptr_(ptr)
  87. {
  88. }
  89. /// Construct from a non-const iterator.
  90. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  91. ptr_(rhs.ptr_)
  92. {
  93. }
  94. /// Assign from a non-const iterator.
  95. RandomAccessConstIterator<T>& operator = (const RandomAccessIterator<T>& rhs) { ptr_ = rhs.ptr_; return *this; }
  96. /// Point to the object.
  97. const T* operator -> () const { return ptr_; }
  98. /// Dereference the object.
  99. const T& operator * () const { return *ptr_; }
  100. /// Preincrement the pointer.
  101. RandomAccessConstIterator<T>& operator ++ () { ++ptr_; return *this; }
  102. /// Postincrement the pointer.
  103. RandomAccessConstIterator<T> operator ++ (int) { RandomAccessConstIterator<T> it = *this; ++ptr_; return it; }
  104. /// Predecrement the pointer.
  105. RandomAccessConstIterator<T>& operator -- () { --ptr_; return *this; }
  106. /// Postdecrement the pointer.
  107. RandomAccessConstIterator<T> operator -- (int) { RandomAccessConstIterator<T> it = *this; --ptr_; return it; }
  108. /// Add an offset to the pointer.
  109. RandomAccessConstIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  110. /// Subtract an offset from the pointer.
  111. RandomAccessConstIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  112. /// Add an offset to the pointer.
  113. RandomAccessConstIterator<T> operator + (int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  114. /// Subtract an offset from the pointer.
  115. RandomAccessConstIterator<T> operator - (int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  116. /// Calculate offset to another iterator.
  117. int operator - (const RandomAccessConstIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  118. /// Test for equality with another iterator.
  119. bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  120. /// Test for inequality with another iterator.
  121. bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  122. /// Test for less than with another iterator.
  123. bool operator < (const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  124. /// Test for greater than with another iterator.
  125. bool operator > (const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  126. /// Test for less than or equal with another iterator.
  127. bool operator <= (const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  128. /// Test for greater than or equal with another iterator.
  129. bool operator >= (const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  130. /// Pointer.
  131. T* ptr_;
  132. };
  133. /// %Vector base class.
  134. /** Note that to prevent extra memory use due to vtable pointer, %VectorBase intentionally does not declare a virtual destructor
  135. and therefore %VectorBase pointers should never be used.
  136. */
  137. class ATOMIC_API VectorBase
  138. {
  139. public:
  140. /// Construct.
  141. VectorBase() :
  142. size_(0),
  143. capacity_(0),
  144. buffer_(0)
  145. {
  146. }
  147. /// Swap with another vector.
  148. void Swap(VectorBase& rhs)
  149. {
  150. Atomic::Swap(size_, rhs.size_);
  151. Atomic::Swap(capacity_, rhs.capacity_);
  152. Atomic::Swap(buffer_, rhs.buffer_);
  153. }
  154. protected:
  155. static unsigned char* AllocateBuffer(unsigned size);
  156. /// Size of vector.
  157. unsigned size_;
  158. /// Buffer capacity.
  159. unsigned capacity_;
  160. /// Buffer.
  161. unsigned char* buffer_;
  162. };
  163. }