ActiveLib
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
active::utility::Memory Class Reference

Class representing (and optionally allocating) memory with a specified location and size. More...

#include <Memory.h>

Inheritance diagram for active::utility::Memory:
Inheritance graph
[legend]
Collaboration diagram for active::utility::Memory:
Collaboration graph
[legend]

Public Types

using size_type = std::size_t
 Memory size/position type.
 
using sizeOption = std::optional<size_type>
 Optional memory size/position.
 
using Option = std::optional<Memory>
 Unique pointer.
 
using Unique = std::unique_ptr<Memory>
 Unique pointer.
 
using Shared = std::shared_ptr<Memory>
 Shared pointer.
 

Public Member Functions

 Memory ()
 
template<typename T >
requires (sizeof(T) > 1) && (!std::is_pointer<T>())
 Memory (T &buffer, bool makeCopy=false, bool takeOwnership=false)
 
 Memory (void *location, size_type size, bool makeCopy=false, bool takeOwnership=false)
 
 Memory (const Memory &source)
 
 Memory (Memory &&source) noexcept
 
MemoryclonePtr () const override
 
Memoryoperator= (const Memory &source)
 
Memoryoperator= (Memory &&source) noexcept
 
 operator bool () const
 
char operator[] (size_type index) const
 
char & operator[] (size_type index)
 
char * data () const
 
size_type size () const
 
bool empty () const
 
bool owned () const
 
char * data ()
 
Memoryresize (size_type newSize, std::optional< char > fillChar=std::nullopt)
 
Memoryappend (const Memory &source, size_type startPos=0, std::optional< size_type > howMany=std::nullopt)
 
void fill (char fillChar=0)
 
void clear (bool isReleased=true)
 
char * release ()
 
- Public Member Functions inherited from active::utility::Cloner
virtual ~Cloner ()=default
 

Static Public Member Functions

static void fill (void *start, size_type size, char fillChar=0)
 
template<typename T >
static void erase (T &target)
 
static size_type copy (char *dest, char *source, size_type destSize, size_type sourceSize)
 
template<typename T >
requires (std::is_integral_v<T>)
static T toBigEndian (T val)
 
template<typename T >
requires (std::is_integral_v<T>)
static T fromBigEndian (T val)
 
template<typename T >
requires (std::is_integral_v<T>)
static void byteSwap (T &val)
 
template<typename T >
requires (std::is_integral_v<T>)
static void byteSwap (T *val, size_type howMany, bool toBigEndian)
 

Static Public Attributes

static constexpr bool defaultEndian {std::endian::native == std::endian::big}
 

Detailed Description

Class representing (and optionally allocating) memory with a specified location and size.

Constructor & Destructor Documentation

◆ Memory() [1/5]

active::utility::Memory::Memory ( )
inline

Default constructor

◆ Memory() [2/5]

template<typename T >
requires (sizeof(T) > 1) && (!std::is_pointer<T>())
active::utility::Memory::Memory ( T & buffer,
bool makeCopy = false,
bool takeOwnership = false )
inline

Constructor

