Commit 5531d30c authored by Jan Veverak Koniarik's avatar Jan Veverak Koniarik
Browse files

changed API of static_vector/circular buffer

parent c726c556
Pipeline #127908 passed with stage
in 4 minutes and 32 seconds
......@@ -43,7 +43,7 @@ public:
return nullptr;
#endif
}
std::size_t i = free_.pop_back();
std::size_t i = free_.take_back();
return &pools_[i];
}
......
......@@ -110,12 +110,17 @@ public:
return ref_item( from_ );
}
T pop_front()
[[nodiscard]] T take_front()
{
T item = std::move( front() );
pop_front();
return item;
}
void pop_front()
{
delete_item( from_ );
from_ = next( from_ );
return item;
}
// methods for handling the back side of the circular buffer
......@@ -140,11 +145,11 @@ public:
[[nodiscard]] reference back()
{
return ref_item( to_ - 1 );
return ref_item( prev( to_ ) );
}
[[nodiscard]] const_reference back() const
{
return ref_item( to_ - 1 );
return ref_item( prev( to_ ) );
}
void push_back( T item )
......@@ -236,17 +241,18 @@ private:
void emplace_item( size_type i, Args&&... args )
{
std::construct_at(
reinterpret_cast< T* >( &data_ ) + i, std::forward< Args >( args )... );
reinterpret_cast< T* >( std::addressof( data_ ) ) + i,
std::forward< Args >( args )... );
}
// Reference to the item in data_storage.
[[nodiscard]] reference ref_item( size_type i )
{
return *( reinterpret_cast< T* >( &data_ ) + i );
return *( reinterpret_cast< T* >( std::addressof( data_ ) ) + i );
}
[[nodiscard]] const_reference ref_item( size_type i ) const
{
return *( reinterpret_cast< const T* >( &data_ ) + i );
return *( reinterpret_cast< const T* >( std::addressof( data_ ) ) + i );
}
// Cleans entire buffer from items.
......@@ -261,14 +267,14 @@ private:
{
to_ = other.size();
std::uninitialized_copy(
other.begin(), other.end(), reinterpret_cast< T* >( &data_ ) );
other.begin(), other.end(), reinterpret_cast< T* >( std::addressof( data_ ) ) );
}
void move_from( static_circular_buffer& other )
{
to_ = other.size();
std::uninitialized_move(
other.begin(), other.end(), reinterpret_cast< T* >( &data_ ) );
other.begin(), other.end(), reinterpret_cast< T* >( std::addressof( data_ ) ) );
}
// Use this only when moving the indexes in the circular buffer - bullet-proof.
......
......@@ -127,12 +127,17 @@ public:
size_ += 1;
}
T pop_back()
[[nodiscard]] T take_back()
{
T item = std::move( back() );
pop_back();
return item;
}
void pop_back()
{
delete_item( size_ - 1 );
size_ -= 1;
return item;
}
[[nodiscard]] reference back()
......
#include "emlabcpp/static_circular_buffer.h"
#include "emlabcpp/algorithm.h"
#include "operations_counter.h"
#include <gtest/gtest.h>
......@@ -267,3 +268,31 @@ TEST( static_circular_buffer_test, back_inserter )
EXPECT_TRUE( are_equal ) << "buff: " << view{ buff } << "\n"
<< "expected: " << view{ expected } << "\n";
}
TEST( static_circular_buffer_test, back )
{
trivial_buffer tbuff;
for ( int i : { 1, 2, 3, 4, 5, 6, 7 } ) {
tbuff.push_back( i );
}
EXPECT_TRUE( tbuff.full() );
tbuff.pop_front();
tbuff.push_back( 8 );
EXPECT_TRUE( tbuff.full() );
EXPECT_EQ( tbuff.back(), 8 );
}
struct operations_counter_circular_buffer
{
using container_type = static_circular_buffer< operations_counter, 32 >;
static constexpr std::size_t n = 16;
};
using types = ::testing::Types< operations_counter_circular_buffer >;
INSTANTIATE_TYPED_TEST_SUITE_P(
operations_counter_circular_buffer_fixture,
operations_counter_fixture,
types );
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment