Skip to content

namespace flake::utils

Functions Overview

Name
template <typename T >
constexprT
safe_or(T lhs, T rhs)
Promotion-safe bitwise OR for unsigned integral types.
template <typename T >
constexprT
safe_and(T lhs, T rhs)
Promotion-safe bitwise AND for unsigned integral types.
template <typename T_dst ,typename T1 ,typename T2 >
constexprT_dst
safe_add(T1 lhs, T2 rhs)
Promotion-safe addition with explicit destination type.
template <typename T_dst ,typename T1 ,typename T2 >
constexprT_dst
safe_sub(T1 lhs, T2 rhs)
Promotion-safe subtraction with explicit destination type.
template <typename T >
constexprT
safe_not(T x)
Promotion-safe bitwise NOT for unsigned integral types.
template <typename T_dst ,typename T_src >
void
memcpy(T_dst * dest, constT_src * src, std::size_t count)
Element-wise, type-checked memcpy for trivial types.
std::size_tstrlen(constchar * str, std::size_t max_len)
Bounded string length (does not read past max_len).
char *charFromString(const std::string & str)
Create a heap-allocated C string from a std::string.
std::stringstringFromChar(constchar * str)
Create a std::string from a null-terminated C string.
uint32_ttimestamp()
Return a monotonic timestamp in milliseconds.
char *copyStr(constchar * src)
Duplicate a C string on the heap.
unsignedcharcrc8(unsignedconstchar * data, unsignedchar len)
Compute an 8-bit CRC over a byte buffer.

Function Details

function safe_or

cpp
template <typename T >
constexprT safe_or(
    T lhs,
    T rhs
)

Promotion-safe bitwise OR for unsigned integral types.

Parameters:

  • lhs Left-hand operand.
  • rhs Right-hand operand.

Template Parameters:

  • T Unsigned integral type.

Return: lhs | rhs cast back to T.

Both operands are widened to std::uintmax_t before the operation, avoiding implicit-promotion pitfalls.

function safe_and

cpp
template <typename T >
constexprT safe_and(
    T lhs,
    T rhs
)

Promotion-safe bitwise AND for unsigned integral types.

Parameters:

  • lhs Left-hand operand.
  • rhs Right-hand operand.

Template Parameters:

  • T Unsigned integral type.

Return: lhs & rhs cast back to T.

function safe_add

cpp
template <typename T_dst ,
typename T1 ,
typename T2 >
constexprT_dst safe_add(
    T1 lhs,
    T2 rhs
)

Promotion-safe addition with explicit destination type.

Parameters:

  • lhs Left-hand operand.
  • rhs Right-hand operand.

Template Parameters:

  • T_dst Destination type.
  • T1 Type of the left-hand operand.
  • T2 Type of the right-hand operand.

Return: lhs + rhs cast to T_dst.

All three types must share the same signedness.

function safe_sub

cpp
template <typename T_dst ,
typename T1 ,
typename T2 >
constexprT_dst safe_sub(
    T1 lhs,
    T2 rhs
)

Promotion-safe subtraction with explicit destination type.

Parameters:

  • lhs Left-hand operand.
  • rhs Right-hand operand.

Template Parameters:

  • T_dst Destination type.
  • T1 Type of the left-hand operand.
  • T2 Type of the right-hand operand.

Return: lhs - rhs cast to T_dst.

function safe_not

cpp
template <typename T >
constexprT safe_not(
    T x
)

Promotion-safe bitwise NOT for unsigned integral types.

Parameters:

  • x Operand.

Template Parameters:

  • T Unsigned integral type.

Return: ~x cast back to T.

function memcpy

cpp
template <typename T_dst ,
typename T_src >
void memcpy(
    T_dst * dest,
    constT_src * src,
    std::size_t count
)

Element-wise, type-checked memcpy for trivial types.

Parameters:

  • dest Destination array.
  • src Source array.
  • count Number of elements to copy.

Template Parameters:

  • T_dst Destination element type (must be trivial).
  • T_src Source element type (must be trivially convertible to T_dst).

Copies count elements from src to dest, performing a static_cast on each element. Both pointers are null-checked.

function strlen

cpp
inline std::size_t strlen(
    constchar * str,
    std::size_t max_len
)

Bounded string length (does not read past max_len).

Parameters:

  • str Null-terminated C string (may be nullptr).
  • max_len Maximum number of characters to examine.

Return: Length of str, capped at max_len.

function charFromString

cpp
char * charFromString(
    const std::string & str
)

Create a heap-allocated C string from a std::string.

Parameters:

  • str Source string.

Return: Newly allocated null-terminated C string. The caller must delete[] the result.

function stringFromChar

cpp
std::string stringFromChar(
    constchar * str
)

Create a std::string from a null-terminated C string.

Parameters:

  • str Source C string (may be nullptr → empty string).

Return: Corresponding std::string.

function timestamp

cpp
uint32_t timestamp()

Return a monotonic timestamp in milliseconds.

Return: Current timestamp (platform-dependent epoch).

function copyStr

cpp
char * copyStr(
    constchar * src
)

Duplicate a C string on the heap.

Parameters:

  • src Null-terminated source string.

Return: Newly allocated copy. Caller must delete[] the result.

function crc8

cpp
unsignedchar crc8(
    unsignedconstchar * data,
    unsignedchar len
)

Compute an 8-bit CRC over a byte buffer.

Parameters:

  • data Input bytes.
  • len Number of bytes.

Return: Computed CRC-8 value.