From 895a983d1717b7642ed837bd7977ce5a85d9713d Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 4 Jun 2017 08:31:57 -0700 Subject: [PATCH] Allow constructing Buffers from std::vector --- include/cppkafka/buffer.h | 17 ++++++++++++++++- tests/buffer_test.cpp | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/cppkafka/buffer.h b/include/cppkafka/buffer.h index f5b97d6..9237cf7 100644 --- a/include/cppkafka/buffer.h +++ b/include/cppkafka/buffer.h @@ -77,6 +77,21 @@ public: static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)"); } + /** + * Constructs a buffer from a vector + * + * \param data The vector to be used as input + */ + template + Buffer(const std::vector& data) + : data_(data.data()), size_(data.size()) { + static_assert(sizeof(T) == sizeof(DataType), "sizeof(T) != sizeof(DataType)"); + } + + // Don't allow construction from temporary vectors + template + Buffer(std::vector&& data) = delete; + /** * \brief Construct a buffer from a const string ref * @@ -85,7 +100,7 @@ public: */ Buffer(const std::string& data); - // Don't allow construction from temporaries + // Don't allow construction from temporary strings Buffer(std::string&&) = delete; Buffer(const Buffer&) = delete; diff --git a/tests/buffer_test.cpp b/tests/buffer_test.cpp index ac842f0..a7fa69d 100644 --- a/tests/buffer_test.cpp +++ b/tests/buffer_test.cpp @@ -43,6 +43,13 @@ TEST_F(BufferTest, VectorConversion) { EXPECT_EQ(data, string(buffer_as_vector.begin(), buffer_as_vector.end())); } +TEST_F(BufferTest, VectorConstruction) { + const string str_data = "Hello world!"; + const vector data(str_data.begin(), str_data.end()); + Buffer buffer(data); + EXPECT_EQ(str_data, buffer); +} + TEST_F(BufferTest, Equality) { string data = "Hello world!"; Buffer buffer1(data);