CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
memory.h
1
3#pragma once
4
5#include <system/array.h>
6#include <system/span.h>
7#include <system/convert.h>
8#include <system/buffers/memory_handle.h>
9#include <system/buffers/memory_manager.h>
10
11namespace System {
12namespace Details {
13
16template <typename T>
17class ArrayMemoryManager : public Buffers::MemoryManager<T>
18{
19public:
22 ArrayMemoryManager(const ArrayPtr<T>& array) : m_array(array) {}
23
26 Span<T> GetSpan() override
27 {
28 return Span<T>(m_array);
29 }
30
33 Buffers::MemoryHandle Pin(int32_t elementIndex = 0) override
34 {
35 return Buffers::MemoryHandle(&m_array[elementIndex]);
36 }
37
39 void Unpin() override
40 {
41 }
42
43protected:
45 void Dispose(bool disposing) override
46 {
47 }
48
49private:
51 ArrayPtr<T> m_array;
52};
53
56template <typename T>
57class MemoryCore : public Details::BoxableObjectBase
58{
59public:
61 MemoryCore() : m_manager(nullptr), m_start(0), m_length(0)
62 {
63 }
64
67 MemoryCore(const ArrayPtr<T>& array) : MemoryCore(array, 0, array->get_Length())
68 {
69 }
70
75 MemoryCore(const ArrayPtr<T>& array, int32_t start, int32_t length)
76 : MemoryCore(System::MakeObject<ArrayMemoryManager<T>>(array), start, length)
77 {
78 }
79
83 MemoryCore(const SharedPtr<Buffers::MemoryManager<T>>& manager, int32_t length) : MemoryCore(manager, 0, length)
84 {
85 }
86
91 MemoryCore(const SharedPtr<Buffers::MemoryManager<T>>& manager, int32_t start, int32_t length)
92 : m_manager(manager), m_start(start), m_length(length)
93 {
94 }
95
98 int32_t get_Length() const
99 {
100 return m_length;
101 }
102
105 int32_t get_IsEmpty() const
106 {
107 return m_length == 0;
108 }
109
112 Buffers::MemoryHandle Pin()
113 {
114 return m_manager ? m_manager->Pin(m_start) : Buffers::MemoryHandle(nullptr);
115 }
116
119 ArrayPtr<T> ToArray() const
120 {
121 return m_manager != nullptr ? m_manager->GetSpan().Slice(m_start, m_length).ToArray() : nullptr;
122 }
123
127 bool Equals(const MemoryCore& other) const
128 {
129 return m_manager == other.m_manager && m_start == other.m_start && m_length == other.m_length;
130 }
131
134 bool GetHashCode() const
135 {
136 return m_manager ? System::ObjectExt::GetHashCode(m_manager) ^ (m_start << 2) ^ (m_length << 2) : 0;
137 }
138
139protected:
140 SharedPtr<Buffers::MemoryManager<T>> m_manager;
141 int32_t m_start;
142 int32_t m_length;
143};
144
145} // namespace Details
146
147template <typename T>
148class ReadOnlyMemory;
149
152template <typename T>
153class Memory : public Details::MemoryCore<T>
154{
155public:
156 using Details::MemoryCore<T>::MemoryCore;
157
161 {
162 return this->m_manager ? this->m_manager->GetSpan().Slice(this->m_start, this->m_length) : Span<T>();
163 }
164
168 {
169 return Memory();
170 }
171
174 void CopyTo(const Memory& destination)
175 {
176 get_Span().CopyTo(destination.get_Span());
177 }
178
182 bool TryCopyTo(const Memory& destination)
183 {
184 return get_Span().TryCopyTo(destination.get_Span());
185 }
186
191 Memory Slice(int32_t start, int32_t length) const
192 {
193 return Memory(this->m_manager, this->m_start + start, length);
194 }
195
198 operator ReadOnlyMemory<T>() const
199 {
200 return ReadOnlyMemory<T>(this->m_manager, this->m_start, this->m_length);
201 }
202
205 template <typename T1 = T>
206 typename std::enable_if<std::is_same<T1, char16_t>::value, String>::type ToString() const
207 {
208 return System::String(this->ToArray());
209 }
210
213 template <typename T1 = T>
214 typename std::enable_if<!std::is_same<T1, char16_t>::value, String>::type ToString() const
215 {
216 return u"System::Memory<T>[" + System::Convert::ToString(this->m_length) + u"]";
217 }
218
222 static Memory to_Memory(const ArrayPtr<T>& array)
223 {
224 return Memory(array);
225 }
226};
227
230template <typename T>
231class ReadOnlyMemory : public Details::MemoryCore<T>
232{
233public:
234 using Details::MemoryCore<T>::MemoryCore;
235
239 {
240 return this->m_manager ? ReadOnlySpan<T>(this->m_manager->GetSpan().Slice(this->m_start, this->m_length))
241 : ReadOnlySpan<T>();
242 }
243
247 {
248 return ReadOnlyMemory();
249 }
250
255 ReadOnlyMemory Slice(int32_t start, int32_t length) const
256 {
257 return ReadOnlyMemory(this->m_manager, this->m_start + start, length);
258 }
259
262 template <typename T1 = T>
263 typename std::enable_if<std::is_same<T1, char16_t>::value, String>::type ToString() const
264 {
265 return System::String(this->ToArray());
266 }
267
270 template <typename T1 = T>
271 typename std::enable_if<!std::is_same<T1, char16_t>::value, String>::type ToString() const
272 {
273 return u"System::ReadOnlyMemory<T>[" + System::Convert::ToString(this->m_length) + u"]";
274 }
275
280 {
281 return ReadOnlyMemory(array);
282 }
283};
284
285} // namespace System
virtual MemoryHandle Pin(int32_t elementIndex=0)=0
Returns a handle to the memory that has been pinned so its address can be taken.
virtual void Unpin()=0
Indicates the memory can be moved again; unpins previously pinned memory.
void Dispose() override
Disposes the memory manager and releases any resources it holds.
Definition: memory_manager.h:43
virtual Span< T > GetSpan()=0
Returns a span wrapping the underlying memory.
Represents a mutable memory segment backed by an array.
Definition: memory.h:154
Memory Slice(int32_t start, int32_t length) const
Creates a slice of the current memory.
Definition: memory.h:191
std::enable_if< std::is_same< T1, char16_t >::value, String >::type ToString() const
Converts the character memory to a string representation.
Definition: memory.h:206
std::enable_if<!std::is_same< T1, char16_t >::value, String >::type ToString() const
Converts the ordinary memory to a string representation.
Definition: memory.h:214
void CopyTo(const Memory &destination)
Copies the contents of this memory to the destination memory.
Definition: memory.h:174
Span< T > get_Span() const
Gets a span that represents the current memory.
Definition: memory.h:160
static Memory get_Empty()
Gets an empty Memory instance.
Definition: memory.h:167
bool TryCopyTo(const Memory &destination)
Attempts to copy the contents of this memory to the destination memory.
Definition: memory.h:182
static Memory to_Memory(const ArrayPtr< T > &array)
Creates a Memory instance from the specified array.
Definition: memory.h:222
static int GetHashCode(const T &obj)
Implements GetHashCode() calls; works on both Object subclasses and unrelated types.
Definition: object_ext.h:30
Represents a read-only memory segment backed by an array.
Definition: memory.h:232
static ReadOnlyMemory get_Empty()
Gets an empty ReadOnlyMemory instance.
Definition: memory.h:246
ReadOnlyMemory Slice(int32_t start, int32_t length) const
Creates a slice of the current readonly memory.
Definition: memory.h:255
std::enable_if< std::is_same< T1, char16_t >::value, String >::type ToString() const
Converts the character read-only memory to a string representation.
Definition: memory.h:263
ReadOnlySpan< T > get_Span() const
Gets a read-only span that represents the current memory.
Definition: memory.h:238
static ReadOnlyMemory to_ReadOnlyMemory(const ArrayPtr< T > &array)
Creates a ReadOnlyMemory instance from the specified array.
Definition: memory.h:279
std::enable_if<!std::is_same< T1, char16_t >::value, String >::type ToString() const
Converts the ordinary read-only memory to a string representation.
Definition: memory.h:271
Forward to use within Span class.
Definition: span.h:404
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:364
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:125
Definition: db_command.h:9
bool Equals(const TA &a, const TB &b)
Determines the equality of two values applying operator==() to them.
Definition: primitive_types.h:77
std::enable_if<!IsSmartPtr< T >::value, SmartPtr< T > >::type MakeObject(Args &&... args)
Creates object on heap and returns shared pointer to it.
Definition: smart_ptr.h:1506
SmartPtr< T > SharedPtr
Alias for smart pointer widely used in the library.
Definition: smart_ptr.h:1798
std::enable_if< std::is_scalar< T >::value, int >::type GetHashCode(const T &obj)
Returns a hash code for the specified scalar value.
Definition: get_hash_code.h:21
static String ToString(int8_t value)
Converts the specified value to its string representation.