simplestringstorage.h 7.5 KB

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