ActiveLib
Loading...
Searching...
No Matches
Memory.h
1
6#ifndef ACTIVE_UTILITY_MEMORY
7#define ACTIVE_UTILITY_MEMORY
8
9#include "Active/Utility/Cloner.h"
10
11#include <bit>
12#include <memory>
13#include <optional>
14
15namespace active::utility {
16
18 class Memory: public utility::Cloner {
19 public:
20
21 inline static constexpr bool defaultEndian{std::endian::native == std::endian::big};
22
23 // MARK: - Types
24
26 using size_type = std::size_t;
28 using sizeOption = std::optional<size_type>;
30 using Option = std::optional<Memory>;
32 using Unique = std::unique_ptr<Memory>;
34 using Shared = std::shared_ptr<Memory>;
35
36 // MARK: - Static functions
37
44 static void fill(void* start, size_type size, char fillChar = 0);
49 template<typename T>
50 static void erase(T& target) { fill(&target, sizeof(target)); }
59 static size_type copy(char* dest, char* source, size_type destSize, size_type sourceSize);
65 template<typename T> requires (std::is_integral_v<T>)
66 static T toBigEndian(T val) {
67 //Return the given value unchanged if the processor is big-endian
68 if (defaultEndian)
69 return val;
70 byteSwap(val);
71 return val;
72 }
78 template<typename T> requires (std::is_integral_v<T>)
79 static T fromBigEndian(T val) {
80 //Return the given value unchanged if the processor is big-endian
81 if (defaultEndian)
82 return val;
83 byteSwap(val);
84 return val;
85 }
90 template<typename T> requires (std::is_integral_v<T>)
91 static void byteSwap(T& val) {
92 auto* data = reinterpret_cast<unsigned char*>(&val);
93 auto bytes = sizeof(T);
94 for (auto i = bytes-- / 2; i--; )
95 std::swap(data[i], data[bytes - i]);
96 }
103 template<typename T> requires (std::is_integral_v<T>)
104 static void byteSwap(T* val, size_type howMany, bool toBigEndian) {
105 if ((howMany < 1) || (toBigEndian == defaultEndian))
106 return;
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]);
112 }
113 }
114
115 // MARK: - Constructors
116
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} {}
136 Memory(void* location, size_type size, bool makeCopy = false, bool takeOwnership = false);
141 Memory(const Memory& source);
146 Memory(Memory&& source) noexcept;
147
152 Memory* clonePtr() const override { return new Memory{*this}; }
153
154 // MARK: - Operators
155
161 Memory& operator= (const Memory& source);
167 Memory& operator= (Memory&& source) noexcept;
172 operator bool() const { return !empty(); }
178 char operator[] (size_type index) const;
184 char& operator[] (size_type index);
185
186 // MARK: - Functions (const)
187
192 char* data() const { return m_location; }
197 size_type size() const { return (m_location == nullptr) ? 0 : m_size.value_or(m_allocSize); }
202 bool empty() const { return (size() == 0); }
207 bool owned() const { return m_store.operator bool() || (m_location == nullptr); }
208
209 // MARK: - Functions (mutating)
210
215 char* data() { return m_location; }
216
223 Memory& resize(size_type newSize, std::optional<char> fillChar = std::nullopt);
231 Memory& append(const Memory& source, size_type startPos = 0, std::optional<size_type> howMany = std::nullopt);
236 void fill(char fillChar = 0);
241 void clear(bool isReleased = true);
246 char* release();
247
248 private:
256 Memory& reallocate(size_type size, std::optional<char> fillChar = std::nullopt, bool retainExisting = true);
257
259 char* m_location = nullptr;
261 sizeOption m_size;
263 size_type m_allocSize = 0;
265 std::unique_ptr<char[]> m_store;
266 };
267
268}
269
270#endif //ACTIVE_UTILITY_MEMORY
Definition Cloner.h:17
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