Motion Master
Loading...
Searching...
No Matches
Classes | Enumerations | Functions
mm::core::util Namespace Reference

Classes

class  ThreadSafeQueue
 A simple thread-safe FIFO queue. More...
 

Enumerations

enum class  Endianness { LITTLE , BIG }
 Specifies the byte order used when converting a byte sequence to an integer. More...
 

Functions

std::vector< std::vector< uint8_t > > splitIntoParts (const std::vector< uint8_t > &input, size_t partSize)
 Splits a byte vector into parts of a specified maximum size. More...
 
std::vector< uint8_t > zipData (const std::string &filename, const std::vector< uint8_t > &data)
 Compresses a block of data into a ZIP archive containing a single file. More...
 
std::unordered_map< std::string, std::vector< uint8_t > > unzipData (const std::vector< uint8_t > &data)
 Unzips a ZIP archive from an in-memory byte buffer. More...
 
std::vector< uint8_t > readBinaryFile (const std::string &filename)
 Reads the entire contents of a binary file into a byte vector. More...
 
std::string readTextFile (const std::string &filename)
 Reads the entire content of a text file into a std::string. More...
 
std::string joinStrings (const std::vector< std::string > &list, const std::string &delimiter)
 Joins a list of strings into a single string with a delimiter. More...
 
std::string formatMacAddress (const std::string &originalMacAddress)
 Formats a MAC address string to ensure each component is two digits and uppercase. More...
 
std::string extractIpAddress (const std::string &socketAddress)
 Extracts the IP address from a socket address string. More...
 
unsigned short extractPort (const std::string &socketAddress)
 Extracts the port number from a socket address string. More...
 
template<typename T >
parseHex (const std::string &s)
 Parses a hexadecimal string into an unsigned integer of type T. More...
 
template<typename T >
toInteger (const std::vector< std::uint8_t > &data, std::size_t offset=0, Endianness endian=Endianness::LITTLE)
 Converts a byte sequence to an integer of type T with optional offset and endianness. More...
 
template<typename T >
bool stringViewToNumber (std::string_view sv, T &result)
 Converts a string_view to a numeric type. More...
 
template<typename T >
std::vector< uint8_t > toBytes (T value, bool bigEndian=false)
 Converts a trivially copyable value to a byte vector. More...
 
template<typename T >
fromBytes (const std::vector< uint8_t > &bytes, bool bigEndian=false)
 Reconstructs a value of type T from a byte vector. More...
 
std::string makeParameterId (int index, int subindex)
 Formats the given index and subindex into a parameter identifier string. More...
 
std::string toHex (const std::vector< uint8_t > &data)
 Converts a vector of bytes to a space-separated hexadecimal string. More...
 
template<typename T >
std::string toHex (T value)
 Converts an integer value to its hexadecimal string representation. More...
 
bool endsWith (const std::string &str, const std::string &suffix)
 Checks whether a string ends with the given suffix. More...
 
size_t countStringsStartingWith (const std::vector< std::string > &strings, const std::string &prefix)
 Count how many strings in the list start with the given prefix. More...
 
std::vector< std::string > split (const std::string &input, char delimiter)
 Splits a string into substrings based on a delimiter character. More...
 
int64_t currentTimeMillis ()
 Gets the current system time in milliseconds since the Unix epoch. More...
 
template<typename T >
constexpr T linearScale (T value, T inMin, T inMax, T outMin, T outMax)
 

Enumeration Type Documentation

◆ Endianness

enum class mm::core::util::Endianness
strong

Specifies the byte order used when converting a byte sequence to an integer.

This enum is used to indicate whether the byte sequence should be interpreted in little-endian or big-endian format.

  • Little: Least significant byte comes first.
  • Big: Most significant byte comes first.
Enumerator
LITTLE 
BIG 

Function Documentation

◆ countStringsStartingWith()

size_t mm::core::util::countStringsStartingWith ( const std::vector< std::string > &  strings,
const std::string &  prefix 
)
inline

