8#include <system/array.h>
9#include <system/diagnostics/debug.h>
10#include <system/exceptions.h>
11#include <system/memory.h>
12#include <system/memory_extensions.h>
13#include <system/object.h>
14#include <system/object_ext.h>
15#include <system/span.h>
17#include "system/buffers/i_buffer_writer.h"
30 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
31 RTTI_INFO_TEMPLATE_CLASS(
ThisType, ThisTypeBaseTypesInfo)
38 m_buffer = System::MakeArray<T>(0);
47 if (initialCapacity <= 0)
52 m_buffer = System::MakeArray<T>(initialCapacity);
80 return m_buffer->get_Length();
87 return m_buffer->get_Length() - m_index;
97 CODEPORTING_DEBUG_ASSERT2(m_buffer->get_Length() >= m_index, u
"Invalid buffer state");
122 if (m_index > m_buffer->get_Length() - count)
124 ThrowInvalidOperationException_AdvancedTooFar(m_buffer->get_Length());
140 CheckAndResizeBuffer(sizeHint);
141 CODEPORTING_DEBUG_ASSERT2(m_buffer->get_Length() > m_index, u
"Invalid buffer state");
155 CheckAndResizeBuffer(sizeHint);
156 CODEPORTING_DEBUG_ASSERT2(m_buffer->get_Length() > m_index, u
"Invalid buffer state");
162 #ifdef ASPOSE_GET_SHARED_MEMBERS
163 void GetSharedMembers(System::Object::shared_members_type& result)
const override
165 Object::GetSharedMembers(result);
166 result.Add(
"System::Buffers::ArrayBufferWriter::m_buffer", this->m_buffer);
173 static const int32_t ArrayMaxLength = 0x7FFFFFC7;
176 static const int32_t DefaultInitialBufferSize = 256;
181 void CheckAndResizeBuffer(int32_t sizeHint)
195 int32_t currentLength = m_buffer->get_Length();
197 int32_t growBy = std::max(sizeHint, currentLength);
199 if (currentLength == 0)
201 growBy = std::max(growBy, DefaultInitialBufferSize);
204 int32_t newSize = currentLength + growBy;
206 if (
static_cast<uint32_t
>(newSize) >
static_cast<uint32_t
>(ArrayMaxLength))
208 uint32_t needed =
static_cast<uint32_t
>(currentLength -
get_FreeCapacity() + sizeHint);
209 CODEPORTING_DEBUG_ASSERT2(needed >
static_cast<uint32_t
>(currentLength), u
"Invalid buffer state");
211 if (needed >
static_cast<uint32_t
>(ArrayMaxLength))
213 ThrowOutOfMemoryException(needed);
216 newSize = ArrayMaxLength;
225 static void ThrowInvalidOperationException_AdvancedTooFar(int32_t capacity)
227 throw InvalidOperationException(
System::String::Format(u
"Cannot advance past the end of the buffer, which has a size of {0}.", System::ExplicitCast<System::Object>(capacity)));
230 static void ThrowOutOfMemoryException(uint32_t capacity)
232 throw OutOfMemoryException(
System::String::Format(u
"Cannot allocate a buffer of size {0}.", System::ExplicitCast<System::Object>(capacity)));
237const int32_t ArrayBufferWriter<T>::ArrayMaxLength;
240const int32_t ArrayBufferWriter<T>::DefaultInitialBufferSize;
static void Resize(ArrayPtr< Type > &arr, int new_size)
Changes the size of the specified array to the specified value or crates new array with specified siz...
Definition: array.h:1518
Represents a heap-based, array-backed output sink into which data can be written.
Definition: array_buffer_writer.h:26
ArrayBufferWriter(int32_t initialCapacity)
Creates an instance of an ArrayBufferWriter, in which data can be written to, with an initial capacit...
Definition: array_buffer_writer.h:45
Span< T > GetSpan(int32_t sizeHint=0) override
Returns a Span to write to that is at least the requested length (specified by sizeHint)....
Definition: array_buffer_writer.h:153
void Advance(int32_t count) override
Notifies the writer that count amount of data was written to the output Span/Memory.
Definition: array_buffer_writer.h:115
ReadOnlyMemory< T > get_WrittenMemory() const
Returns the data written to the underlying buffer so far, as a ReadOnlyMemory.
Definition: array_buffer_writer.h:57
Memory< T > GetMemory(int32_t sizeHint=0) override
Returns a Memory to write to that is at least the requested length (specified by sizeHint)....
Definition: array_buffer_writer.h:138
ArrayBufferWriter()
Creates an instance of an ArrayBufferWriter, in which data can be written to, with the default initia...
Definition: array_buffer_writer.h:36
void Clear()
Clears the data written to the underlying buffer.
Definition: array_buffer_writer.h:95
int32_t get_WrittenCount() const
Returns the amount of data written to the underlying buffer so far.
Definition: array_buffer_writer.h:71
int32_t get_FreeCapacity() const
Returns the amount of space available that can still be written into without forcing the underlying b...
Definition: array_buffer_writer.h:85
void ResetWrittenCount()
Resets the data written to the underlying buffer without zeroing its content.
Definition: array_buffer_writer.h:105
int32_t get_Capacity() const
Returns the total amount of space within the underlying buffer.
Definition: array_buffer_writer.h:78
ReadOnlySpan< T > get_WrittenSpan() const
Returns the data written to the underlying buffer so far, as a ReadOnlySpan.
Definition: array_buffer_writer.h:64
Represents a sink for data, into which a consumer can write data. Objects of this class should only b...
Definition: i_buffer_writer.h:24
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:118
Represents a mutable memory segment backed by an array.
Definition: memory.h:159
Represents a read-only memory segment backed by an array.
Definition: memory.h:237
Forward to use within Span class.
Definition: span.h:415
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Represents a contiguous region of arbitrary memory similar to C++20's std::span.
Definition: span.h:375
static String Format(const SharedPtr< IFormatProvider > &fp, const String &format, const Args &... args)
Formats string in C# style.
Definition: string.h:1449
Span< T > AsSpan(const ArrayPtr< T > &array, int32_t start=0, int32_t length=-1)
Creates a span from an array.
Definition: memory_extensions.h:2051
ReadOnlyMemory< char16_t > AsMemory(const System::String &text)
Creates a read-only memory from a string.
Definition: db_command.h:9