Skip to content

Commit ab002fe

Browse files
authored
Merge pull request #147 from accelerated/raw_arrays
Support for raw array Buffer constructor
2 parents d89840b + 06ddd79 commit ab002fe

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

include/cppkafka/buffer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ class CPPKAFKA_API Buffer {
126126
*/
127127
template <typename T, size_t N>
128128
Buffer(std::array<T, N>&& data) = delete;
129+
130+
/**
131+
* Constructs a buffer from a raw array
132+
*
133+
* \param data The the array to be used as input
134+
*/
135+
template <typename T, size_t N>
136+
Buffer(const T(&data)[N])
137+
: Buffer(data, N) {
138+
}
139+
140+
// Don't allow construction from temporary raw arrays
141+
template <typename T, size_t N>
142+
Buffer(T(&&data)[N]) = delete;
129143

130144
/**
131145
* \brief Construct a buffer from a const string ref

tests/buffer_test.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#include <string>
22
#include <vector>
3+
#include <array>
34
#include <sstream>
45
#include <catch.hpp>
56
#include "cppkafka/buffer.h"
67

78
using std::string;
89
using std::vector;
10+
using std::array;
911
using std::ostringstream;
1012

1113
using namespace cppkafka;
@@ -36,14 +38,32 @@ TEST_CASE("conversions", "[buffer]") {
3638
}
3739

3840
TEST_CASE("construction", "[buffer]") {
41+
// From string
3942
const string str_data = "Hello world!";
40-
const vector<uint8_t> data(str_data.begin(), str_data.end());
41-
const Buffer buffer(data);
42-
const Buffer buffer2(data.begin(), data.end());
43-
const Buffer buffer3(str_data.data(), str_data.data() + str_data.size());
43+
// From vector
44+
const vector<uint8_t> vector_data(str_data.begin(), str_data.end());
45+
// From array
46+
const array<char,12> array_data{'H','e','l','l','o',' ','w','o','r','l','d','!'};
47+
// From raw array
48+
const char raw_array[12]{'H','e','l','l','o',' ','w','o','r','l','d','!'};
49+
50+
// Build buffers
51+
const Buffer buffer(vector_data); //vector
52+
const Buffer buffer2(vector_data.begin(), vector_data.end()); //iterators
53+
const Buffer buffer3(str_data.data(), str_data.data() + str_data.size()); //char iterators
54+
const Buffer buffer4(array_data); //arrays
55+
const Buffer buffer5(raw_array); //raw arrays
56+
const Buffer buffer6(str_data); //string
57+
const Buffer buffer7(str_data.data(), str_data.size()); //type + size
58+
59+
// Test
4460
CHECK(str_data == buffer);
4561
CHECK(buffer == buffer2);
4662
CHECK(buffer == buffer3);
63+
CHECK(buffer == buffer4);
64+
CHECK(buffer == buffer5);
65+
CHECK(buffer == buffer6);
66+
CHECK(buffer == buffer7);
4767
}
4868

4969

0 commit comments

Comments
 (0)