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>
17class ArrayMemoryManager :
public Buffers::MemoryManager<T>
22 ArrayMemoryManager(
const ArrayPtr<T>& array) : m_array(array) {}
28 return Span<T>(m_array);
33 Buffers::MemoryHandle
Pin(int32_t elementIndex = 0)
override
35 return Buffers::MemoryHandle(&m_array[elementIndex]);
45 void Dispose(
bool disposing)
override
57class MemoryCore :
public Details::BoxableObjectBase
61 MemoryCore() : m_manager(nullptr), m_start(0), m_length(0)
67 MemoryCore(
const ArrayPtr<T>& array) : MemoryCore(array, 0, array->get_Length())
75 MemoryCore(
const ArrayPtr<T>& array, int32_t start, int32_t length)
83 MemoryCore(
const SharedPtr<Buffers::MemoryManager<T>>& manager, int32_t length) : MemoryCore(manager, 0, length)
91 MemoryCore(
const SharedPtr<Buffers::MemoryManager<T>>& manager, int32_t start, int32_t length)
92 : m_manager(manager), m_start(start), m_length(length)
98 int32_t get_Length()
const
105 int32_t get_IsEmpty()
const
107 return m_length == 0;
112 Buffers::MemoryHandle Pin()
114 return m_manager ? m_manager->Pin(m_start) : Buffers::MemoryHandle(
nullptr);
119 ArrayPtr<T> ToArray()
const
121 return m_manager !=
nullptr ? m_manager->GetSpan().Slice(m_start, m_length).ToArray() :
nullptr;
127 bool Equals(
const MemoryCore& other)
const
129 return m_manager == other.m_manager && m_start == other.m_start && m_length == other.m_length;
140 SharedPtr<Buffers::MemoryManager<T>> m_manager;
153class Memory :
public Details::MemoryCore<T>
156 using Details::MemoryCore<T>::MemoryCore;
162 return this->m_manager ? this->m_manager->GetSpan().Slice(this->m_start, this->m_length) :
Span<T>();
193 return Memory(this->m_manager, this->m_start + start, length);
205 template <
typename T1 = T>
206 typename std::enable_if<std::is_same<T1, char16_t>::value,
String>::type
ToString()
const
213 template <
typename T1 = T>
214 typename std::enable_if<!std::is_same<T1, char16_t>::value,
String>::type
ToString()
const
234 using Details::MemoryCore<T>::MemoryCore;
240 return this->m_manager ?
ReadOnlySpan<T>(this->m_manager->GetSpan().Slice(this->m_start, this->m_length))
257 return ReadOnlyMemory(this->m_manager, this->m_start + start, length);
262 template <
typename T1 = T>
263 typename std::enable_if<std::is_same<T1, char16_t>::value,
String>::type
ToString()
const
270 template <
typename T1 = T>
271 typename std::enable_if<!std::is_same<T1, char16_t>::value,
String>::type
ToString()
const
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.