aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/tourist/foundation/src/buffer.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-04-23 18:16:57 +0200
committerStefan Boberg <[email protected]>2026-04-23 18:16:57 +0200
commit0232b991cd7d8e3a2114ea30e4591dd3e7b65c36 (patch)
tree94730e7594fd09ae1fa820391ce311f6daf13905 /thirdparty/tourist/foundation/src/buffer.cpp
parentFix forward declaration order for s_GotSigWinch and SigWinchHandler (diff)
parenttrace: declare Region event name fields as AnsiString (#1012) (diff)
downloadarchived-zen-sb/zen-help.tar.xz
archived-zen-sb/zen-help.zip
Merge branch 'main' into sb/zen-helpsb/zen-help
- Combine HelpCommand (this branch) with HistoryCommand (main) in zen CLI dispatcher - Keep filter-aware TuiPickOne rewrite; adopt main's ASCII arrow glyphs in doc comment
Diffstat (limited to 'thirdparty/tourist/foundation/src/buffer.cpp')
-rw-r--r--thirdparty/tourist/foundation/src/buffer.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/thirdparty/tourist/foundation/src/buffer.cpp b/thirdparty/tourist/foundation/src/buffer.cpp
new file mode 100644
index 000000000..a4baccdef
--- /dev/null
+++ b/thirdparty/tourist/foundation/src/buffer.cpp
@@ -0,0 +1,117 @@
+#include <foundation/buffer.h>
+
+#include "slab.h"
+
+//------------------------------------------------------------------------------
+Buffer::~Buffer()
+{
+ dec_ref();
+}
+
+//------------------------------------------------------------------------------
+Buffer::Buffer(Buffer&& rhs)
+{
+ move(std::move(rhs));
+}
+
+//------------------------------------------------------------------------------
+void Buffer::operator = (Buffer&& rhs)
+{
+ move(std::move(rhs));
+}
+
+//------------------------------------------------------------------------------
+const uint8* Buffer::get_pointer() const
+{
+ return _slab->get_pointer() + _offset;
+}
+
+//------------------------------------------------------------------------------
+uint32 Buffer::get_size() const
+{
+ return _size;
+}
+
+//------------------------------------------------------------------------------
+BufferStream Buffer::create_stream() const
+{
+ return BufferStream(_slab, get_pointer(), get_size());
+}
+
+//------------------------------------------------------------------------------
+BufferRef Buffer::create_ref() const
+{
+ return BufferRef(_slab, get_pointer());
+}
+
+//------------------------------------------------------------------------------
+void Buffer::inc_ref()
+{
+ if (_slab != nullptr)
+ _slab->inc_ref();
+}
+
+//------------------------------------------------------------------------------
+void Buffer::dec_ref()
+{
+ if (_slab != nullptr)
+ _slab->dec_ref();
+ _slab = nullptr;
+}
+
+//------------------------------------------------------------------------------
+Buffer Buffer::create_sub_buffer(uint32 left, uint32 right) const
+{
+ right = std::min<uint32>(right, _size);
+ return create_sub_buffer(get_pointer() + left, right - left);
+}
+
+//------------------------------------------------------------------------------
+Buffer Buffer::create_sub_buffer(const uint8* ptr, uint32 size) const
+{
+ uint32 offset = uint32(ptrdiff_t(ptr - get_pointer()));
+ if (offset >= _size)
+ return Buffer();
+
+ Buffer buffer = clone();
+ buffer._offset += offset;
+ buffer._size = size;
+ return buffer;
+}
+
+//------------------------------------------------------------------------------
+void Buffer::move(Buffer&& rhs)
+{
+ dec_ref();
+ std::swap(_slab, rhs._slab);
+ std::swap(_offset, rhs._offset);
+ std::swap(_size, rhs._size);
+}
+
+//------------------------------------------------------------------------------
+Buffer Buffer::clone() const
+{
+ Buffer buffer;
+ buffer._slab = _slab;
+ buffer._offset = _offset;
+ buffer._size = _size;
+ buffer.inc_ref();
+ return buffer;
+}
+
+
+
+//------------------------------------------------------------------------------
+uint8* MutableBuffer::get_pointer()
+{
+ return _slab->get_pointer() + _offset;
+}
+
+//------------------------------------------------------------------------------
+MutableBuffer::MutableBuffer(Slab* slab, uint32 size, uint32 offset)
+{
+ _slab = slab;
+ _offset = offset;
+ _size = size;
+ inc_ref();
+}