Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 139 additions & 1 deletion include/boost/circular_buffer/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ private empty_value<Alloc>
boost::allocator_construct(alloc(), boost::to_address(m_last), static_cast<ValT>(item));
increment(m_last);
++m_size;
}
}
}

/*! INTERNAL ONLY */
Expand All @@ -1454,6 +1454,86 @@ private empty_value<Alloc>
BOOST_CATCH_END
}

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_back_impl(BOOST_FWD_REF(Args) ...args) {
if (full()) {
if (empty())
return;
destroy_item(m_last);
boost::allocator_construct(alloc(), boost::to_address(m_last), ::boost::forward<Args>(args)...);
increment(m_last);
m_first = m_last;
} else {
boost::allocator_construct(alloc(), boost::to_address(m_last), ::boost::forward<Args>(args)...);
increment(m_last);
++m_size;
}
}
#else
template <class V>
void emplace_back_impl(BOOST_FWD_REF(V) value) {
if (full()) {
if (empty())
return;
destroy_item(m_last);
boost::allocator_construct(alloc(), boost::to_address(m_last), ::boost::forward<Args>(args)...);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been compiled with BOOST_NO_CXX11_VARIADIC_TEMPLATES defined because I do not think this would compile.

There is no variadic template parameter Args (args) here

increment(m_last);
m_first = m_last;
} else {
boost::allocator_construct(alloc(), boost::to_address(m_last), ::boost::forward<V>(value));
increment(m_last);
++m_size;
}
}
#endif

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_front_impl(BOOST_FWD_REF(Args) ...args) {
BOOST_TRY {
if (full()) {
if (empty())
return;
decrement(m_first);
destroy_item(m_first);
boost::allocator_construct(alloc(), boost::to_address(m_first), ::boost::forward<Args>(args)...);
m_last = m_first;
} else {
decrement(m_first);
boost::allocator_construct(alloc(), boost::to_address(m_first), ::boost::forward<Args>(args)...);
++m_size;
}
} BOOST_CATCH(...) {
increment(m_first);
BOOST_RETHROW
}
BOOST_CATCH_END
}
#else
template <class V>
void emplace_front_impl(BOOST_FWD_REF(V) value) {
BOOST_TRY {
if (full()) {
if (empty())
return;
decrement(m_first);
destroy_item(m_first);
boost::allocator_construct(alloc(), boost::to_address(m_first), ::boost::forward<Args>(args)...);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been compiled with BOOST_NO_CXX11_VARIADIC_TEMPLATES defined because I do not think this would compile.

There is no variadic template parameter Args (args) here

m_last = m_first;
} else {
decrement(m_first);
boost::allocator_construct(alloc(), boost::to_address(m_first), ::boost::forward<V>(value));
++m_size;
}
} BOOST_CATCH(...) {
increment(m_first);
BOOST_RETHROW
}
BOOST_CATCH_END
}
#endif

