CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
array_buffer_writer.h
1
3#pragma once
4
5#include <algorithm>
6#include <cstdint>
7
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>
16
17#include "system/buffers/i_buffer_writer.h"
18
19namespace System {
20namespace Buffers {
21
24template <typename T>
25class ArrayBufferWriter final : public IBufferWriter<T>
26{
29
30 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
31 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo)
32
33public:
36 ArrayBufferWriter() : m_index(0)
37 {
38 m_buffer = System::MakeArray<T>(0);
39 }
40
45 explicit ArrayBufferWriter(int32_t initialCapacity) : m_index(0)
46 {
47 if (initialCapacity <= 0)
48 {
49 throw ArgumentException(nullptr, u"initialCapacity");
50 }
51
52 m_buffer = System::MakeArray<T>(initialCapacity);
53 }
54
58 {
59 return MemoryExtensions::AsMemory(m_buffer, 0, m_index);
60 }
61
65 {
66 return MemoryExtensions::AsSpan(m_buffer, 0, m_index);
67 }
68
71 int32_t get_WrittenCount() const
72 {
73 return m_index;
74 }
75
78 int32_t get_Capacity() const
79 {
80 return m_buffer->get_Length();
81 }
82
85 int32_t get_FreeCapacity() const
86 {
87 return m_buffer->get_Length() - m_index;
88 }
89
95 void Clear()
96 {
97 CODEPORTING_DEBUG_ASSERT2(m_buffer->get_Length() >= m_index, u"Invalid buffer state");
98 MemoryExtensions::AsSpan(m_buffer, 0, m_index).Clear();
99 m_index = 0;
100 }
101
106 {
107 m_index = 0;
108 }
109
115 void Advance(int32_t count) override
116 {
117 if (count < 0)
118 {
119 throw ArgumentException(nullptr, u"count");
120 }
121
122 if (m_index > m_buffer->get_Length() - count)
123 {
124 ThrowInvalidOperationException_AdvancedTooFar(m_buffer->get_Length());
125 }
126
127 m_index += count;
128 }
129
138 Memory<T> GetMemory(int32_t sizeHint = 0) override
139 {
140 CheckAndResizeBuffer(sizeHint);
141 CODEPORTING_DEBUG_ASSERT2(m_buffer->get_Length() > m_index, u"Invalid buffer state");
142 return MemoryExtensions::AsMemory(m_buffer, m_index);
143 }
144
153 Span<T> GetSpan(int32_t sizeHint = 0) override
154 {
155 CheckAndResizeBuffer(sizeHint);
156 CODEPORTING_DEBUG_ASSERT2(m_buffer->get_Length() > m_index, u"Invalid buffer state");
157 return MemoryExtensions::AsSpan(m_buffer, m_index);
158 }
159
160protected:
161
162 #ifdef ASPOSE_GET_SHARED_MEMBERS
163 void GetSharedMembers(System::Object::shared_members_type& result) const override
164 {
165 Object::GetSharedMembers(result);
166 result.Add("System::Buffers::ArrayBufferWriter::m_buffer", this->m_buffer);
167 }
168 #endif
169
170private:
173 static const int32_t ArrayMaxLength = 0x7FFFFFC7;
174
176 static const int32_t DefaultInitialBufferSize = 256;
177
178 ArrayPtr<T> m_buffer;
179 int32_t m_index;
180
181 void CheckAndResizeBuffer(int32_t sizeHint)
182 {
183 if (sizeHint < 0)
184 {
185 throw ArgumentException(nullptr, u"sizeHint");
186 }
187
188 if (sizeHint == 0)
189 {
190 sizeHint = 1;
191 }
192
193 if (sizeHint > get_FreeCapacity())
194 {
195 int32_t currentLength = m_buffer->get_Length();
196
197 int32_t growBy = std::max(sizeHint, currentLength);
198
199 if (currentLength == 0)
200 {
201 growBy = std::max(growBy, DefaultInitialBufferSize);
202 }
203
204 int32_t newSize = currentLength + growBy;
205
206 if (static_cast<uint32_t>(newSize) > static_cast<uint32_t>(ArrayMaxLength))
207 {
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");
210
211 if (needed > static_cast<uint32_t>(ArrayMaxLength))
212 {
213 ThrowOutOfMemoryException(needed);
214 }
215
216 newSize = ArrayMaxLength;
217 }
218
219 System::Array<T>::Resize(m_buffer, newSize);
220 }
221
222 CODEPORTING_DEBUG_ASSERT2(get_FreeCapacity() > 0 && get_FreeCapacity() >= sizeHint, u"Invalid buffer state");
223 }
224
225 static void ThrowInvalidOperationException_AdvancedTooFar(int32_t capacity)
226 {
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)));
228 }
229
230 static void ThrowOutOfMemoryException(uint32_t capacity)
231 {
232 throw OutOfMemoryException(System::String::Format(u"Cannot allocate a buffer of size {0}.", System::ExplicitCast<System::Object>(capacity)));
233 }
234};
235
236template<typename T>
237const int32_t ArrayBufferWriter<T>::ArrayMaxLength;
238
239template<typename T>
240const int32_t ArrayBufferWriter<T>::DefaultInitialBufferSize;
241
242} // namespace Buffers
243} // namespace System
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