Count how many strings in the list start with the given prefix.

Parameters
stringsThe vector of strings to search.
prefixThe prefix to match.
Returns
Number of strings starting with the given prefix.

◆ currentTimeMillis()

int64_t mm::core::util::currentTimeMillis ( )
inline

Gets the current system time in milliseconds since the Unix epoch.

Uses std::chrono to obtain the current time point from the system clock, converts it to milliseconds duration since epoch, and returns the count as a 64-bit integer.

Returns
int64_t Current time in milliseconds since 1970-01-01 00:00:00 UTC.

◆ endsWith()

bool mm::core::util::endsWith ( const std::string &  str,
const std::string &  suffix 
)
inline

Checks whether a string ends with the given suffix.

Parameters
strThe full string to check.
suffixThe suffix to compare against.
Returns
true if str ends with suffix, false otherwise.

◆ extractIpAddress()

std::string mm::core::util::extractIpAddress ( const std::string &  socketAddress)

Extracts the IP address from a socket address string.

Parses a string of the form "IP:port" and returns the IP portion.

Parameters
socketAddressA socket address in the format "IP:port".
Returns
The extracted IP address.
Exceptions
std::invalid_argumentIf the input string does not contain a colon.

◆ extractPort()

unsigned short mm::core::util::extractPort ( const std::string &  socketAddress)

Extracts the port number from a socket address string.

Parses a string of the form "IP:port" and returns the port portion as an unsigned short.

Parameters
socketAddressA socket address in the format "IP:port".
Returns
The extracted port number.
Exceptions
std::invalid_argumentIf the input string does not contain a valid port.

◆ formatMacAddress()

std::string mm::core::util::formatMacAddress ( const std::string &  originalMacAddress)

Formats a MAC address string to ensure each component is two digits and uppercase.

This function takes a MAC address string in formats such as "a-b-c-d-e-f" or "a:b:c:d:e:f", and returns a standardized format like "0A:0B:0C:0D:0E:0F". It ensures that each component is two characters long (adding leading zeros if necessary), uses ':' as the delimiter, and converts all letters to uppercase.

Parameters
originalMacAddressThe input MAC address string, using either ':' or '-' as delimiter.
Returns
A formatted MAC address string with two-digit uppercase hexadecimal components, separated by colons.

◆ fromBytes()

template<typename T >
T mm::core::util::fromBytes ( const std::vector< uint8_t > &  bytes,
bool  bigEndian = false 
)

Reconstructs a value of type T from a byte vector.

Deserializes a value of trivially copyable type T from a std::vector<uint8_t> containing its memory representation. Optionally interprets the byte vector as big-endian.

Template Parameters
TThe type of the value to reconstruct. Must be trivially copyable.
Parameters
bytesThe byte vector representing the serialized value.
bigEndianIf true, the byte vector is interpreted as big-endian. If false (default), little-endian order is assumed.
Returns
The deserialized value of type T.
Exceptions
std::invalid_argumentIf the size of the byte vector does not match sizeof(T).
Note
The function performs a shallow memory copy. Ensure that the byte vector was created with a compatible layout (e.g., using toBytes<T>). Be cautious with types containing padding or platform-specific layout.

◆ joinStrings()

std::string mm::core::util::joinStrings ( const std::vector< std::string > &  list,
const std::string &  delimiter 
)

Joins a list of strings into a single string with a delimiter.

Concatenates all elements in the given vector of strings, inserting the specified delimiter between each element. If the list is empty, returns an empty string.

Parameters
listThe vector of strings to join.
delimiterThe delimiter to insert between each string.
Returns
A single string composed of the input strings separated by the delimiter.

◆ linearScale()

template<typename T >
constexpr T mm::core::util::linearScale ( value,
inMin,
inMax,
outMin,
outMax 
)
inlineconstexpr

Maps a value from one range to another using linear interpolation.