public:
//! Insert a new element at the end of the <code>circular_buffer</code>.
/*!
Expand Down Expand Up @@ -1583,6 +1663,64 @@ private empty_value<Alloc>
push_front(boost::move(temp));
}

//! Construct a new element at the end of the <code>circular_buffer</code>.
/*!
\post if <code>capacity() > 0</code> then <code>back() == item</code><br>
If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
<code>0</code>, nothing will be inserted.
\param item The element to be inserted.
\throws Whatever <code>T::T(Args...)</code> throws.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
\par Iterator Invalidation
Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link push_back() push_back(const_reference)\endlink</code>,
<code>pop_back()</code>, <code>emplace_front()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_back(BOOST_FWD_REF(Args) ...args) {
emplace_back_impl(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_back(BOOST_FWD_REF(V) value) {
emplace_back_impl(::boost::forward<V>(value));
}
#endif

//! Construct a new element at the beginning of the <code>circular_buffer</code>.
/*!
\post if <code>capacity() > 0</code> then <code>back() == item</code><br>
If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
<code>0</code>, nothing will be inserted.
\param item The element to be inserted.
\throws Whatever <code>T::T(Args...)</code> throws.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
\par Iterator Invalidation
Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link push_front() push_front(const_reference)\endlink</code>,
<code>pop_front()</code>, <code>emplace_back()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_front(BOOST_FWD_REF(Args) ...args) {
emplace_front_impl(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_front(BOOST_FWD_REF(V) value) {
emplace_front_impl(::boost::forward<V>(value));
}
#endif

//! Remove the last element from the <code>circular_buffer</code>.
/*!
\pre <code>!empty()</code>
Expand Down
70 changes: 70 additions & 0 deletions include/boost/circular_buffer/space_optimized.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,76 @@ public:<br>
circular_buffer<T, Alloc>::push_front();
}

//! Construct a new element at the end of the space optimized circular buffer.
/*!
\post if <code>capacity().%capacity() > 0</code> then <code>back() == item</code><br>
If the <code>circular_buffer_space_optimized</code> is full, the first element will be removed. If the
capacity is <code>0</code>, nothing will be inserted.<br><br>
The amount of allocated memory in the internal buffer may be predictively increased.
\param item The element to be inserted.
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(Args...)</code> throws.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer_space_optimized</code> (except iterators
equal to <code>end()</code>).
\par Complexity
Linear (in the size of the <code>circular_buffer_space_optimized</code>).
\sa <code>\link push_front() push_front(const_reference)\endlink</code>, <code>pop_back()</code>,
<code>pop_front()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_back(BOOST_FWD_REF(Args) ...args) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_back(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_back(BOOST_FWD_REF(V) value) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_back(::boost::forward<V>(value));
}
#endif

//! Construct a new element at the beginning of the space optimized circular buffer.
/*!
\post if <code>capacity().%capacity() > 0</code> then <code>front() == item</code><br>
If the <code>circular_buffer_space_optimized</code> is full, the last element will be removed. If the
capacity is <code>0</code>, nothing will be inserted.<br><br>
The amount of allocated memory in the internal buffer may be predictively increased.
\param item The element to be inserted.
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(Args...)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer_space_optimized</code> (except iterators
equal to <code>end()</code>).
\par Complexity
Linear (in the size of the <code>circular_buffer_space_optimized</code>).
\sa <code>\link push_back() push_back(const_reference)\endlink</code>, <code>pop_back()</code>,
<code>pop_front()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_front(BOOST_FWD_REF(Args) ...args) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_front(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_front(BOOST_FWD_REF(V) value) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_front(::boost::forward<V>(value));
}
#endif

//! Remove the last element from the space optimized circular buffer.
/*!
\pre <code>!empty()</code>
Expand Down
27 changes: 27 additions & 0 deletions test/common.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,32 @@ void pop_back_test() {
generic_test(cb);
}

void emplace_test(){
CB_CONTAINER<MyInteger> cb(4);
cb.emplace_back(4);
cb.emplace_back(5);
cb.emplace_back(6);
cb.emplace_back(7);

BOOST_TEST(cb.size() == 4);
BOOST_TEST(cb.front() == 4);
BOOST_TEST(cb.back() == 7);

cb.emplace_front(3);
cb.emplace_front(2);

BOOST_TEST(cb.front() == 2);
BOOST_TEST(cb.back() == 5);

cb.emplace_front(1);
cb.emplace_front(0);

BOOST_TEST(cb.size() == 4);
BOOST_TEST(*cb.begin() == 0);
BOOST_TEST(cb.front() == 0);
BOOST_TEST(cb.back() == 3);
}

void insert_test() {

CB_CONTAINER<MyInteger> cb1(4);
Expand Down Expand Up @@ -2467,6 +2493,7 @@ void run_common_tests()
swap_test();
push_back_test();
pop_back_test();
emplace_test();
insert_test();
insert_n_test();
insert_range_test();
Expand Down