util/CircularBuffer: add method GetSize()

This commit is contained in:
Max Kellermann 2014-03-15 23:11:13 +01:00
parent 3d4f588a7f
commit 89ac111853
2 changed files with 19 additions and 0 deletions

View File

@ -98,6 +98,15 @@ public:
return Next(tail) == head;
}
/**
* Returns the number of elements stored in this buffer.
*/
constexpr size_type GetSize() const {
return head <= tail
? tail - head
: capacity - head + tail;
}
/**
* Prepares writing. Returns a buffer range which may be written.
* When you are finished, call Append().

View File

@ -32,6 +32,7 @@ public:
/* [.......X] */
CPPUNIT_ASSERT_EQUAL(true, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(true, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Write().data);
@ -43,6 +44,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
@ -55,6 +57,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(true, buffer.Write().IsEmpty());
@ -65,6 +68,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
@ -78,6 +82,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(true, buffer.Write().IsEmpty());
@ -88,6 +93,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
@ -100,6 +106,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
@ -112,6 +119,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(true, buffer.Write().IsEmpty());
@ -122,6 +130,7 @@ public:
CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.Read().size);
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
@ -135,6 +144,7 @@ public:
buffer.Consume(3);
CPPUNIT_ASSERT_EQUAL(true, buffer.IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSize());
CPPUNIT_ASSERT_EQUAL(true, buffer.Read().IsEmpty());
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
CPPUNIT_ASSERT_EQUAL(&data[3], buffer.Write().data);