Template Parameters
TType of input and output values (int, float, double, etc.)
Parameters
valueInput value to be mapped
inMinLower bound of input range
inMaxUpper bound of input range
outMinLower bound of output range
outMaxUpper bound of output range
Returns
Mapped value in the output range

◆ makeParameterId()

std::string mm::core::util::makeParameterId ( int  index,
int  subindex 
)
inline

Formats the given index and subindex into a parameter identifier string.

This function takes an index and a subindex and formats them into a string of the form "0xINDEX:SUBINDEX". The index is formatted as a 4-digit hexadecimal number, and the subindex is formatted as a 2-digit hexadecimal number.

Parameters
indexThe 16-bit index value (e.g., 0x2030).
subindexThe 8-bit subindex value (e.g., 0x01).
Returns
std::string The formatted parameter ID string in the format "0xINDEX:SUBINDEX".

◆ parseHex()

template<typename T >
T mm::core::util::parseHex ( const std::string &  s)

Parses a hexadecimal string into an unsigned integer of type T.

This function converts a hex string (e.g., "0x1A3F" or "1A3F") to an unsigned integer of the specified type T. It supports any unsigned integer type such as uint16_t, uint32_t, uint64_t, etc.

The function uses std::stoul for types up to the size of unsigned long, and std::stoull for larger types (e.g., uint64_t).

Template Parameters
TThe unsigned integer type to parse to. Must be an unsigned integral type.
Parameters
sThe hexadecimal string to parse.
Returns
Parsed value as type T.
Exceptions
std::invalid_argumentif the string is not a valid hex number.
std::out_of_rangeif the parsed value is out of range for type T.

◆ readBinaryFile()

std::vector< uint8_t > mm::core::util::readBinaryFile ( const std::string &  filename)

Reads the entire contents of a binary file into a byte vector.

Opens the file in binary mode and reads all bytes into a std::vector<uint8_t>.

Parameters
filePathThe path to the binary file to read.
Returns
A std::vector<uint8_t> containing the file's raw bytes.
Exceptions
std::runtime_errorIf the file cannot be opened.

◆ readTextFile()

std::string mm::core::util::readTextFile ( const std::string &  filename)

Reads the entire content of a text file into a std::string.

Parameters
filePathThe path to the text file to be read.
Returns
A std::string containing the full content of the file.
Exceptions
std::runtime_errorIf the file cannot be opened.

◆ split()

std::vector< std::string > mm::core::util::split ( const std::string &  input,
char  delimiter 
)
inline

Splits a string into substrings based on a delimiter character.

Parses the input string, splitting it at each occurrence of the delimiter, and returns a vector containing the resulting substrings in order.

Parameters
inputThe input string to split.
delimiterThe character used as the delimiter to split the string.
Returns
std::vector<std::string> A vector of substrings obtained by splitting the input.

◆ splitIntoParts()

std::vector< std::vector< uint8_t > > mm::core::util::splitIntoParts ( const std::vector< uint8_t > &  input,
size_t  partSize 
)

Splits a byte vector into parts of a specified maximum size.

Parameters
inputThe input vector of bytes.
partSizeThe maximum size (in bytes) for each part.
Returns
std::vector<std::vector<uint8_t>> The resulting vector of parts.

◆ stringViewToNumber()

template<typename T >
bool mm::core::util::stringViewToNumber ( std::string_view  sv,
T &  result 
)

Converts a string_view to a numeric type.

This function uses std::from_chars to convert a given std::string_view into a numeric value of type T. The conversion is done without allocating memory, and the function returns true if the conversion succeeds, false otherwise.

Template Parameters
TThe numeric type to convert the string_view to (e.g., int, long, double).
Parameters
svThe string_view representing the numeric string to be converted.
resultThe variable where the result of the conversion will be stored.
Returns
True if the conversion was successful, false otherwise.
Note
This function only supports arithmetic types (integers and floating-point types).

◆ toBytes()

template<typename T >
std::vector< uint8_t > mm::core::util::toBytes ( value,
bool  bigEndian = false 
)

Converts a trivially copyable value to a byte vector.

