17 #ifndef THRILL_NET_BUFFER_BUILDER_HEADER 18 #define THRILL_NET_BUFFER_BUILDER_HEADER 29 #include <type_traits> 47 using Byte =
unsigned char;
82 other.data_ =
nullptr;
99 Assign(str.data(), str.size());
114 if (data_)
free(data_);
117 capacity_ = other.capacity_;
118 other.data_ =
nullptr;
133 if (data_)
free(data_);
135 size_ = capacity_ = 0;
179 assert(n <= capacity_);
190 data_ =
static_cast<Byte*
>(
realloc(data_, capacity_));
204 while (newsize < n) {
205 if (newsize < 256) newsize = 512;
206 else if (newsize < 1024 * 1024) newsize = 2 * newsize;
207 else newsize += 1024 * 1024;
220 size_ = capacity_ = 0;
226 return std::string(reinterpret_cast<const char*>(data_), size_);
244 if (len > capacity_)
Reserve(len);
246 const Byte* cdata =
reinterpret_cast<const Byte*
>(
data);
247 std::copy(cdata, cdata + len, data_);
265 size_t rem = size_ % n;
268 size_t add = n - rem;
269 if (size_ + add > capacity_)
DynReserve(size_ + add);
270 std::fill(data_ + size_, data_ + size_ + add, 0);
273 assert((size_ % n) == 0);
285 if (size_ + len > capacity_)
DynReserve(size_ + len);
287 const Byte* cdata =
reinterpret_cast<const Byte*
>(
data);
288 std::copy(cdata, cdata + len, data_ + size_);
302 return Append(s.data(), s.size());
307 template <
typename Type>
311 "You only want to Put() trivially copyable types as raw values.");
315 *
reinterpret_cast<Type*
>(data_ +
size_) = item;
316 size_ +=
sizeof(
Type);
323 return Put<uint8_t>(
data);
328 template <
typename Type>
330 return Put<Type>(item);
350 {
return data_ +
size_; }
353 {
return data_ +
size_; }
361 return *(
begin() + i);
372 #endif // !THRILL_NET_BUFFER_BUILDER_HEADER size_t size() const
Return the currently used length in bytes.
iterator begin()
return mutable iterator to first element
const_iterator cend() const
return constant iterator beyond last element
const Byte * const_iterator
simple pointer iterators
BufferBuilder & AppendString(const std::string &s)
const Byte & const_reference
simple pointer references
BufferBuilder & DynReserve(size_t n)
const_iterator begin() const
return constant iterator to first element
size_t capacity() const
Return the currently allocated buffer capacity.
~BufferBuilder()
Destroys the memory space.
Byte * iterator
simple pointer iterators
Buffer ToBuffer()
Explicit conversion to Buffer MOVING the memory ownership.
BufferBuilder & PutByte(Byte data)
Put a single byte to the buffer (used via CRTP from ItemWriterToolsBase)
BufferBuilder & Append(const void *data, size_t len)
Append a memory range to the buffer.
BufferBuilder(const std::string &str)
Constructor from std::string, COPIES string content.
BufferBuilder()=default
Create a new empty object.
BufferBuilder(size_t size)
Constructor, create object with n bytes pre-allocated.
Byte * data()
Return a writeable pointer to the currently kept memory area.
Byte & reference
simple pointer references
BufferBuilder & Deallocate()
const Byte * data() const
Return a pointer to the currently kept memory area.
BufferBuilder & Assign(const BufferBuilder &other)
size_t size_
Size of valid data.
BufferBuilder & operator=(const BufferBuilder &other)
Assignment operator: copy other's memory range into buffer.
static Buffer Acquire(void *data, size_type size)
BufferBuilder(const BufferBuilder &other)
Copy-Constructor, duplicates memory content.
BufferBuilder & Align(size_t n)
Align the size of the buffer to a multiple of n. Fills up with 0s.
reference operator[](size_t i)
return the i-th position of the vector
unsigned char Byte
type used to store the bytes
BufferBuilder & Append(const class BufferBuilder &bb)
Append the contents of a different buffer object to this one.
BufferBuilder & Clear()
Clears the memory contents, does not deallocate the memory.
const_iterator cbegin() const
return constant iterator to first element
std::is_trivially_copyable< T > is_trivially_copyable
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
std::string ToString() const
Explicit conversion to std::string (copies memory of course).
BufferBuilder & Assign(const void *data, size_t len)
BufferBuilder(const void *data, size_t size)
Constructor, copy memory area.
BufferBuilder & Reserve(size_t n)
Make sure that at least n bytes are allocated.
BufferBuilder(BufferBuilder &&other) noexcept
Move-Constructor, moves memory area.
BufferBuilder represents a dynamically growable area of memory, which can be modified by appending in...
BufferBuilder & PutRaw(const Type &item)
Byte * data_
Allocated buffer pointer.
Simple buffer of characters without initialization or growing functionality.
void free(void *ptr) NOEXCEPT
exported free symbol that overrides loading from libc
BufferBuilder & Put(const Type &item)
const Byte * Detach()
Detach the memory from the object, returns the memory pointer.
iterator end()
return mutable iterator beyond last element
void * realloc(void *ptr, size_t size) NOEXCEPT
exported realloc() symbol that overrides loading from libc
BufferBuilder & set_size(size_t n)
size_t capacity_
Total capacity of buffer.
const_iterator end() const
return constant iterator beyond last element