details.hpp 17 KB

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