Serializes the given value into a std::vector<uint8_t> by copying its memory representation. Optionally converts to big-endian byte order.

Template Parameters
TThe type of the value to convert. Must be trivially copyable.
Parameters
valueThe value to convert to bytes.
bigEndianIf true, the resulting byte vector will be in big-endian order. If false (default), little-endian order is used.
Returns
A vector of bytes representing the memory of the value.
Note
The function performs a shallow copy of the memory representation. Use with caution on types with padding or platform-dependent layout.

◆ toHex() [1/2]

std::string mm::core::util::toHex ( const std::vector< uint8_t > &  data)
inline

Converts a vector of bytes to a space-separated hexadecimal string.

Formats each byte in the input vector as a two-digit hexadecimal value with a 0x prefix. Bytes are separated by a single space.

Example: a vector containing {0xAB, 0xCD} will be formatted as "0xAB 0xCD".

Parameters
dataThe vector of bytes to format.
Returns
A string containing the hexadecimal representation of the bytes.

◆ toHex() [2/2]

template<typename T >
std::string mm::core::util::toHex ( value)
inline

Converts an integer value to its hexadecimal string representation.

This function template formats any integral value as a hexadecimal string, including a 0x prefix, uppercase letters, and leading zeros based on the size of the type.

Template Parameters
TAn integral type (e.g., int, uint32_t).
Parameters
valueThe integer value to convert.
Returns
A string containing the hexadecimal representation of the input.
Note
If the input type is not integral, a compile-time error will occur.

◆ toInteger()

template<typename T >
T mm::core::util::toInteger ( const std::vector< std::uint8_t > &  data,
std::size_t  offset = 0,
Endianness  endian = Endianness::LITTLE 
)

Converts a byte sequence to an integer of type T with optional offset and endianness.

This function reads up to sizeof(T) bytes from the given data vector starting at the specified offset and interprets them as an integer of type T. If there are fewer than sizeof(T) bytes available, the missing high-order bytes are zero-padded.

Supported integer types include signed and unsigned types like int16_t, uint32_t, etc.

Template Parameters
TThe target integral type to convert to (e.g., int32_t, uint16_t).
Parameters
dataA vector of bytes to extract the integer value from.
offsetThe starting index in the vector (default is 0).
endianThe byte order used for conversion (default is Endianness::Little).
Returns
T The resulting integer value of type T.
Note
This function uses static_assert to ensure T is an integral type.
If the offset is beyond the end of the vector, the result will be 0.

◆ unzipData()

std::unordered_map< std::string, std::vector< uint8_t > > mm::core::util::unzipData ( const std::vector< uint8_t > &  data)

Unzips a ZIP archive from an in-memory byte buffer.

This function opens a ZIP archive from the provided raw data buffer, extracts all files inside it, and returns a mapping from filenames to their respective uncompressed file data.

Parameters
dataA vector of bytes containing the ZIP archive data.
Returns
An unordered_map where each key is a filename (string) and the corresponding value is a vector<uint8_t> with the uncompressed file contents.
Exceptions
std::runtime_errorIf the ZIP archive cannot be opened, is empty, contains invalid entries, or any error occurs during extraction.

◆ zipData()

std::vector< uint8_t > mm::core::util::zipData ( const std::string &  filename,
const std::vector< uint8_t > &  data 
)

Compresses a block of data into a ZIP archive containing a single file.

This function creates an in-memory ZIP archive and adds a single entry (file) to it, containing the provided raw data. It returns the entire ZIP archive as a byte vector.

Parameters
filenameThe name of the file entry to store inside the ZIP archive.
dataThe raw data to be compressed and stored in the ZIP archive.
Returns
std::vector<uint8_t> The resulting ZIP archive as a vector of bytes.
Exceptions
std::runtime_errorIf any step of the ZIP creation fails (e.g., entry open, write, or stream copy).
Note
This function requires a ZIP library that provides zip_stream_* functions, such as miniz or a compatible wrapper.