CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
buffers_extensions.h
1
3#pragma once
4
5#include <cstdint>
6#include <algorithm>
7#include <system/object.h>
8#include <system/shared_ptr.h>
9#include <system/array.h>
10#include <system/span.h>
11#include <system/memory.h>
12#include <system/sequence_position.h>
13#include <system/buffers/read_only_sequence.h>
14
15namespace System {
16namespace Buffers {
17
20{
21public:
26 template <typename T>
27 static void CopyTo(const ReadOnlySequence<T>& source, Span<T> destination)
28 {
29 SequencePosition position = source.get_Start();
30 int32_t index = 0;
31 int32_t destLength = destination.get_Length();
32
33 ReadOnlyMemory<T> memory;
34 while (index < destLength && source.TryGet(position, memory, true))
35 {
36 ReadOnlySpan<T> span = memory.get_Span();
37 int32_t count = (std::min)(span.get_Length(), destLength - index);
38 if (count > 0)
39 {
40 span.Slice(0, count).CopyTo(destination.Slice(index));
41 index += count;
42 }
43 }
44 }
45
50 template <typename T>
52 {
53 int64_t length = sequence.get_Length();
54 SharedPtr<Array<T>> result = System::MakeObject<Array<T>>(System::CheckedCast<int32_t>(length));
55 CopyTo(sequence, Span<T>(result, 0, result->get_Length()));
56 return result;
57 }
58};
59
60} // namespace Buffers
61} // namespace System
62
Provides extension-style helper methods for working with buffers.
Definition: buffers_extensions.h:20
static SharedPtr< Array< T > > ToArray(const ReadOnlySequence< T > &sequence)
Copies the elements of the given ReadOnlySequence to a newly allocated array.
Definition: buffers_extensions.h:51
static void CopyTo(const ReadOnlySequence< T > &source, Span< T > destination)
Copies the elements of the given ReadOnlySequence to the destination span.
Definition: buffers_extensions.h:27
Represents a sequence of elements that can be backed either by a single contiguous memory block or by...
Definition: read_only_sequence.h:92
int64_t get_Length() const
Gets the number of elements in the sequence.
Definition: read_only_sequence.h:152
bool TryGet(SequencePosition &position, ReadOnlyMemory< T > &memory, bool advance) const
Retrieves the memory block that contains the given position.
Definition: read_only_sequence.h:325
SequencePosition get_Start() const
Gets the position of the first element in the sequence.
Definition: read_only_sequence.h:198
Represents a read-only memory segment backed by an array.
Definition: memory.h:237
ReadOnlySpan< T > get_Span() const
Gets a read-only span that represents the current memory.
Definition: memory.h:243
Forward to use within Span class.
Definition: span.h:415
Represents the position of an element in a sequence. It wraps a reference to a segment object (usuall...
Definition: sequence_position.h:17
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
Definition: db_command.h:9