Parameters
bufferA buffer (where the length can be established with sizeof(T)
makeCopyTrue to make a private (owned) copy of the data
takeOwnershipTrue for this object to take ownership of the data (must be allocated with new char[] - ignored if makeCopy == true)

◆ Memory() [3/5]

Memory::Memory ( void * location,
size_type size,
bool makeCopy = false,
bool takeOwnership = false )

Constructor

Parameters
locationA pointer to the data location
sizeThe length of the data source in bytes (0 = null-terminated string)
makeCopyTrue to make a private (owned) copy of the data
takeOwnershipTrue for this object to take ownership of the data (must be allocated with new char[] - ignored if makeCopy == true)
Here is the call graph for this function:

◆ Memory() [4/5]

Memory::Memory ( const Memory & source)

Copy constructor

Parameters
sourceThe object to copy

◆ Memory() [5/5]

Memory::Memory ( Memory && source)
noexcept

Move constructor

Parameters
sourceThe object to move

Member Function Documentation

◆ append()

Memory & Memory::append ( const Memory & source,
size_type startPos = 0,
std::optional< size_type > howMany = std::nullopt )

Append data to the memory allocation. NB: Makes a private copy of unowned data

Parameters
sourceThe data source
startPosThe start position of the data to append
howManyThe number of bytes to append from the source (nullopt = to end)
Returns
A reference to this
Here is the call graph for this function:

◆ byteSwap() [1/2]

template<typename T >
requires (std::is_integral_v<T>)
static void active::utility::Memory::byteSwap ( T & val)
inlinestatic

Byte-swap an integer value (reverse the order of bytes to change endianess)

Parameters
valThe target value
Here is the call graph for this function:
Here is the caller graph for this function:

◆ byteSwap() [2/2]

template<typename T >
requires (std::is_integral_v<T>)
static void active::utility::Memory::byteSwap ( T * val,
size_type howMany,
bool toBigEndian )
inlinestatic

Byte-swap an array of integer values (in the native byte-order)

Parameters
valA pointer to the array start
howManyHow many values are in the array
toBigEndianTrue if the end result should be big-endian
Here is the call graph for this function:

◆ clear()

void Memory::clear ( bool isReleased = true)

Clear data copied to this memory allocation

Parameters
isReleasedTrue to release any memory allocation held by the object

◆ clonePtr()

Memory * active::utility::Memory::clonePtr ( ) const
inlineoverridevirtual

Clone method

Returns
A clone of this object

Implements active::utility::Cloner.

◆ copy()

Memory::size_type Memory::copy ( char * dest,
char * source,
Memory::size_type destSize,
Memory::size_type sourceSize )
static

Copy memory from source to destination memory

Parameters
destThe destination memory
sourceThe source memory
destSizeThe number of bytes to copy into the destination (can be less than the total destination allocation)
sourceSizeThe source memory size
Returns
The number of bytes copied
Here is the caller graph for this function:

◆ data() [1/2]

char * active::utility::Memory::data ( )
inline

Get the location of the allocated memory

Returns
The allocated memory location (writable)

◆ data() [2/2]

char * active::utility::Memory::data ( ) const
inline

Get the location of the allocated memory

Returns
The allocated memory location
Here is the caller graph for this function:

◆ empty()

bool active::utility::Memory::empty ( ) const
inline

Determine if the memory allocation is empty

Returns
True if the memory allocation is empty (size() == 0 or location == nullptr)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ erase()

template<typename T >
static void active::utility::Memory::erase ( T & target)
inlinestatic

Erase the memory of a target data structure (reset content to 0)

Parameters
targetThe target data structure
Here is the call graph for this function:

◆ fill() [1/2]

void active::utility::Memory::fill ( char fillChar = 0)

Fill allocated memory with a specified character

Parameters
fillCharThe character to fill the allocated memory with (0 by default)

◆ fill() [2/2]

void Memory::fill ( void * start,
size_type size,
char fillChar = 0 )
static

Fill memory with a specified character

Parameters
startThe memory start
sizeThe memory size
fillCharThe char to fill the memory with
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromBigEndian()

template<typename T >
requires (std::is_integral_v<T>)
static T active::utility::Memory::fromBigEndian ( T val)
inlinestatic

Make a big-endian integer value suited to the host processor (no action if the host processor is already big-endian)

Parameters
valThe target value
Returns
The integer suited to the host processor
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator bool()

active::utility::Memory::operator bool ( ) const
inline

Conversion operator

Returns
True if the memory allocation is not empty
Here is the call graph for this function:

◆ operator=() [1/2]

Memory & Memory::operator= ( const Memory & source)

Assignment operator

Parameters
sourceThe object to be copied (a complete copy of the allocated memory will be made)
Returns
A reference to this object

◆ operator=() [2/2]

Memory & Memory::operator= ( Memory && source)
noexcept

Assignment with move operator

Parameters
sourceThe object to move
Returns
A reference to this object

◆ operator[]() [1/2]

char & Memory::operator[] ( size_type index)

Subscript operator

Parameters
indexIndex of the byte to access
Returns
A reference to this object
Here is the call graph for this function:

◆ operator[]() [2/2]

char Memory::operator[] ( size_type index) const

Subscript operator

Parameters
indexIndex of the byte to access
Returns
A reference to this object
Here is the call graph for this function:

◆ owned()

bool active::utility::Memory::owned ( ) const
inline

Determine if the memory is owned by this object

Returns
True if the memory is owned by this object (otherwise it points to a fixed-length allocation)
Here is the caller graph for this function:

◆ release()

char * Memory::release ( )

Release ownership of the memory allocation to the caller (this will become empty)

Returns
The allocated memory location (caller takes ownership - nullptr returned if this object doesn't own the allocation)

◆ resize()

Memory & Memory::resize ( Memory::size_type size,
std::optional< char > fillChar = std::nullopt )

Resize the memory allocation. NB: Makes a private copy of unowned data

Parameters
newSizeThe new allocation size
fillCharOptional character to fill additional allocated memory with (nullopt = don't fill)
Returns
A reference to this
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

size_type active::utility::Memory::size ( ) const
inline

Get the size of the allocated memory

Returns
The size of the allocated memory
Here is the caller graph for this function:

◆ toBigEndian()

template<typename T >
requires (std::is_integral_v<T>)
static T active::utility::Memory::toBigEndian ( T val)
inlinestatic

Make an integer value big-endian (no action if the host processor is already big-endian)

Parameters
valThe target value
Returns
The integer as big-endian
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: