VectorBase.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "Urho3D.h"
  24. #include "Swap.h"
  25. namespace Urho3D
  26. {
  27. /// Random access iterator.
  28. template <class T> struct RandomAccessIterator
  29. {
  30. /// Construct.
  31. RandomAccessIterator() :
  32. ptr_(0)
  33. {
  34. }
  35. /// Construct with an object pointer.
  36. explicit RandomAccessIterator(T* ptr) :
  37. ptr_(ptr)
  38. {
  39. }
  40. /// Point to the object.
  41. T* operator -> () const { return ptr_; }
  42. /// Dereference the object.
  43. T& operator * () const { return *ptr_; }
  44. /// Preincrement the pointer.
  45. RandomAccessIterator<T>& operator ++ () { ++ptr_; return *this; }
  46. /// Postincrement the pointer.
  47. RandomAccessIterator<T> operator ++ (int) { RandomAccessIterator<T> it = *this; ++ptr_; return it; }
  48. /// Predecrement the pointer.
  49. RandomAccessIterator<T>& operator -- () { --ptr_; return *this; }
  50. /// Postdecrement the pointer.
  51. RandomAccessIterator<T> operator -- (int) { RandomAccessIterator<T> it = *this; --ptr_; return it; }
  52. /// Add an offset to the pointer.
  53. RandomAccessIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  54. /// Subtract an offset from the pointer.
  55. RandomAccessIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  56. /// Add an offset to the pointer.
  57. RandomAccessIterator<T> operator + (int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  58. /// Subtract an offset from the pointer.
  59. RandomAccessIterator<T> operator - (int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  60. /// Calculate offset to another iterator.
  61. int operator - (const RandomAccessIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  62. /// Test for equality with another iterator.
  63. bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  64. /// Test for inequality with another iterator.
  65. bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  66. /// Test for less than with another iterator.
  67. bool operator < (const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  68. /// Test for greater than with another iterator.
  69. bool operator > (const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  70. /// Test for less than or equal with another iterator.
  71. bool operator <= (const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  72. /// Test for greater than or equal with another iterator.
  73. bool operator >= (const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  74. /// Pointer.
  75. T* ptr_;
  76. };
  77. /// Random access const iterator.
  78. template <class T> struct RandomAccessConstIterator
  79. {
  80. /// Construct.
  81. RandomAccessConstIterator() :
  82. ptr_(0)
  83. {
  84. }
  85. /// Construct with an object pointer.
  86. explicit RandomAccessConstIterator(T* ptr) :
  87. ptr_(ptr)
  88. {
  89. }
  90. /// Construct from a non-const iterator.
  91. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  92. ptr_(rhs.ptr_)
  93. {
  94. }
  95. /// Assign from a non-const iterator.
  96. RandomAccessConstIterator<T>& operator = (const RandomAccessIterator<T>& rhs) { ptr_ = rhs.ptr_; return *this; }
  97. /// Point to the object.
  98. const T* operator -> () const { return ptr_; }
  99. /// Dereference the object.
  100. const T& operator * () const { return *ptr_; }
  101. /// Preincrement the pointer.
  102. RandomAccessConstIterator<T>& operator ++ () { ++ptr_; return *this; }
  103. /// Postincrement the pointer.
  104. RandomAccessConstIterator<T> operator ++ (int) { RandomAccessConstIterator<T> it = *this; ++ptr_; return it; }
  105. /// Predecrement the pointer.
  106. RandomAccessConstIterator<T>& operator -- () { --ptr_; return *this; }
  107. /// Postdecrement the pointer.
  108. RandomAccessConstIterator<T> operator -- (int) { RandomAccessConstIterator<T> it = *this; --ptr_; return it; }
  109. /// Add an offset to the pointer.
  110. RandomAccessConstIterator<T>& operator += (int value) { ptr_ += value; return *this; }
  111. /// Subtract an offset from the pointer.
  112. RandomAccessConstIterator<T>& operator -= (int value) { ptr_ -= value; return *this; }
  113. /// Add an offset to the pointer.
  114. RandomAccessConstIterator<T> operator + (int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  115. /// Subtract an offset from the pointer.
  116. RandomAccessConstIterator<T> operator - (int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  117. /// Calculate offset to another iterator.
  118. int operator - (const RandomAccessConstIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  119. /// Test for equality with another iterator.
  120. bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  121. /// Test for inequality with another iterator.
  122. bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  123. /// Test for less than with another iterator.
  124. bool operator < (const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  125. /// Test for greater than with another iterator.
  126. bool operator > (const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  127. /// Test for less than or equal with another iterator.
  128. bool operator <= (const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  129. /// Test for greater than or equal with another iterator.
  130. bool operator >= (const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  131. /// Pointer.
  132. T* ptr_;
  133. };
  134. /// %Vector base class.
  135. class URHO3D_API VectorBase
  136. {
  137. public:
  138. /// Construct.
  139. VectorBase() :
  140. size_(0),
  141. capacity_(0),
  142. buffer_(0)
  143. {
  144. }
  145. /// Swap with another vector.
  146. void Swap(VectorBase& rhs)
  147. {
  148. Urho3D::Swap(size_, rhs.size_);
  149. Urho3D::Swap(capacity_, rhs.capacity_);
  150. Urho3D::Swap(buffer_, rhs.buffer_);
  151. }
  152. protected:
  153. static unsigned char* AllocateBuffer(unsigned size);
  154. /// Size of vector.
  155. unsigned size_;
  156. /// Buffer capacity.
  157. unsigned capacity_;
  158. /// Buffer.
  159. unsigned char* buffer_;
  160. };
  161. }