123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP)
- #define BOOST_CIRCULAR_BUFFER_DETAILS_HPP
- #if defined(_MSC_VER)
- #pragma once
- #endif
- #include <boost/iterator.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/container/allocator_traits.hpp>
- #include <boost/move/move.hpp>
- #include <boost/type_traits/is_nothrow_move_constructible.hpp>
- #include <boost/utility/addressof.hpp>
- #include <boost/detail/no_exceptions_support.hpp>
- #include <iterator>
- #if defined(_MSC_VER)
- # pragma warning(push)
- # pragma warning(disable:4913)
- #endif
- namespace boost {
- namespace cb_details {
- template <class Traits> struct nonconst_traits;
- template<class ForwardIterator, class Diff, class T, class Alloc>
- void uninitialized_fill_n_with_alloc(
- ForwardIterator first, Diff n, const T& item, Alloc& alloc);
- template<class InputIterator, class ForwardIterator, class Alloc>
- ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a);
- template<class InputIterator, class ForwardIterator, class Alloc>
- ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a);
- template <class Traits>
- struct const_traits {
-
- typedef typename Traits::value_type value_type;
- typedef typename Traits::const_pointer pointer;
- typedef typename Traits::const_reference reference;
- typedef typename Traits::size_type size_type;
- typedef typename Traits::difference_type difference_type;
-
- typedef nonconst_traits<Traits> nonconst_self;
- };
- template <class Traits>
- struct nonconst_traits {
-
- typedef typename Traits::value_type value_type;
- typedef typename Traits::pointer pointer;
- typedef typename Traits::reference reference;
- typedef typename Traits::size_type size_type;
- typedef typename Traits::difference_type difference_type;
-
- typedef nonconst_traits<Traits> nonconst_self;
- };
- template <class Iterator>
- struct iterator_wrapper {
- mutable Iterator m_it;
- explicit iterator_wrapper(Iterator it) : m_it(it) {}
- Iterator operator () () const { return m_it++; }
- private:
- iterator_wrapper<Iterator>& operator = (const iterator_wrapper<Iterator>&);
- };
- template <class Pointer, class Value>
- struct item_wrapper {
- Value m_item;
- explicit item_wrapper(Value item) : m_item(item) {}
- Pointer operator () () const { return &m_item; }
- private:
- item_wrapper<Pointer, Value>& operator = (const item_wrapper<Pointer, Value>&);
- };
- template <class Value, class Alloc>
- struct assign_n {
- typedef typename boost::container::allocator_traits<Alloc>::size_type size_type;
- size_type m_n;
- Value m_item;
- Alloc& m_alloc;
- assign_n(size_type n, Value item, Alloc& alloc) : m_n(n), m_item(item), m_alloc(alloc) {}
- template <class Pointer>
- void operator () (Pointer p) const {
- uninitialized_fill_n_with_alloc(p, m_n, m_item, m_alloc);
- }
- private:
- assign_n<Value, Alloc>& operator = (const assign_n<Value, Alloc>&);
- };
- template <class Iterator, class Alloc>
- struct assign_range {
- Iterator m_first;
- Iterator m_last;
- Alloc& m_alloc;
- assign_range(const Iterator& first, const Iterator& last, Alloc& alloc)
- : m_first(first), m_last(last), m_alloc(alloc) {}
- template <class Pointer>
- void operator () (Pointer p) const {
- boost::cb_details::uninitialized_copy(m_first, m_last, p, m_alloc);
- }
- };
- template <class Iterator, class Alloc>
- inline assign_range<Iterator, Alloc> make_assign_range(const Iterator& first, const Iterator& last, Alloc& a) {
- return assign_range<Iterator, Alloc>(first, last, a);
- }
- template <class Size>
- class capacity_control {
-
- Size m_capacity;
-
- Size m_min_capacity;
- public:
-
- capacity_control(Size buffer_capacity, Size min_buffer_capacity = 0)
- : m_capacity(buffer_capacity), m_min_capacity(min_buffer_capacity)
- {
- BOOST_CB_ASSERT(buffer_capacity >= min_buffer_capacity);
- }
-
-
-
- Size capacity() const { return m_capacity; }
-
- Size min_capacity() const { return m_min_capacity; }
-
- operator Size() const { return m_capacity; }
- };
- template <class Buff, class Traits>
- struct iterator :
- public boost::iterator<
- std::random_access_iterator_tag,
- typename Traits::value_type,
- typename Traits::difference_type,
- typename Traits::pointer,
- typename Traits::reference>
- #if BOOST_CB_ENABLE_DEBUG
- , public debug_iterator_base
- #endif
- {
-
- typedef boost::iterator<
- std::random_access_iterator_tag,
- typename Traits::value_type,
- typename Traits::difference_type,
- typename Traits::pointer,
- typename Traits::reference> base_iterator;
-
- typedef iterator<Buff, typename Traits::nonconst_self> nonconst_self;
-
- typedef typename base_iterator::value_type value_type;
-
- typedef typename base_iterator::pointer pointer;
-
- typedef typename base_iterator::reference reference;
-
- typedef typename Traits::size_type size_type;
-
- typedef typename base_iterator::difference_type difference_type;
-
- const Buff* m_buff;
-
- pointer m_it;
-
-
- iterator() : m_buff(0), m_it(0) {}
- #if BOOST_CB_ENABLE_DEBUG
-
- iterator(const nonconst_self& it) : debug_iterator_base(it), m_buff(it.m_buff), m_it(it.m_it) {}
-
-
- iterator(const Buff* cb, const pointer p) : debug_iterator_base(cb), m_buff(cb), m_it(p) {}
- #else
- iterator(const nonconst_self& it) : m_buff(it.m_buff), m_it(it.m_it) {}
- iterator(const Buff* cb, const pointer p) : m_buff(cb), m_it(p) {}
- #endif
-
- iterator& operator = (const iterator& it) {
- if (this == &it)
- return *this;
- #if BOOST_CB_ENABLE_DEBUG
- debug_iterator_base::operator =(it);
- #endif
- m_buff = it.m_buff;
- m_it = it.m_it;
- return *this;
- }
-
- reference operator * () const {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(m_it != 0);
- return *m_it;
- }
-
- pointer operator -> () const { return &(operator*()); }
-
- template <class Traits0>
- difference_type operator - (const iterator<Buff, Traits0>& it) const {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(it.is_valid(m_buff));
- return linearize_pointer(*this) - linearize_pointer(it);
- }
-
- iterator& operator ++ () {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(m_it != 0);
- m_buff->increment(m_it);
- if (m_it == m_buff->m_last)
- m_it = 0;
- return *this;
- }
-
- iterator operator ++ (int) {
- iterator<Buff, Traits> tmp = *this;
- ++*this;
- return tmp;
- }
-
- iterator& operator -- () {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(m_it != m_buff->m_first);
- if (m_it == 0)
- m_it = m_buff->m_last;
- m_buff->decrement(m_it);
- return *this;
- }
-
- iterator operator -- (int) {
- iterator<Buff, Traits> tmp = *this;
- --*this;
- return tmp;
- }
-
- iterator& operator += (difference_type n) {
- BOOST_CB_ASSERT(is_valid(m_buff));
- if (n > 0) {
- BOOST_CB_ASSERT(m_buff->end() - *this >= n);
- m_it = m_buff->add(m_it, n);
- if (m_it == m_buff->m_last)
- m_it = 0;
- } else if (n < 0) {
- *this -= -n;
- }
- return *this;
- }
-
- iterator operator + (difference_type n) const { return iterator<Buff, Traits>(*this) += n; }
-
- iterator& operator -= (difference_type n) {
- BOOST_CB_ASSERT(is_valid(m_buff));
- if (n > 0) {
- BOOST_CB_ASSERT(*this - m_buff->begin() >= n);
- m_it = m_buff->sub(m_it == 0 ? m_buff->m_last : m_it, n);
- } else if (n < 0) {
- *this += -n;
- }
- return *this;
- }
-
- iterator operator - (difference_type n) const { return iterator<Buff, Traits>(*this) -= n; }
-
- reference operator [] (difference_type n) const { return *(*this + n); }
-
- template <class Traits0>
- bool operator == (const iterator<Buff, Traits0>& it) const {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(it.is_valid(m_buff));
- return m_it == it.m_it;
- }
-
- template <class Traits0>
- bool operator != (const iterator<Buff, Traits0>& it) const {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(it.is_valid(m_buff));
- return m_it != it.m_it;
- }
-
- template <class Traits0>
- bool operator < (const iterator<Buff, Traits0>& it) const {
- BOOST_CB_ASSERT(is_valid(m_buff));
- BOOST_CB_ASSERT(it.is_valid(m_buff));
- return linearize_pointer(*this) < linearize_pointer(it);
- }
-
- template <class Traits0>
- bool operator > (const iterator<Buff, Traits0>& it) const { return it < *this; }
-
- template <class Traits0>
- bool operator <= (const iterator<Buff, Traits0>& it) const { return !(it < *this); }
-
- template <class Traits0>
- bool operator >= (const iterator<Buff, Traits0>& it) const { return !(*this < it); }
-
- template <class Traits0>
- typename Traits0::pointer linearize_pointer(const iterator<Buff, Traits0>& it) const {
- return it.m_it == 0 ? m_buff->m_buff + m_buff->size() :
- (it.m_it < m_buff->m_first ? it.m_it + (m_buff->m_end - m_buff->m_first)
- : m_buff->m_buff + (it.m_it - m_buff->m_first));
- }
- };
- template <class Buff, class Traits>
- inline iterator<Buff, Traits>
- operator + (typename Traits::difference_type n, const iterator<Buff, Traits>& it) {
- return it + n;
- }
- template<class InputIterator, class ForwardIterator, class Alloc>
- inline ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) {
- ForwardIterator next = dest;
- BOOST_TRY {
- for (; first != last; ++first, ++dest)
- boost::container::allocator_traits<Alloc>::construct(a, boost::addressof(*dest), *first);
- } BOOST_CATCH(...) {
- for (; next != dest; ++next)
- boost::container::allocator_traits<Alloc>::destroy(a, boost::addressof(*next));
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- return dest;
- }
- template<class InputIterator, class ForwardIterator, class Alloc>
- ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a,
- true_type) {
- for (; first != last; ++first, ++dest)
- boost::container::allocator_traits<Alloc>::construct(a, boost::addressof(*dest), boost::move(*first));
- return dest;
- }
- template<class InputIterator, class ForwardIterator, class Alloc>
- ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a,
- false_type) {
- return uninitialized_copy(first, last, dest, a);
- }
- template<class InputIterator, class ForwardIterator, class Alloc>
- ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) {
- typedef typename boost::is_nothrow_move_constructible<typename boost::container::allocator_traits<Alloc>::value_type>::type tag_t;
- return uninitialized_move_if_noexcept_impl(first, last, dest, a, tag_t());
- }
- template<class ForwardIterator, class Diff, class T, class Alloc>
- inline void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc) {
- ForwardIterator next = first;
- BOOST_TRY {
- for (; n > 0; ++first, --n)
- boost::container::allocator_traits<Alloc>::construct(alloc, boost::addressof(*first), item);
- } BOOST_CATCH(...) {
- for (; next != first; ++next)
- boost::container::allocator_traits<Alloc>::destroy(alloc, boost::addressof(*next));
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- }
- }
- }
- #if defined(_MSC_VER)
- # pragma warning(pop)
- #endif
- #endif
|