From 06ddd79a29b578158ff1efd0a151e94697cfb768 Mon Sep 17 00:00:00 2001 From: accelerated <> Date: Mon, 10 Dec 2018 15:39:16 -0500 Subject: [PATCH] Support for raw array Buffer constructor --- include/cppkafka/buffer.h | 14 ++++++++++++++ tests/buffer_test.cpp | 28 ++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/include/cppkafka/buffer.h b/include/cppkafka/buffer.h index 671160d..6a9453f 100644 --- a/include/cppkafka/buffer.h +++ b/include/cppkafka/buffer.h @@ -126,6 +126,20 @@ public: */ template Buffer(std::array&& data) = delete; + + /** + * Constructs a buffer from a raw array + * + * \param data The the array to be used as input + */ + template + Buffer(const T(&data)[N]) + : Buffer(data, N) { + } + + // Don't allow construction from temporary raw arrays + template + Buffer(T(&&data)[N]) = delete; /** * \brief Construct a buffer from a const string ref diff --git a/tests/buffer_test.cpp b/tests/buffer_test.cpp index 126f524..c4c9210 100644 --- a/tests/buffer_test.cpp +++ b/tests/buffer_test.cpp @@ -1,11 +1,13 @@ #include #include +#include #include #include #include "cppkafka/buffer.h" using std::string; using std::vector; +using std::array; using std::ostringstream; using namespace cppkafka; @@ -36,14 +38,32 @@ TEST_CASE("conversions", "[buffer]") { } TEST_CASE("construction", "[buffer]") { + // From string const string str_data = "Hello world!"; - const vector data(str_data.begin(), str_data.end()); - const Buffer buffer(data); - const Buffer buffer2(data.begin(), data.end()); - const Buffer buffer3(str_data.data(), str_data.data() + str_data.size()); + // From vector + const vector vector_data(str_data.begin(), str_data.end()); + // From array + const array array_data{'H','e','l','l','o',' ','w','o','r','l','d','!'}; + // From raw array + const char raw_array[12]{'H','e','l','l','o',' ','w','o','r','l','d','!'}; + + // Build buffers + const Buffer buffer(vector_data); //vector + const Buffer buffer2(vector_data.begin(), vector_data.end()); //iterators + const Buffer buffer3(str_data.data(), str_data.data() + str_data.size()); //char iterators + const Buffer buffer4(array_data); //arrays + const Buffer buffer5(raw_array); //raw arrays + const Buffer buffer6(str_data); //string + const Buffer buffer7(str_data.data(), str_data.size()); //type + size + + // Test CHECK(str_data == buffer); CHECK(buffer == buffer2); CHECK(buffer == buffer3); + CHECK(buffer == buffer4); + CHECK(buffer == buffer5); + CHECK(buffer == buffer6); + CHECK(buffer == buffer7); }