aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-03-15 23:11:13 +0100
committerMax Kellermann <max@duempel.org>2014-03-16 00:27:18 +0100
commit89ac111853eb4bae2fc358087c4c4657939321c0 (patch)
tree735bd12a1896ff049d58d1138eaa1ec99457561d
parent3d4f588a7ff6b3c0953d706c526f4f391db9dfd2 (diff)
downloadmpd-89ac111853eb4bae2fc358087c4c4657939321c0.tar.gz
mpd-89ac111853eb4bae2fc358087c4c4657939321c0.tar.xz
mpd-89ac111853eb4bae2fc358087c4c4657939321c0.zip
util/CircularBuffer: add method GetSize()
Diffstat (limited to '')
-rw-r--r--src/util/CircularBuffer.hxx9
-rw-r--r--test/TestCircularBuffer.hxx10
2 files changed, 19 insertions, 0 deletions
diff --git a/src/util/CircularBuffer.hxx b/src/util/CircularBuffer.hxx
index 67b60e198..0321b91f6 100644
--- a/src/util/CircularBuffer.hxx
+++ b/src/util/CircularBuffer.hxx
@@ -99,6 +99,15 @@ public:
}
/**
+ * 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().
*/
diff --git a/test/TestCircularBuffer.hxx b/test/TestCircularBuffer.hxx
index f8fb95418..88b0a9ea8 100644
--- a/test/TestCircularBuffer.hxx
+++ b/test/TestCircularBuffer.hxx
@@ -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);