Skip to content

Commit 9bfbeb7

Browse files
committed
DynamicBuffer: Add function for moves to String
Allow moving a DynamicBuffer's buffer in to a String object. Requires that the buffer hold a null-terminated string.
1 parent d196a5f commit 9bfbeb7

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/DynamicBuffer.cpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,42 @@
22
#include <numeric>
33

44
// Helper class - lets us move the buffer out of a String
5-
class ReleasableString : public String {
6-
public:
7-
// Inherit constructors
8-
using String::String;
9-
ReleasableString(String&& s) : String(std::move(s)) {};
10-
11-
// Special feature: releease the buffer to the caller without deallocating
12-
char* release() {
13-
if (isSSO()) return nullptr;
14-
auto result = wbuffer();
15-
init();
16-
return result;
17-
}
18-
};
5+
namespace {
6+
class DynamicBufferString : public String {
7+
public:
8+
// Inherit constructors
9+
using String::String;
10+
DynamicBufferString(String&& s) : String(std::move(s)) {};
11+
DynamicBufferString(DynamicBuffer&& d) : String() {
12+
setSSO(false);
13+
setCapacity(d.size() - 1);
14+
setBuffer(d.release());
15+
setLen(strlen(ptr.buff));
16+
}
17+
18+
// Special feature: releease the buffer to the caller without deallocating
19+
char* release() {
20+
if (isSSO()) return nullptr;
21+
auto result = wbuffer();
22+
init();
23+
return result;
24+
}
25+
};
26+
}
1927

2028
DynamicBuffer::DynamicBuffer(String&& s) : _data(nullptr), _len(s.length()) {
21-
auto rb = ReleasableString(std::move(s));
29+
auto rb = DynamicBufferString(std::move(s));
2230
_data = rb.release();
2331
if (!_data) {
2432
*this = DynamicBuffer(rb); // use const-ref constructor to copy string
2533
}
2634
}
2735

36+
String toString(DynamicBuffer buf) {
37+
auto dbstr = DynamicBufferString(std::move(buf));
38+
return std::move(*static_cast<String*>(&dbstr)); // Move-construct the result string from dbstr
39+
}
40+
2841
template<typename list_type>
2942
static inline list_type allocateList(size_t total, size_t max_buffer_size) {
3043
list_type buffers;

src/DynamicBuffer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ class DynamicBuffer {
3939

4040
explicit operator bool() const { return (_data != nullptr) && (_len > 0); }
4141

42+
// Release the buffer without freeing it
43+
char* release() { char* temp = _data; _data = nullptr; _len = 0; return temp; }
44+
4245
// TODO, if it ever matters - resizing
4346
};
4447

45-
4648
// Same interface as DynamicBuffer, but with shared_ptr semantics: buffer is held until last copy releases it.
4749
class SharedBuffer {
4850
std::shared_ptr<DynamicBuffer> _buf;
@@ -65,6 +67,9 @@ class SharedBuffer {
6567
DynamicBuffer copy() const { return *_buf; }; // Make a copy of the buffer
6668
};
6769

70+
// Utility functions
71+
String toString(DynamicBuffer buf); // Move a buffer in to a string. Buffer will be moved if buf is an rvalue, copied otherwise.
72+
6873

6974
// DynamicBufferList - an RAII list of DynamicBuffers
7075
// This structure can be used to create a chain of buffers, useful when the heap could get fragmented

0 commit comments

Comments
 (0)