ministringstorage.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // "$Id$"
  3. //
  4. // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef DAHUA_MINI_STRING_STORAGE_INC_
  11. #define DAHUA_MINI_STRING_STORAGE_INC_
  12. /* This is the template for a storage policy
  13. ////////////////////////////////////////////////////////////////////////////////
  14. template <typename E, class A = @>
  15. class StoragePolicy
  16. {
  17. typedef E value_type;
  18. typedef @ iterator;
  19. typedef @ const_iterator;
  20. typedef A allocator_type;
  21. typedef @ size_type;
  22. StoragePolicy(const StoragePolicy& s);
  23. StoragePolicy(const A&);
  24. StoragePolicy(const E* s, size_type len, const A&);
  25. StoragePolicy(size_type len, E c, const A&);
  26. ~StoragePolicy();
  27. iterator begin();
  28. const_iterator begin() const;
  29. iterator end();
  30. const_iterator end() const;
  31. size_type size() const;
  32. size_type max_size() const;
  33. size_type capacity() const;
  34. void reserve(size_type res_arg);
  35. template <class ForwardIterator>
  36. void append(ForwardIterator b, ForwardIterator e);
  37. void resize(size_type newSize, E fill);
  38. void swap(StoragePolicy& rhs);
  39. const E* c_str() const;
  40. const E* data() const;
  41. A get_allocator() const;
  42. };
  43. ////////////////////////////////////////////////////////////////////////////////
  44. */
  45. #include <memory>
  46. #include <algorithm>
  47. #include <functional>
  48. #include <cassert>
  49. #include <limits>
  50. #include <stdexcept>
  51. namespace Dahua {
  52. namespace Infra {
  53. ////////////////////////////////////////////////////////////////////////////////
  54. // class template MiniStringStorage
  55. // Allocates memory with malloc
  56. ////////////////////////////////////////////////////////////////////////////////
  57. template <typename E, class A = std::allocator<E> >
  58. class MiniStringStorage
  59. {
  60. // The "public" below exists because MSVC can't do template typedefs
  61. public:
  62. struct Data
  63. {
  64. Data() : pEnd_(buffer_), pEndOfMem_(buffer_) { buffer_[0] = E(0); }
  65. E* pEnd_;
  66. E* pEndOfMem_;
  67. E buffer_[1];
  68. };
  69. typedef typename A::size_type size_type;
  70. private:
  71. Data* pData_;
  72. void Init(size_type size, size_type capacity)
  73. {
  74. assert(size <= capacity);
  75. if (capacity == 0)
  76. {
  77. pData_ = NULL;
  78. }
  79. else
  80. {
  81. // 11-17-2000: comment added:
  82. // No need to allocate (capacity + 1) to
  83. // accommodate the terminating 0, because Data already
  84. // has one one character in there
  85. pData_ = static_cast<Data*>(
  86. malloc(sizeof(Data) + capacity * sizeof(E)));
  87. if (!pData_) assert(0);
  88. pData_->pEnd_ = pData_->buffer_ + size;
  89. pData_->pEndOfMem_ = pData_->buffer_ + capacity;
  90. }
  91. }
  92. private:
  93. // Warning - this doesn't initialize pData_. Used in reserve()
  94. MiniStringStorage() : pData_(NULL)
  95. { }
  96. public:
  97. typedef E value_type;
  98. typedef E* iterator;
  99. typedef const E* const_iterator;
  100. typedef A allocator_type;
  101. typedef typename A::reference reference;
  102. MiniStringStorage(const MiniStringStorage& rhs)
  103. {
  104. const size_type sz = rhs.size();
  105. Init(sz, sz);
  106. if (sz) flex_string_details::pod_copy(rhs.begin(), rhs.end(), begin());
  107. }
  108. MiniStringStorage(const MiniStringStorage& s,
  109. flex_string_details::Shallow)
  110. : pData_(s.pData_)
  111. {
  112. }
  113. MiniStringStorage(const A&)
  114. { pData_ = NULL; }
  115. MiniStringStorage(const E* s, size_type len, const A&)
  116. {
  117. Init(len, len);
  118. flex_string_details::pod_copy(s, s + len, begin());
  119. }
  120. MiniStringStorage(size_type len, E c, const A&)
  121. {
  122. Init(len, len);
  123. flex_string_details::pod_fill(begin(), end(), c);
  124. }
  125. MiniStringStorage& operator=(const MiniStringStorage& rhs)
  126. {
  127. const size_type sz = rhs.size();
  128. reserve(sz);
  129. flex_string_details::pod_copy(&*rhs.begin(), &*rhs.end(), begin());
  130. pData_->pEnd_ = &*begin() + sz;
  131. return *this;
  132. }
  133. ~MiniStringStorage()
  134. {
  135. assert(begin() <= end());
  136. free(pData_);
  137. }
  138. iterator begin()
  139. { return pData_ ? pData_->buffer_ : NULL; }
  140. const_iterator begin() const
  141. { return pData_ ? pData_->buffer_ : NULL; }
  142. iterator end()
  143. { return pData_ ? pData_->pEnd_ : NULL; }
  144. const_iterator end() const
  145. { return pData_ ? pData_->pEnd_ : NULL; }
  146. size_type size() const
  147. { return pData_ ? pData_->pEnd_ - pData_->buffer_ : 0; }
  148. size_type max_size() const
  149. { return size_t(-1) / sizeof(E) - sizeof(Data) - 1; }
  150. size_type capacity() const
  151. { return pData_ ? pData_->pEndOfMem_ - pData_->buffer_ : 0; }
  152. void reserve(size_type res_arg)
  153. {
  154. if (pData_ != NULL && res_arg <= capacity())
  155. {
  156. // @@@ insert shrinkage here if you wish
  157. return;
  158. }
  159. if (pData_ == NULL)
  160. {
  161. Init(0, res_arg);
  162. }
  163. else
  164. {
  165. const size_type sz = size();
  166. void* p = realloc(pData_,
  167. sizeof(Data) + res_arg * sizeof(E));
  168. if (!p) assert(0);
  169. if (p != pData_)
  170. {
  171. pData_ = static_cast<Data*>(p);
  172. pData_->pEnd_ = pData_->buffer_ + sz;
  173. }
  174. pData_->pEndOfMem_ = pData_->buffer_ + res_arg;
  175. }
  176. }
  177. template <class InputIterator>
  178. void append(InputIterator b, InputIterator e)
  179. {
  180. const size_type
  181. sz = std::distance(b, e),
  182. neededCapacity = size() + sz;
  183. if (capacity() < neededCapacity)
  184. {
  185. static std::less_equal<const E*> le;
  186. (void) le;
  187. assert(!(le(begin(), &*b) && le(&*b, end()))); // no aliasing
  188. reserve(neededCapacity);
  189. }
  190. std::copy(b, e, end());
  191. pData_->pEnd_ += sz;
  192. }
  193. void resize(size_type newSize, E fill)
  194. {
  195. const int delta = int(newSize - size());
  196. if (delta == 0) return;
  197. if (delta > 0)
  198. {
  199. if (newSize > capacity())
  200. {
  201. reserve(newSize);
  202. }
  203. E* e = &*end();
  204. flex_string_details::pod_fill(e, e + delta, fill);
  205. }
  206. pData_->pEnd_ = pData_->buffer_ + newSize;
  207. }
  208. void swap(MiniStringStorage& rhs)
  209. {
  210. std::swap(pData_, rhs.pData_);
  211. }
  212. const E* c_str() const
  213. {
  214. if (pData_ == NULL)
  215. {
  216. return "";
  217. }
  218. *pData_->pEnd_ = E();
  219. return pData_->buffer_;
  220. }
  221. const E* data() const
  222. { return pData_ ? pData_->buffer_ : ""; }
  223. A get_allocator() const
  224. { return A(); }
  225. };
  226. } // namespace Infra
  227. } // namespace Dahua
  228. #endif // DAHUA_MINI_STRING_STORAGE_INC_