Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
4.9 KiB

  1. #ifndef CRYPTOPP_SMARTPTR_H
  2. #define CRYPTOPP_SMARTPTR_H
  3. #include "config.h"
  4. #include <algorithm>
  5. NAMESPACE_BEGIN(CryptoPP)
  6. template <class T> class simple_ptr
  7. {
  8. public:
  9. simple_ptr(T *p = NULL) : m_p(p) {}
  10. ~simple_ptr() {delete m_p; m_p = NULL;} // set m_p to NULL so double destruction (which might occur in Singleton) will be harmless
  11. T *m_p;
  12. };
  13. template <class T> class member_ptr
  14. {
  15. public:
  16. explicit member_ptr(T *p = NULL) : m_p(p) {}
  17. ~member_ptr();
  18. const T& operator*() const { return *m_p; }
  19. T& operator*() { return *m_p; }
  20. const T* operator->() const { return m_p; }
  21. T* operator->() { return m_p; }
  22. const T* get() const { return m_p; }
  23. T* get() { return m_p; }
  24. T* release()
  25. {
  26. T *old_p = m_p;
  27. m_p = 0;
  28. return old_p;
  29. }
  30. void reset(T *p = 0);
  31. protected:
  32. member_ptr(const member_ptr<T>& rhs); // copy not allowed
  33. void operator=(const member_ptr<T>& rhs); // assignment not allowed
  34. T *m_p;
  35. };
  36. template <class T> member_ptr<T>::~member_ptr() {delete m_p;}
  37. template <class T> void member_ptr<T>::reset(T *p) {delete m_p; m_p = p;}
  38. // ********************************************************
  39. template<class T> class value_ptr : public member_ptr<T>
  40. {
  41. public:
  42. value_ptr(const T &obj) : member_ptr<T>(new T(obj)) {}
  43. value_ptr(T *p = NULL) : member_ptr<T>(p) {}
  44. value_ptr(const value_ptr<T>& rhs)
  45. : member_ptr<T>(rhs.m_p ? new T(*rhs.m_p) : NULL) {}
  46. value_ptr<T>& operator=(const value_ptr<T>& rhs);
  47. bool operator==(const value_ptr<T>& rhs)
  48. {
  49. return (!this->m_p && !rhs.m_p) || (this->m_p && rhs.m_p && *this->m_p == *rhs.m_p);
  50. }
  51. };
  52. template <class T> value_ptr<T>& value_ptr<T>::operator=(const value_ptr<T>& rhs)
  53. {
  54. T *old_p = this->m_p;
  55. this->m_p = rhs.m_p ? new T(*rhs.m_p) : NULL;
  56. delete old_p;
  57. return *this;
  58. }
  59. // ********************************************************
  60. template<class T> class clonable_ptr : public member_ptr<T>
  61. {
  62. public:
  63. clonable_ptr(const T &obj) : member_ptr<T>(obj.Clone()) {}
  64. clonable_ptr(T *p = NULL) : member_ptr<T>(p) {}
  65. clonable_ptr(const clonable_ptr<T>& rhs)
  66. : member_ptr<T>(rhs.m_p ? rhs.m_p->Clone() : NULL) {}
  67. clonable_ptr<T>& operator=(const clonable_ptr<T>& rhs);
  68. };
  69. template <class T> clonable_ptr<T>& clonable_ptr<T>::operator=(const clonable_ptr<T>& rhs)
  70. {
  71. T *old_p = this->m_p;
  72. this->m_p = rhs.m_p ? rhs.m_p->Clone() : NULL;
  73. delete old_p;
  74. return *this;
  75. }
  76. // ********************************************************
  77. template<class T> class counted_ptr
  78. {
  79. public:
  80. explicit counted_ptr(T *p = 0);
  81. counted_ptr(const T &r) : m_p(0) {attach(r);}
  82. counted_ptr(const counted_ptr<T>& rhs);
  83. ~counted_ptr();
  84. const T& operator*() const { return *m_p; }
  85. T& operator*() { return *m_p; }
  86. const T* operator->() const { return m_p; }
  87. T* operator->() { return get(); }
  88. const T* get() const { return m_p; }
  89. T* get();
  90. void attach(const T &p);
  91. counted_ptr<T> & operator=(const counted_ptr<T>& rhs);
  92. private:
  93. T *m_p;
  94. };
  95. template <class T> counted_ptr<T>::counted_ptr(T *p)
  96. : m_p(p)
  97. {
  98. if (m_p)
  99. m_p->m_referenceCount = 1;
  100. }
  101. template <class T> counted_ptr<T>::counted_ptr(const counted_ptr<T>& rhs)
  102. : m_p(rhs.m_p)
  103. {
  104. if (m_p)
  105. m_p->m_referenceCount++;
  106. }
  107. template <class T> counted_ptr<T>::~counted_ptr()
  108. {
  109. if (m_p && --m_p->m_referenceCount == 0)
  110. delete m_p;
  111. }
  112. template <class T> void counted_ptr<T>::attach(const T &r)
  113. {
  114. if (m_p && --m_p->m_referenceCount == 0)
  115. delete m_p;
  116. if (r.m_referenceCount == 0)
  117. {
  118. m_p = r.clone();
  119. m_p->m_referenceCount = 1;
  120. }
  121. else
  122. {
  123. m_p = const_cast<T *>(&r);
  124. m_p->m_referenceCount++;
  125. }
  126. }
  127. template <class T> T* counted_ptr<T>::get()
  128. {
  129. if (m_p && m_p->m_referenceCount > 1)
  130. {
  131. T *temp = m_p->clone();
  132. m_p->m_referenceCount--;
  133. m_p = temp;
  134. m_p->m_referenceCount = 1;
  135. }
  136. return m_p;
  137. }
  138. template <class T> counted_ptr<T> & counted_ptr<T>::operator=(const counted_ptr<T>& rhs)
  139. {
  140. if (m_p != rhs.m_p)
  141. {
  142. if (m_p && --m_p->m_referenceCount == 0)
  143. delete m_p;
  144. m_p = rhs.m_p;
  145. if (m_p)
  146. m_p->m_referenceCount++;
  147. }
  148. return *this;
  149. }
  150. // ********************************************************
  151. template <class T> class vector_member_ptrs
  152. {
  153. public:
  154. vector_member_ptrs(size_t sizex=0)
  155. : m_size(sizex), m_ptr(new member_ptr<T>[sizex]) {}
  156. ~vector_member_ptrs()
  157. {delete [] this->m_ptr;}
  158. member_ptr<T>& operator[](size_t index)
  159. {assert(index<this->m_size); return this->m_ptr[index];}
  160. const member_ptr<T>& operator[](size_t index) const
  161. {assert(index<this->m_size); return this->m_ptr[index];}
  162. size_t size() const {return this->m_size;}
  163. void resize(size_t newSize)
  164. {
  165. member_ptr<T> *newPtr = new member_ptr<T>[newSize];
  166. for (size_t i=0; i<this->m_size && i<newSize; i++)
  167. newPtr[i].reset(this->m_ptr[i].release());
  168. delete [] this->m_ptr;
  169. this->m_size = newSize;
  170. this->m_ptr = newPtr;
  171. }
  172. private:
  173. vector_member_ptrs(const vector_member_ptrs<T> &c); // copy not allowed
  174. void operator=(const vector_member_ptrs<T> &x); // assignment not allowed
  175. size_t m_size;
  176. member_ptr<T> *m_ptr;
  177. };
  178. NAMESPACE_END
  179. #endif