details.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Helper classes and functions for the circular buffer.
  2. // Copyright (c) 2003-2008 Jan Gaspar
  3. // Copyright 2014,2018 Glen Joseph Fernandes
  4. // (glenjofe@gmail.com)
  5. // Use, modification, and distribution is subject to the Boost Software
  6. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP)
  9. #define BOOST_CIRCULAR_BUFFER_DETAILS_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/circular_buffer/allocators.hpp>
  15. #include <boost/core/pointer_traits.hpp>
  16. #include <boost/move/move.hpp>
  17. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  18. #include <boost/core/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 const value_type& 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 value_type& 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 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. #if BOOST_CB_ENABLE_DEBUG
  166. : public debug_iterator_base
  167. #endif // #if BOOST_CB_ENABLE_DEBUG
  168. {
  169. // Helper types
  170. //! Non-const iterator.
  171. typedef iterator<Buff, typename Traits::nonconst_self> nonconst_self;
  172. // Basic types
  173. typedef std::random_access_iterator_tag iterator_category;
  174. //! The type of the elements stored in the circular buffer.
  175. typedef typename Traits::value_type value_type;
  176. //! Pointer to the element.
  177. typedef typename Traits::pointer pointer;
  178. //! Reference to the element.
  179. typedef typename Traits::reference reference;
  180. //! Size type.
  181. typedef typename Traits::size_type size_type;
  182. //! Difference type.
  183. typedef typename Traits::difference_type difference_type;
  184. // Member variables
  185. //! The circular buffer where the iterator points to.
  186. const Buff* m_buff;
  187. //! An internal iterator.
  188. pointer m_it;
  189. // Construction & assignment
  190. // Default copy constructor.
  191. //! Default constructor.
  192. iterator() : m_buff(0), m_it(0) {}
  193. #if BOOST_CB_ENABLE_DEBUG
  194. //! Copy constructor (used for converting from a non-const to a const iterator).
  195. iterator(const nonconst_self& it) : debug_iterator_base(it), m_buff(it.m_buff), m_it(it.m_it) {}
  196. //! Internal constructor.
  197. /*!
  198. \note This constructor is not intended to be used directly by the user.
  199. */
  200. iterator(const Buff* cb, const pointer p) : debug_iterator_base(cb), m_buff(cb), m_it(p) {}
  201. #else
  202. iterator(const nonconst_self& it) : m_buff(it.m_buff), m_it(it.m_it) {}
  203. iterator(const Buff* cb, const pointer p) : m_buff(cb), m_it(p) {}
  204. #endif // #if BOOST_CB_ENABLE_DEBUG
  205. //! Assign operator.
  206. iterator& operator = (const iterator& it) {
  207. if (this == &it)
  208. return *this;
  209. #if BOOST_CB_ENABLE_DEBUG
  210. debug_iterator_base::operator =(it);
  211. #endif // #if BOOST_CB_ENABLE_DEBUG
  212. m_buff = it.m_buff;
  213. m_it = it.m_it;
  214. return *this;
  215. }
  216. // Random access iterator methods
  217. //! Dereferencing operator.
  218. reference operator * () const {
  219. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  220. BOOST_CB_ASSERT(m_it != 0); // check for iterator pointing to end()
  221. return *m_it;
  222. }
  223. //! Dereferencing operator.
  224. pointer operator -> () const { return &(operator*()); }
  225. //! Difference operator.
  226. template <class Traits0>
  227. difference_type operator - (const iterator<Buff, Traits0>& it) const {
  228. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  229. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  230. return linearize_pointer(*this) - linearize_pointer(it);
  231. }
  232. //! Increment operator (prefix).
  233. iterator& operator ++ () {
  234. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  235. BOOST_CB_ASSERT(m_it != 0); // check for iterator pointing to end()
  236. m_buff->increment(m_it);
  237. if (m_it == m_buff->m_last)
  238. m_it = 0;
  239. return *this;
  240. }
  241. //! Increment operator (postfix).
  242. iterator operator ++ (int) {
  243. iterator<Buff, Traits> tmp = *this;
  244. ++*this;
  245. return tmp;
  246. }
  247. //! Decrement operator (prefix).
  248. iterator& operator -- () {
  249. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  250. BOOST_CB_ASSERT(m_it != m_buff->m_first); // check for iterator pointing to begin()
  251. if (m_it == 0)
  252. m_it = m_buff->m_last;
  253. m_buff->decrement(m_it);
  254. return *this;
  255. }
  256. //! Decrement operator (postfix).
  257. iterator operator -- (int) {
  258. iterator<Buff, Traits> tmp = *this;
  259. --*this;
  260. return tmp;
  261. }
  262. //! Iterator addition.
  263. iterator& operator += (difference_type n) {
  264. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  265. if (n > 0) {
  266. BOOST_CB_ASSERT(m_buff->end() - *this >= n); // check for too large n
  267. m_it = m_buff->add(m_it, n);
  268. if (m_it == m_buff->m_last)
  269. m_it = 0;
  270. } else if (n < 0) {
  271. *this -= -n;
  272. }
  273. return *this;
  274. }
  275. //! Iterator addition.
  276. iterator operator + (difference_type n) const { return iterator<Buff, Traits>(*this) += n; }
  277. //! Iterator subtraction.
  278. iterator& operator -= (difference_type n) {
  279. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  280. if (n > 0) {
  281. BOOST_CB_ASSERT(*this - m_buff->begin() >= n); // check for too large n
  282. m_it = m_buff->sub(m_it == 0 ? m_buff->m_last : m_it, n);
  283. } else if (n < 0) {
  284. *this += -n;
  285. }
  286. return *this;
  287. }
  288. //! Iterator subtraction.
  289. iterator operator - (difference_type n) const { return iterator<Buff, Traits>(*this) -= n; }
  290. //! Element access operator.
  291. reference operator [] (difference_type n) const { return *(*this + n); }
  292. // Equality & comparison
  293. //! Equality.
  294. template <class Traits0>
  295. bool operator == (const iterator<Buff, Traits0>& it) const {
  296. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  297. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  298. return m_it == it.m_it;
  299. }
  300. //! Inequality.
  301. template <class Traits0>
  302. bool operator != (const iterator<Buff, Traits0>& it) const {
  303. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  304. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  305. return m_it != it.m_it;
  306. }
  307. //! Less.
  308. template <class Traits0>
  309. bool operator < (const iterator<Buff, Traits0>& it) const {
  310. BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator
  311. BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator
  312. return linearize_pointer(*this) < linearize_pointer(it);
  313. }
  314. //! Greater.
  315. template <class Traits0>
  316. bool operator > (const iterator<Buff, Traits0>& it) const { return it < *this; }
  317. //! Less or equal.
  318. template <class Traits0>
  319. bool operator <= (const iterator<Buff, Traits0>& it) const { return !(it < *this); }
  320. //! Greater or equal.
  321. template <class Traits0>
  322. bool operator >= (const iterator<Buff, Traits0>& it) const { return !(*this < it); }
  323. // Helpers
  324. //! Get a pointer which would point to the same element as the iterator in case the circular buffer is linearized.
  325. template <class Traits0>
  326. typename Traits0::pointer linearize_pointer(const iterator<Buff, Traits0>& it) const {
  327. return it.m_it == 0 ? m_buff->m_buff + m_buff->size() :
  328. (it.m_it < m_buff->m_first ? it.m_it + (m_buff->m_end - m_buff->m_first)
  329. : m_buff->m_buff + (it.m_it - m_buff->m_first));
  330. }
  331. };
  332. //! Iterator addition.
  333. template <class Buff, class Traits>
  334. inline iterator<Buff, Traits>
  335. operator + (typename Traits::difference_type n, const iterator<Buff, Traits>& it) {
  336. return it + n;
  337. }
  338. /*!
  339. \fn ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest)
  340. \brief Equivalent of <code>std::uninitialized_copy</code> but with explicit specification of value type.
  341. */
  342. template<class InputIterator, class ForwardIterator, class Alloc>
  343. inline ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) {
  344. ForwardIterator next = dest;
  345. BOOST_TRY {
  346. for (; first != last; ++first, ++dest)
  347. allocator_traits<Alloc>::construct(a, boost::to_address(dest), *first);
  348. } BOOST_CATCH(...) {
  349. for (; next != dest; ++next)
  350. allocator_traits<Alloc>::destroy(a, boost::to_address(next));
  351. BOOST_RETHROW
  352. }
  353. BOOST_CATCH_END
  354. return dest;
  355. }
  356. template<class InputIterator, class ForwardIterator, class Alloc>
  357. ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a,
  358. true_type) {
  359. for (; first != last; ++first, ++dest)
  360. allocator_traits<Alloc>::construct(a, boost::to_address(dest), boost::move(*first));
  361. return dest;
  362. }
  363. template<class InputIterator, class ForwardIterator, class Alloc>
  364. ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a,
  365. false_type) {
  366. return uninitialized_copy(first, last, dest, a);
  367. }
  368. /*!
  369. \fn ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest)
  370. \brief Equivalent of <code>std::uninitialized_copy</code> but with explicit specification of value type and moves elements if they have noexcept move constructors.
  371. */
  372. template<class InputIterator, class ForwardIterator, class Alloc>
  373. ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) {
  374. typedef typename boost::is_nothrow_move_constructible<typename allocator_traits<Alloc>::value_type>::type tag_t;
  375. return uninitialized_move_if_noexcept_impl(first, last, dest, a, tag_t());
  376. }
  377. /*!
  378. \fn void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc)
  379. \brief Equivalent of <code>std::uninitialized_fill_n</code> with allocator.
  380. */
  381. template<class ForwardIterator, class Diff, class T, class Alloc>
  382. inline void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc) {
  383. ForwardIterator next = first;
  384. BOOST_TRY {
  385. for (; n > 0; ++first, --n)
  386. allocator_traits<Alloc>::construct(alloc, boost::to_address(first), item);
  387. } BOOST_CATCH(...) {
  388. for (; next != first; ++next)
  389. allocator_traits<Alloc>::destroy(alloc, boost::to_address(next));
  390. BOOST_RETHROW
  391. }
  392. BOOST_CATCH_END
  393. }
  394. } // namespace cb_details
  395. } // namespace boost
  396. #if defined(_MSC_VER)
  397. # pragma warning(pop)
  398. #endif
  399. #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP)