summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2019-09-24 13:38:47 -0500
committerMatt Spinler <spinler@us.ibm.com>2019-10-09 13:11:51 +0000
commitd377793c6fe0b8ea8755d2526c1ef20d51f5043e (patch)
treed890c3e6356f9e026e366e79772e67aa24c8cdec
parentb832363d4314f7d93d09a626e9b987c287e774df (diff)
downloadphosphor-logging-d377793c6fe0b8ea8755d2526c1ef20d51f5043e.tar.gz
phosphor-logging-d377793c6fe0b8ea8755d2526c1ef20d51f5043e.zip
PEL: Add Stream ops for std::vector<uint8_t>
Add << and >> operators to the Stream class for use with a std::vector<uint8_t>. Signed-off-by: Matt Spinler <spinler@us.ibm.com> Change-Id: I9656ee4e34840d311148e6a701131b3420e62d25
-rw-r--r--extensions/openpower-pels/stream.hpp36
-rw-r--r--test/openpower-pels/stream_test.cpp22
2 files changed, 57 insertions, 1 deletions
diff --git a/extensions/openpower-pels/stream.hpp b/extensions/openpower-pels/stream.hpp
index eb91d4c..5c813b2 100644
--- a/extensions/openpower-pels/stream.hpp
+++ b/extensions/openpower-pels/stream.hpp
@@ -143,6 +143,23 @@ class Stream
}
/**
+ * @brief Extraction operator for a std::vector<uint8_t>
+ *
+ * The vector's size is the amount extracted.
+ *
+ * @param[out] value - filled in with the value
+ * @return Stream&
+ */
+ Stream& operator>>(std::vector<uint8_t>& value)
+ {
+ if (!value.empty())
+ {
+ read(value.data(), value.size());
+ }
+ return *this;
+ }
+
+ /**
* @brief Insert operator for a uint8_t
*
* @param[in] value - the value to write to the stream
@@ -206,6 +223,23 @@ class Stream
}
/**
+ * @brief Insert operator for a std::vector<uint8_t>
+ *
+ * The full vector is written to the stream.
+ *
+ * @param[in] value - the value to write to the stream
+ * @return Stream&
+ */
+ Stream& operator<<(const std::vector<uint8_t>& value)
+ {
+ if (!value.empty())
+ {
+ write(value.data(), value.size());
+ }
+ return *this;
+ }
+
+ /**
* @brief Sets the offset of the stream
*
* @param[in] newOffset - the new offset
@@ -261,7 +295,7 @@ class Stream
* @param[in] in - the data to write
* @param[in] size - the size to write
*/
- void write(void* in, std::size_t size)
+ void write(const void* in, std::size_t size)
{
size_t newSize = _offset + size;
if (newSize > _data.size())
diff --git a/test/openpower-pels/stream_test.cpp b/test/openpower-pels/stream_test.cpp
index dd5ca93..619952f 100644
--- a/test/openpower-pels/stream_test.cpp
+++ b/test/openpower-pels/stream_test.cpp
@@ -158,3 +158,25 @@ TEST(StreamTest, TestOffsets)
EXPECT_THROW(stream.offset(100), std::out_of_range);
}
+
+TEST(StreamTest, TestVectorInsertExtract)
+{
+ std::vector<uint8_t> toInsert{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
+ std::vector<uint8_t> data;
+
+ // Insert
+ Stream stream{data};
+ stream << toInsert;
+ EXPECT_EQ(data, toInsert);
+
+ // Extract
+ std::vector<uint8_t> toExtract;
+ toExtract.resize(toInsert.size());
+ stream.offset(0);
+ stream >> toExtract;
+
+ EXPECT_EQ(data, toExtract);
+
+ // Go off the end
+ EXPECT_THROW(stream >> toExtract, std::out_of_range);
+}
OpenPOWER on IntegriCloud