vectorstringstorage.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_VECTOR_STRING_STORAGE_INC_
  13. #define DAHUA_VECTOR_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. void append(const E* s, size_type sz);
  39. template <class InputIterator>
  40. void append(InputIterator b, InputIterator e);
  41. void resize(size_type newSize, E fill);
  42. void swap(StoragePolicy& rhs);
  43. const E* c_str() const;
  44. const E* data() const;
  45. A get_allocator() const;
  46. };
  47. ////////////////////////////////////////////////////////////////////////////////
  48. */
  49. #include <memory>
  50. #include <vector>
  51. #include <algorithm>
  52. #include <functional>
  53. #include <cassert>
  54. #include <limits>
  55. #include <stdexcept>
  56. namespace Dahua {
  57. namespace Infra {
  58. ////////////////////////////////////////////////////////////////////////////////
  59. // class template VectorStringStorage
  60. // Uses std::vector
  61. // Takes advantage of the Empty Base Optimization if available
  62. ////////////////////////////////////////////////////////////////////////////////
  63. template <typename E, class A = std::allocator<E> >
  64. class VectorStringStorage : protected std::vector<E, A>
  65. {
  66. typedef std::vector<E, A> base;
  67. public: // protected:
  68. typedef E value_type;
  69. typedef typename base::iterator iterator;
  70. typedef typename base::const_iterator const_iterator;
  71. typedef A allocator_type;
  72. typedef typename A::size_type size_type;
  73. typedef typename A::reference reference;
  74. VectorStringStorage(const VectorStringStorage& s) : base(s)
  75. { }
  76. VectorStringStorage(const A& a) : base(1, value_type(), a)
  77. { }
  78. VectorStringStorage(const value_type* s, size_type len, const A& a)
  79. : base(a)
  80. {
  81. base::reserve(len + 1);
  82. base::insert(base::end(), s, s + len);
  83. // Terminating zero
  84. base::push_back(value_type());
  85. }
  86. VectorStringStorage(size_type len, E c, const A& a)
  87. : base(len + 1, c, a)
  88. {
  89. // Terminating zero
  90. base::back() = value_type();
  91. }
  92. VectorStringStorage& operator=(const VectorStringStorage& rhs)
  93. {
  94. base& v = *this;
  95. v = rhs;
  96. return *this;
  97. }
  98. iterator begin()
  99. { return base::begin(); }
  100. const_iterator begin() const
  101. { return base::begin(); }
  102. iterator end()
  103. { return base::end() - 1; }
  104. const_iterator end() const
  105. { return base::end() - 1; }
  106. size_type size() const
  107. { return base::size() - 1; }
  108. size_type max_size() const
  109. { return base::max_size() - 1; }
  110. size_type capacity() const
  111. { return base::capacity() - 1; }
  112. void reserve(size_type res_arg)
  113. {
  114. assert(res_arg < max_size());
  115. base::reserve(res_arg + 1);
  116. }
  117. template <class ForwardIterator>
  118. void append(ForwardIterator b, ForwardIterator e)
  119. {
  120. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  121. typedef typename std::iterator_traits<ForwardIterator>::distance_type diff_t;
  122. #else
  123. typedef typename std::iterator_traits<ForwardIterator>::difference_type diff_t;
  124. #endif
  125. const diff_t sz = std::distance(b, e);
  126. assert(sz >= 0);
  127. if (sz == 0) return;
  128. base::reserve(base::size() + sz);
  129. const value_type & v = *b;
  130. struct OnBlockExit
  131. {
  132. VectorStringStorage * that;
  133. ~OnBlockExit()
  134. {
  135. that->base::push_back(value_type());
  136. }
  137. } onBlockExit = { this };
  138. (void) onBlockExit;
  139. assert(!base::empty());
  140. assert(base::back() == value_type());
  141. base::back() = v;
  142. base::insert(base::end(), ++b, e);
  143. }
  144. void resize(size_type n, E c)
  145. {
  146. base::reserve(n + 1);
  147. base::back() = c;
  148. base::resize(n + 1, c);
  149. base::back() = E();
  150. }
  151. void swap(VectorStringStorage& rhs)
  152. { base::swap(rhs); }
  153. const E* c_str() const
  154. { return &*begin(); }
  155. const E* data() const
  156. { return &*begin(); }
  157. A get_allocator() const
  158. { return base::get_allocator(); }
  159. };
  160. } // namespace Infra
  161. } // namespace Dahua
  162. #endif // DAHUA_VECTOR_STRING_STORAGE_INC_