bufferptr.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 2.3.15
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. #include <memory.h>
  26. #include <malloc.h>
  27. template<class T, size_t MAX_CACHE_SIZE = 0>
  28. class CBufferPtrT
  29. {
  30. public:
  31. explicit CBufferPtrT(size_t size = 0, bool zero = false) {Reset(); Malloc(size, zero);}
  32. explicit CBufferPtrT(const T* pch, size_t size) {Reset(); Copy(pch, size);}
  33. CBufferPtrT(const CBufferPtrT& other) {Reset(); Copy(other);}
  34. template<size_t S> CBufferPtrT(const CBufferPtrT<T, S>& other) {Reset(); Copy(other);}
  35. ~CBufferPtrT() {Free();}
  36. T* Malloc(size_t size = 1, bool zero = false)
  37. {
  38. Free();
  39. return Alloc(size, zero, false);
  40. }
  41. T* Realloc(size_t size, bool zero = false)
  42. {
  43. return Alloc(size, zero, true);
  44. }
  45. void Free()
  46. {
  47. if(m_pch)
  48. {
  49. free(m_pch);
  50. Reset();
  51. }
  52. }
  53. template<size_t S> CBufferPtrT& Copy(const CBufferPtrT<T, S>& other)
  54. {
  55. if((void*)&other != (void*)this)
  56. Copy(other.Ptr(), other.Size());
  57. return *this;
  58. }
  59. CBufferPtrT& Copy(const T* pch, size_t size)
  60. {
  61. Malloc(size);
  62. if(m_pch)
  63. memcpy(m_pch, pch, size * sizeof(T));
  64. return *this;
  65. }
  66. template<size_t S> CBufferPtrT& Cat(const CBufferPtrT<T, S>& other)
  67. {
  68. if((void*)&other != (void*)this)
  69. Cat(other.Ptr(), other.Size());
  70. return *this;
  71. }
  72. CBufferPtrT& Cat(const T* pch, size_t size = 1)
  73. {
  74. size_t pre_size = m_size;
  75. Realloc(m_size + size);
  76. if(m_pch)
  77. memcpy(m_pch + pre_size, pch, size * sizeof(T));
  78. return *this;
  79. }
  80. template<size_t S> bool Equal(const CBufferPtrT<T, S>& other) const
  81. {
  82. if((void*)&other == (void*)this)
  83. return true;
  84. else if(m_size != other.Size())
  85. return false;
  86. else if(m_size == 0)
  87. return true;
  88. else
  89. return (memcmp(m_pch, other.Ptr(), m_size * sizeof(T)) == 0);
  90. }
  91. bool Equal(T* pch) const
  92. {
  93. if(m_pch == pch)
  94. return true;
  95. else if(!m_pch || !pch)
  96. return false;
  97. else
  98. return (memcmp(m_pch, pch, m_size * sizeof(T)) == 0);
  99. }
  100. T* Ptr() {return m_pch;}
  101. const T* Ptr() const {return m_pch;}
  102. T& Get(int i) {return *(m_pch + i);}
  103. const T& Get(int i) const {return *(m_pch + i);}
  104. size_t Size() const {return m_size;}
  105. bool IsValid() const {return m_pch != 0;}
  106. operator T* () {return Ptr();}
  107. operator const T* () const {return Ptr();}
  108. T& operator [] (int i) {return Get(i);}
  109. const T& operator [] (int i) const {return Get(i);}
  110. bool operator == (T* pv) const {return Equal(pv);}
  111. template<size_t S> bool operator == (const CBufferPtrT<T, S>& other) {return Equal(other);}
  112. CBufferPtrT& operator = (const CBufferPtrT& other) {return Copy(other);}
  113. template<size_t S> CBufferPtrT& operator = (const CBufferPtrT<T, S>& other) {return Copy(other);}
  114. private:
  115. void Reset() {m_pch = 0; m_size = 0; m_capacity = 0;}
  116. size_t GetAllocSize(size_t size) {return max(size, min(size * 2, m_size + MAX_CACHE_SIZE));}
  117. T* Alloc(size_t size, bool zero = false, bool is_realloc = false)
  118. {
  119. if(size >= 0 && size != m_size)
  120. {
  121. size_t rsize = GetAllocSize(size);
  122. if(size > m_capacity || rsize < m_size)
  123. {
  124. m_pch = is_realloc ?
  125. (T*)realloc(m_pch, rsize * sizeof(T)) :
  126. (T*)malloc(rsize * sizeof(T)) ;
  127. if(m_pch || rsize == 0)
  128. {
  129. m_size = size;
  130. m_capacity = rsize;
  131. }
  132. else
  133. Reset();
  134. }
  135. else
  136. m_size = size;
  137. }
  138. if(zero && m_pch)
  139. memset(m_pch, 0, m_size * sizeof(T));
  140. return m_pch;
  141. }
  142. private:
  143. T* m_pch;
  144. size_t m_size;
  145. size_t m_capacity;
  146. };
  147. typedef CBufferPtrT<char> CCharBufferPtr;
  148. typedef CBufferPtrT<wchar_t> CWCharBufferPtr;
  149. typedef CBufferPtrT<unsigned char> CByteBufferPtr;
  150. typedef CByteBufferPtr CBufferPtr;
  151. #ifdef _UNICODE
  152. typedef CWCharBufferPtr CTCharBufferPtr;
  153. #else
  154. typedef CCharBufferPtr CTCharBufferPtr;
  155. #endif