mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
synced 2025-11-01 11:07:56 +00:00
Add iterators to Buffer
This commit is contained in:
@@ -49,8 +49,16 @@ namespace cppkafka {
|
|||||||
*/
|
*/
|
||||||
class Buffer {
|
class Buffer {
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* The type of data this buffer points to
|
||||||
|
*/
|
||||||
using DataType = unsigned char;
|
using DataType = unsigned char;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The const iterator type
|
||||||
|
*/
|
||||||
|
using const_iterator = const DataType*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty buffer
|
* Constructs an empty buffer
|
||||||
*/
|
*/
|
||||||
@@ -94,6 +102,16 @@ public:
|
|||||||
*/
|
*/
|
||||||
size_t get_size() const;
|
size_t get_size() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an iterator to the beginning of this buffer
|
||||||
|
*/
|
||||||
|
const_iterator begin() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an iterator to the end of this buffer
|
||||||
|
*/
|
||||||
|
const_iterator end() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether this is a non empty buffer
|
* Checks whether this is a non empty buffer
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -58,8 +58,16 @@ size_t Buffer::get_size() const {
|
|||||||
return size_;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Buffer::const_iterator Buffer::begin() const {
|
||||||
|
return data_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Buffer::const_iterator Buffer::end() const {
|
||||||
|
return data_ + size_;
|
||||||
|
}
|
||||||
|
|
||||||
Buffer::operator bool() const {
|
Buffer::operator bool() const {
|
||||||
return data_ != nullptr;
|
return size_ != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer::operator string() const {
|
Buffer::operator string() const {
|
||||||
@@ -78,17 +86,16 @@ bool Buffer::operator!=(const Buffer& rhs) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ostream& operator<<(ostream& output, const Buffer& rhs) {
|
ostream& operator<<(ostream& output, const Buffer& rhs) {
|
||||||
for (size_t i = 0; i < rhs.get_size(); ++i) {
|
for (const uint8_t value : rhs) {
|
||||||
char c = static_cast<char>(rhs.get_data()[i]);
|
if (value >= 0x20 && value < 0x7f) {
|
||||||
if (c >= ' ' && c < 127) {
|
output << value;
|
||||||
output << c;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
output << "\\x";
|
output << "\\x";
|
||||||
if (c < 16) {
|
if (value < 16) {
|
||||||
output << '0';
|
output << '0';
|
||||||
}
|
}
|
||||||
output << hex << (int)c << dec;
|
output << hex << static_cast<int>(value) << dec;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
|
|||||||
@@ -15,6 +15,15 @@ public:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TEST_F(BufferTest, OperatorBool) {
|
||||||
|
string data = "Hello world!";
|
||||||
|
Buffer buffer1(data);
|
||||||
|
Buffer buffer2;
|
||||||
|
|
||||||
|
EXPECT_TRUE(buffer1);
|
||||||
|
EXPECT_FALSE(buffer2);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(BufferTest, StringConversion) {
|
TEST_F(BufferTest, StringConversion) {
|
||||||
string data = "Hello world!";
|
string data = "Hello world!";
|
||||||
Buffer buffer(data);
|
Buffer buffer(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user