details.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // Helper classes and functions for the circular buffer.
  2. // Copyright (c) 2003-2008 Jan Gaspar
  3. // Copyright (c) 2014 Glen Fernandes // C++11 allocator model support.
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP)
  8. #define BOOST_CIRCULAR_BUFFER_DETAILS_HPP
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/iterator.hpp>
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/container/allocator_traits.hpp>
  15. #include <boost/move/move.hpp>
  16. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  17. #include <boost/utility/addressof.hpp>
  18. #include <boost/detail/no_exceptions_support.hpp>
  19. #include <iterator>
  20. // Silence MS /W4 warnings like C4913:
  21. // "user defined binary operator ',' exists but no overload could convert all operands, default built-in binary operator ',' used"
  22. // This might happen when previously including some boost headers that overload the coma operator.
  23. #if defined(_MSC_VER)
  24. # pragma warning(push)
  25. # pragma warning(disable:4913)
  26. #endif
  27. namespace boost {
  28. namespace cb_details {
  29. template <class Traits> struct nonconst_traits;
  30. template<class ForwardIterator, class Diff, class T, class Alloc>
  31. void uninitialized_fill_n_with_alloc(
  32. ForwardIterator first, Diff n, const T& item, Alloc& alloc);
  33. template<class InputIterator, class ForwardIterator, class Alloc>
  34. ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a);
  35. template<class InputIterator, class ForwardIterator, class Alloc>
  36. ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a);
  37. /*!
  38. \struct const_traits
  39. \brief Defines the data types for a const iterator.
  40. */
  41. template <class Traits>
  42. struct const_traits {
  43. // Basic types
  44. typedef typename Traits::value_type value_type;
  45. typedef typename Traits::const_pointer pointer;
  46. typedef typename Traits::const_reference reference;
  47. typedef typename Traits::size_type size_type;
  48. typedef typename Traits::difference_type difference_type;
  49. // Non-const traits
  50. typedef nonconst_traits<Traits> nonconst_self;
  51. };
  52. /*!
  53. \struct nonconst_traits
  54. \brief Defines the data types for a non-const iterator.
  55. */
  56. template <class Traits>
  57. struct nonconst_traits {
  58. // Basic types
  59. typedef typename Traits::value_type value_type;
  60. typedef typename Traits::pointer pointer;
  61. typedef typename Traits::reference reference;
  62. typedef typename Traits::size_type size_type;
  63. typedef typename Traits::difference_type difference_type;
  64. // Non-const traits
  65. typedef nonconst_traits<Traits> nonconst_self;
  66. };
  67. /*!
  68. \struct iterator_wrapper
  69. \brief Helper iterator dereference wrapper.
  70. */
  71. template <class Iterator>
  72. struct iterator_wrapper {
  73. mutable Iterator m_it;
  74. explicit iterator_wrapper(Iterator it) : m_it(it) {}
  75. Iterator operator () () const { return m_it++; }
  76. private:
  77. iterator_wrapper<Iterator>& operator = (const iterator_wrapper<Iterator>&); // do not generate
  78. };
  79. /*!
  80. \struct item_wrapper
  81. \brief Helper item dereference wrapper.
  82. */
  83. template <class Pointer, class Value>
  84. struct item_wrapper {
  85. Value m_item;
  86. explicit item_wrapper(Value item) : m_item(item) {}
  87. Pointer operator () () const { return &m_item; }
  88. private:
  89. item_wrapper<Pointer, Value>& operator = (const item_wrapper<Pointer, Value>&); // do not generate
  90. };
  91. /*!
  92. \struct assign_n
  93. \brief Helper functor for assigning n items.
  94. */
  95. template <class Value, class Alloc>
  96. struct assign_n {
  97. typedef typename boost::container::allocator_traits<Alloc>::size_type size_type;
  98. size_type m_n;
  99. Value m_item;
  100. Alloc& m_alloc;
  101. assign_n(size_type n, Value item, Alloc& alloc) : m_n(n), m_item(item), m_alloc(alloc) {}
  102. template <class Pointer>
  103. void operator () (Pointer p) const {
  104. uninitialized_fill_n_with_alloc(p, m_n, m_item, m_alloc);
  105. }
  106. private:
  107. assign_n<Value, Alloc>& operator = (const assign_n<Value, Alloc>&); // do not generate
  108. };
  109. /*!
  110. \struct assign_range
  111. \brief Helper functor for assigning range of items.
  112. */
  113. template <class Iterator, class Alloc>
  114. struct assign_range {
  115. Iterator m_first;
  116. Iterator m_last;
  117. Alloc& m_alloc;
  118. assign_range(const Iterator& first, const Iterator& last, Alloc& alloc)
  119. : m_first(first), m_last(last), m_alloc(alloc) {}
  120. template <class Pointer>
  121. void operator () (Pointer p) const {
  122. boost::cb_details::uninitialized_copy(m_first, m_last, p, m_alloc);
  123. }
  124. };
  125. template <class Iterator, class Alloc>
  126. inline assign_range<Iterator, Alloc> make_assign_range(const Iterator& first, const Iterator& last, Alloc& a) {
  127. return assign_range<Iterator, Alloc>(first, last, a);
  128. }
  129. /*!
  130. \class capacity_control
  131. \brief Capacity controller of the space optimized circular buffer.
  132. */
  133. template <class Size>
  134. class capacity_control {
  135. //! The capacity of the space-optimized circular buffer.
  136. Size m_capacity;
  137. //! The lowest guaranteed or minimum capacity of the adapted space-optimized circular buffer.
  138. Size m_min_capacity;
  139. public:
  140. //! Constructor.
  141. capacity_control(Size buffer_capacity, Size min_buffer_capacity = 0)
  142. : m_capacity(buffer_capacity), m_min_capacity(min_buffer_capacity)
  143. { // Check for capacity lower than min_capacity.
  144. BOOST_CB_ASSERT(buffer_capacity >= min_buffer_capacity);
  145. }
  146. // Default copy constructor.
  147. // Default assign operator.
  148. //! Get the capacity of the space optimized circular buffer.
  149. Size capacity() const { return m_capacity; }
  150. //! Get the minimal capacity of the space optimized circular buffer.
  151. Size min_capacity() const { return m_min_capacity; }
  152. //! Size operator - returns the capacity of the space optimized circular buffer.
  153. operator Size() const { return m_capacity; }
  154. };
  155. /*!
  156. \struct iterator
  157. \brief Random access iterator for the circular buffer.
  158. \param Buff The type of the underlying circular buffer.
  159. \param Traits Basic iterator types.
  160. \note This iterator is not circular. It was designed
  161. for iterating from begin() to end() of the circular buffer.
  162. */
  163. template <class Buff, class Traits>
  164. struct iterator :
  165. public boost::iterator<
  166. std::random_access_iterator_tag,
  167. typename Traits::value_type,
  168. typename Traits::difference_type,
  169. typename Traits::pointer,
  170. typename Traits::reference>
  171. #if BOOST_CB_ENABLE_DEBUG
  172. , public debug_iterator_base
  173. #endif // #if BOOST_CB_ENABLE_DEBUG
  174. {
  175. // Helper types
  176. //! Base iterator.
  177. typedef boost::iterator<
  178. std::random_access_iterator_tag,
  179. typename Traits::value_type,
  180. typename Traits::difference_type,
  181. typename Traits::pointer,
  182. typename Traits::reference> base_iterator;
  183. //! Non-const iterator.
  184. typedef iterator<Buff, typename Traits::nonconst_self> nonconst_self;
  185. // Basic types
  186. //! The type of the elements stored in the circular buffer.
  187. typedef typename base_iterator::value_type value_type;
  188. //! Pointer to the element.
  189. typedef typename base_iterator::pointer pointer;
  190. //! Reference to the element.
  191. typedef typename base_iterator::reference reference;
  192. //! Size type.
  193. typedef typename Traits::size_type size_type;
  194. //! Difference type.
  195. typedef typename base_iterator::difference_type difference_type;
  196. // Member variables
  197. //! The circular buffer where the iterator points to.
  198. const Buff* m_buff;
  199. //! An internal iterator.
  200. pointer m_it;
  201. // Construction & assignment
  202. // Default copy constructor.
  203. //! Default constructor.
  204. iterator() : m_buff(0), m_it(0) {}
  205. #if BOOST_CB_ENABLE_DEBUG
  206. //! Copy constructor (used for converting from a non-const to a const iterator).
  207. iterator(const nonconst_self& it) : debug_iterator_base(it), m_buff(it.m_buff), m_it(it.m_it) {}
  208. //! Internal constructor.
  209. /*!
  210. \note This constructor is not intended to be used directly by the user.
  211. */
  212. iterator(const Buff* cb, const pointer p) : debug_iterator_base(cb), m_buff(cb), m_it(p) {}
  213. #else
  214. iterator(const nonconst_self& it) : m_buff(it.m_buff), m_it(it.m_it) {}
  215. iterator(const Buff* cb, const pointer p) : m_buff(cb), m_it(p) {}
  216. #endif // #if BOOST_CB_ENABLE_DEBUG
  217. //! Assign operator.
  218. iterator& operator = (const iterator& it) {
  219. if (this == &it)
  220. return *this;
  221. #if BOOST_CB_ENABLE_DEBUG
  222. debug_iterator_base::operator =(it);
  223. #endif // #if BOOST_CB_ENABLE_DEBUG
  224. m_buff = it.m_buff;
  225. m_it = it.m_it;
  226. return *this;
  227. }
  228. // Random access iterator methods
  229. //! Dereferencing operator.
  230. reference operator * () const {
  231. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  232. BOOST_CB_ASSERT(m_it != 0); // check for iterator pointing to end()
  233. return *m_it;
  234. }
  235. //! Dereferencing operator.
  236. pointer operator -> () const { return &(operator*()); }
  237. //! Difference operator.
  238. template <class Traits0>
  239. difference_type operator - (const iterator<Buff, Traits0>& it) const {
  240. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  241. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  242. return linearize_pointer(*this) - linearize_pointer(it);
  243. }
  244. //! Increment operator (prefix).
  245. iterator& operator ++ () {
  246. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  247. BOOST_CB_ASSERT(m_it != 0); // check for iterator pointing to end()
  248. m_buff->increment(m_it);
  249. if (m_it == m_buff->m_last)
  250. m_it = 0;
  251. return *this;
  252. }
  253. //! Increment operator (postfix).
  254. iterator operator ++ (int) {
  255. iterator<Buff, Traits> tmp = *this;
  256. ++*this;
  257. return tmp;
  258. }
  259. //! Decrement operator (prefix).
  260. iterator& operator -- () {
  261. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  262. BOOST_CB_ASSERT(m_it != m_buff->m_first); // check for iterator pointing to begin()
  263. if (m_it == 0)
  264. m_it = m_buff->m_last;
  265. m_buff->decrement(m_it);
  266. return *this;
  267. }
  268. //! Decrement operator (postfix).
  269. iterator operator -- (int) {
  270. iterator<Buff, Traits> tmp = *this;
  271. --*this;
  272. return tmp;
  273. }
  274. //! Iterator addition.
  275. iterator& operator += (difference_type n) {
  276. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  277. if (n > 0) {
  278. BOOST_CB_ASSERT(m_buff->end() - *this >= n); // check for too large n
  279. m_it = m_buff->add(m_it, n);
  280. if (m_it == m_buff->m_last)
  281. m_it = 0;
  282. } else if (n < 0) {
  283. *this -= -n;
  284. }
  285. return *this;
  286. }
  287. //! Iterator addition.
  288. iterator operator + (difference_type n) const { return iterator<Buff, Traits>(*this) += n; }
  289. //! Iterator subtraction.
  290. iterator& operator -= (difference_type n) {
  291. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  292. if (n > 0) {
  293. BOOST_CB_ASSERT(*this - m_buff->begin() >= n); // check for too large n
  294. m_it = m_buff->sub(m_it == 0 ? m_buff->m_last : m_it, n);
  295. } else if (n < 0) {
  296. *this += -n;
  297. }
  298. return *this;
  299. }
  300. //! Iterator subtraction.
  301. iterator operator - (difference_type n) const { return iterator<Buff, Traits>(*this) -= n; }
  302. //! Element access operator.
  303. reference operator [] (difference_type n) const { return *(*this + n); }
  304. // Equality & comparison
  305. //! Equality.
  306. template <class Traits0>
  307. bool operator == (const iterator<Buff, Traits0>& it) const {
  308. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  309. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  310. return m_it == it.m_it;
  311. }
  312. //! Inequality.
  313. template <class Traits0>
  314. bool operator != (const iterator<Buff, Traits0>& it) const {
  315. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  316. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  317. return m_it != it.m_it;
  318. }
  319. //! Less.
  320. template <class Traits0>
  321. bool operator < (const iterator<Buff, Traits0>& it) const {
  322. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  323. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  324. return linearize_pointer(*this) < linearize_pointer(it);
  325. }
  326. //! Greater.
  327. template <class Traits0>
  328. bool operator > (const iterator<Buff, Traits0>& it) const { return it < *this; }
  329. //! Less or equal.
  330. template <class Traits0>
  331. bool operator <= (const iterator<Buff, Traits0>& it) const { return !(it < *this); }
  332. //! Greater or equal.
  333. template <class Traits0>
  334. bool operator >= (const iterator<Buff, Traits0>& it) const { return !(*this < it); }
  335. // Helpers
  336. //! Get a pointer which would point to the same element as the iterator in case the circular buffer is linearized.
  337. template <class Traits0>
  338. typename Traits0::pointer linearize_pointer(const iterator<Buff, Traits0>& it) const {
  339. return it.m_it == 0 ? m_buff->m_buff + m_buff->size() :
  340. (it.m_it < m_buff->m_first ? it.m_it + (m_buff->m_end - m_buff->m_first)
  341. : m_buff->m_buff + (it.m_it - m_buff->m_first));
  342. }
  343. };
  344. //! Iterator addition.
  345. template <class Buff, class Traits>
  346. inline iterator<Buff, Traits>
  347. operator + (typename Traits::difference_type n, const iterator<Buff, Traits>& it) {
  348. return it + n;
  349. }
  350. /*!
  351. \fn ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest)
  352. \brief Equivalent of <code>std::uninitialized_copy</code> but with explicit specification of value type.
  353. */
  354. template<class InputIterator, class ForwardIterator, class Alloc>
  355. inline ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) {
  356. ForwardIterator next = dest;
  357. BOOST_TRY {
  358. for (; first != last; ++first, ++dest)
  359. boost::container::allocator_traits<Alloc>::construct(a, boost::addressof(*dest), *first);
  360. } BOOST_CATCH(...) {
  361. for (; next != dest; ++next)
  362. boost::container::allocator_traits<Alloc>::destroy(a, boost::addressof(*next));
  363. BOOST_RETHROW
  364. }
  365. BOOST_CATCH_END
  366. return dest;
  367. }
  368. template<class InputIterator, class ForwardIterator, class Alloc>
  369. ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a,
  370. true_type) {
  371. for (; first != last; ++first, ++dest)
  372. boost::container::allocator_traits<Alloc>::construct(a, boost::addressof(*dest), boost::move(*first));
  373. return dest;
  374. }
  375. template<class InputIterator, class ForwardIterator, class Alloc>
  376. ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a,
  377. false_type) {
  378. return uninitialized_copy(first, last, dest, a);
  379. }
  380. /*!
  381. \fn ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest)
  382. \brief Equivalent of <code>std::uninitialized_copy</code> but with explicit specification of value type and moves elements if they have noexcept move constructors.
  383. */
  384. template<class InputIterator, class ForwardIterator, class Alloc>
  385. ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) {
  386. typedef typename boost::is_nothrow_move_constructible<typename boost::container::allocator_traits<Alloc>::value_type>::type tag_t;
  387. return uninitialized_move_if_noexcept_impl(first, last, dest, a, tag_t());
  388. }
  389. /*!
  390. \fn void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc)
  391. \brief Equivalent of <code>std::uninitialized_fill_n</code> with allocator.
  392. */
  393. template<class ForwardIterator, class Diff, class T, class Alloc>
  394. inline void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc) {
  395. ForwardIterator next = first;
  396. BOOST_TRY {
  397. for (; n > 0; ++first, --n)
  398. boost::container::allocator_traits<Alloc>::construct(alloc, boost::addressof(*first), item);
  399. } BOOST_CATCH(...) {
  400. for (; next != first; ++next)
  401. boost::container::allocator_traits<Alloc>::destroy(alloc, boost::addressof(*next));
  402. BOOST_RETHROW
  403. }
  404. BOOST_CATCH_END
  405. }
  406. } // namespace cb_details
  407. } // namespace boost
  408. #if defined(_MSC_VER)
  409. # pragma warning(pop)
  410. #endif
  411. #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP)