Skip to content

Commit 64d65f8

Browse files
committed
DynamicBuffer: Construction from SharedBuffer
This permits a more graceful handling of the 2.1.0 API ...
1 parent b3bdc6b commit 64d65f8

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/DynamicBuffer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ DynamicBuffer::DynamicBuffer(String&& s) : _data(nullptr), _len(s.length()) {
3333
}
3434
}
3535

36+
DynamicBuffer::DynamicBuffer(const SharedBuffer& b) : DynamicBuffer(b.copy()) {};
37+
38+
DynamicBuffer::DynamicBuffer(SharedBuffer&& b) : _data(nullptr), _len(0) {
39+
if (b) *this = std::move(*b._buf);
40+
}
41+
3642
String toString(DynamicBuffer buf) {
3743
auto dbstr = DynamicBufferString(std::move(buf));
3844
return std::move(*static_cast<String*>(&dbstr)); // Move-construct the result string from dbstr

src/DynamicBuffer.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <list>
99
#include <utility>
1010

11+
// Forward declaration
12+
class SharedBuffer;
13+
1114
// The DynamicBuffer class holds a malloc() allocated heap buffer.
1215
// It's similar to std::vector<char>, but permits allocation failures without crashing the system.
1316
class DynamicBuffer {
@@ -23,6 +26,8 @@ class DynamicBuffer {
2326
DynamicBuffer(const char* buf, size_t len) : DynamicBuffer(len) { if (_data) memcpy(_data, buf, len); };
2427
explicit DynamicBuffer(const String& s) : DynamicBuffer(s.begin(), s.length()) {};
2528
explicit DynamicBuffer(String&&); // Move string contents in to buffer if possible
29+
DynamicBuffer(const SharedBuffer&);
30+
DynamicBuffer(SharedBuffer&&);
2631
~DynamicBuffer() { clear(); };
2732

2833
// Move
@@ -48,6 +53,7 @@ class DynamicBuffer {
4853
// Same interface as DynamicBuffer, but with shared_ptr semantics: buffer is held until last copy releases it.
4954
class SharedBuffer {
5055
std::shared_ptr<DynamicBuffer> _buf;
56+
friend class DynamicBuffer;
5157

5258
public:
5359

@@ -56,8 +62,8 @@ class SharedBuffer {
5662
SharedBuffer(const char* buf, size_t len) : _buf(std::make_shared<DynamicBuffer>(buf, len)) {};
5763
explicit SharedBuffer(const String& s) : _buf(std::make_shared<DynamicBuffer>(s)) {};
5864
explicit SharedBuffer(String&& s) : _buf(std::make_shared<DynamicBuffer>(std::move(s))) {};
59-
explicit SharedBuffer(const DynamicBuffer &d) : _buf(std::make_shared<DynamicBuffer>(d)) {};
60-
explicit SharedBuffer(DynamicBuffer&& d) : _buf(std::make_shared<DynamicBuffer>(std::move(d))) {};
65+
SharedBuffer(const DynamicBuffer &d) : _buf(std::make_shared<DynamicBuffer>(d)) {};
66+
SharedBuffer(DynamicBuffer&& d) : _buf(std::make_shared<DynamicBuffer>(std::move(d))) {};
6167

6268
char* data() const { return _buf ? _buf->data() : nullptr; };
6369
size_t size() const { return _buf ? _buf->size() : 0U; };

0 commit comments

Comments
 (0)