6#ifndef ACTIVE_UTILITY_MEMORY
7#define ACTIVE_UTILITY_MEMORY
9#include "Active/Utility/Cloner.h"
21 inline static constexpr bool defaultEndian{std::endian::native == std::endian::big};
30 using Option = std::optional<Memory>;
32 using Unique = std::unique_ptr<Memory>;
34 using Shared = std::shared_ptr<Memory>;
50 static void erase(T& target) {
fill(&target,
sizeof(target)); }
65 template<
typename T>
requires (std::is_integral_v<T>)
78 template<
typename T>
requires (std::is_integral_v<T>)
90 template<
typename T>
requires (std::is_integral_v<T>)
92 auto*
data =
reinterpret_cast<unsigned char*
>(&val);
93 auto bytes =
sizeof(T);
94 for (
auto i = bytes-- / 2; i--; )
103 template<
typename T>
requires (std::is_integral_v<T>)
105 if ((howMany < 1) || (
toBigEndian == defaultEndian))
107 for (; howMany--; ++val) {
108 auto data =
reinterpret_cast<unsigned char*
>(val);
109 auto bytes =
sizeof(T);
110 for (
auto i = bytes-- / 2; i--; )
111 std::swap(
data[i],
data[bytes - i]);
127 template<
typename T>
requires (
sizeof(T) > 1) && (!std::is_pointer<T>())
128 Memory(T& buffer,
bool makeCopy =
false,
bool takeOwnership =
false) :
Memory{&buffer,
sizeof(buffer), makeCopy, takeOwnership} {}
172 operator bool()
const {
return !
empty(); }
192 char*
data()
const {
return m_location; }
197 size_type size()
const {
return (m_location ==
nullptr) ? 0 : m_size.value_or(m_allocSize); }
207 bool owned()
const {
return m_store.operator bool() || (m_location ==
nullptr); }
215 char*
data() {
return m_location; }
241 void clear(
bool isReleased =
true);
256 Memory& reallocate(
size_type size, std::optional<char> fillChar = std::nullopt,
bool retainExisting =
true);
259 char* m_location =
nullptr;
265 std::unique_ptr<char[]> m_store;
Class representing (and optionally allocating) memory with a specified location and size.
Definition Memory.h:18
Memory()
Definition Memory.h:120
char * data() const
Definition Memory.h:192
Memory & append(const Memory &source, size_type startPos=0, std::optional< size_type > howMany=std::nullopt)
Definition Memory.cpp:178
void clear(bool isReleased=true)
Definition Memory.cpp:196
std::unique_ptr< Memory > Unique
Unique pointer.
Definition Memory.h:32
void fill(char fillChar=0)
char operator[](size_type index) const
Definition Memory.cpp:134
std::optional< size_type > sizeOption
Optional memory size/position.
Definition Memory.h:28
Memory(T &buffer, bool makeCopy=false, bool takeOwnership=false)
Definition Memory.h:128
bool empty() const
Definition Memory.h:202
Memory * clonePtr() const override
Definition Memory.h:152
char * release()
Definition Memory.cpp:211
static void erase(T &target)
Definition Memory.h:50
bool owned() const
Definition Memory.h:207
std::optional< Memory > Option
Unique pointer.
Definition Memory.h:30
Memory & resize(size_type newSize, std::optional< char > fillChar=std::nullopt)
Definition Memory.cpp:163
size_type size() const
Definition Memory.h:197
std::size_t size_type
Memory size/position type.
Definition Memory.h:26
Memory & operator=(const Memory &source)
Definition Memory.cpp:95
static void byteSwap(T &val)
Definition Memory.h:91
static T fromBigEndian(T val)
Definition Memory.h:79
static T toBigEndian(T val)
Definition Memory.h:66
static void fill(void *start, size_type size, char fillChar=0)
Definition Memory.cpp:22
char * data()
Definition Memory.h:215
std::shared_ptr< Memory > Shared
Shared pointer.
Definition Memory.h:34
static size_type copy(char *dest, char *source, size_type destSize, size_type sourceSize)
Definition Memory.cpp:37
static void byteSwap(T *val, size_type howMany, bool toBigEndian)
Definition Memory.h:104
Definition Base64Transport.h:11