dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Utils Namespace Reference

Namespaces

namespace  literals
 Namespace containing fixed-width integer literal operators.

Classes

class  Exception
 Runtime error that captures a std::source_location. More...
struct  ExecOnOOS
 Scope guard that executes a callable when it goes out of scope. More...
class  Flags
 Typesafe bitmask wrapper for enum flags. More...
class  JoinedView
 Lightweight wrapper used to format a range as a delimiter-joined string via std::format. More...
class  JsonValidator
 Registry of JSON Schemas with validation helpers. More...
class  LoggerFactory
class  MonotonicIdAllocator
 Monotonically increasing ID allocator. More...
class  RecyclableIdAllocator
 ID allocator that can recycle released identifiers. More...
struct  ScopedMdc
class  SystemConfig
 System configuration registry with typed values, entry metadata, and change callbacks. More...
struct  TransparentEqual
 Transparent equality predicate for string-like lookups. More...
struct  TransparentHash
 Transparent hash functor for string-like lookups. More...
struct  UUID
 128-bit UUID value type. More...
class  UUIDGenerator
 Random UUID generator. More...

Concepts

concept  IsEnum
 Concept matching any enumeration type. This is used to constrain Flags to be instantiated only with enum types.

Typedefs

using ExceptionStack = std::vector<Exception>
 Convenience alias for a linearized nested exception chain.
using TransparentLess = std::less<>
 Comparator alias enabling transparent ordered lookup.
template<typename T, typename Allocator = std::allocator<std::pair<std::string const, T>>>
using UnorderedStringMap = std::unordered_map<std::string, T, TransparentHash, TransparentEqual, Allocator>
 Convenience alias for an unordered map keyed by std::string with transparent lookup.
template<typename T, typename Allocator = std::allocator<std::pair<std::string const, T>>>
using StringMap = std::map<std::string, T, TransparentLess, Allocator>
 Convenience alias for an ordered map keyed by std::string with transparent lookup.

Functions

template<typename T>
constexpr bool __b_assert_expr (T &&expr)
 Helper used by B_ASSERT to validate and evaluate the asserted expression.
template<typename F, typename ... Args>
requires std::invocable<F, Args...>
auto safeInvoke (std::shared_ptr< spdlog::logger > logger, F &&f, Args &&... args) noexcept
 Invoke a callable and catch/log all exceptions.
template<typename F, typename ... Args>
requires std::invocable<F, Args...>
auto safeInvoke (std::string const &loggerName, F &&f, Args &&... args) noexcept
 Convenience overload of safeInvoke using a logger name. Internally resolves the logger via spdlog::get(loggerName) and then calls the logger-pointer overload.
template<std::ranges::forward_range Range>
constexpr JoinedView< Range > join (Range const &range, std::string_view delimiter) noexcept
 Create a JoinedView from a range and a delimiter.
template<typename It, typename Sent>
requires std::forward_iterator<It> && std::sentinel_for<Sent, It>
constexpr auto join (It first, Sent last, std::string_view delimiter) noexcept
 Create a JoinedView from an iterator/sentinel pair and a delimiter.
std::vector< std::string > split (std::string const &str, std::string_view sep=" ", bool skipEmpty=true, bool shouldTrim=false)
 Split a string into substrings separated by sep.
std::vector< std::string_view > splitView (std::string_view str, std::string_view sep=" ", bool skipEmpty=true, bool shouldTrim=false)
 Split a string view into substring views separated by sep.
std::string toLower (std::string_view str)
 Convert a string to lower case.
std::string toUpper (std::string_view str)
 Convert a string to upper case.
std::string & ltrim (std::string &s) noexcept
 Trim leading whitespace from a string in-place.
std::string & rtrim (std::string &s) noexcept
 Trim trailing whitespace from a string in-place.
std::string & trim (std::string &s) noexcept
 Trim leading and trailing whitespace from a string in-place.
std::string_view & trimView (std::string_view &str) noexcept
 Trim leading and trailing whitespace from a string view by adjusting its boundaries.
template<typename T1, typename T2>
bool isOneOf (T1 val, std::initializer_list< T2 const > list)
 Test whether val is equal to one of the elements in list.
template<typename T1, typename T2>
bool isOneOf (T1 val, std::span< T2 const > list)
 Test whether val is equal to one of the elements in list.
std::string formatBytes (std::uint64_t byteCount, std::size_t precision=2)
 Format a byte count as a human-readable string.
std::optional< bool > boolFromString (std::string_view str) noexcept
 Parse a boolean value from text.
std::string_view boolToString (bool value) noexcept
 Convert a boolean value to a stable textual representation.
std::string_view strsignal (int sig) noexcept
 Convert a signal number to a stringified version.

Typedef Documentation

◆ ExceptionStack

using dfx::Utils::ExceptionStack = std::vector<Exception>

Convenience alias for a linearized nested exception chain.

The first element is the outer-most exception (the one you caught), followed by nested exceptions (as produced by std::throw_with_nested).

◆ StringMap

template<typename T, typename Allocator = std::allocator<std::pair<std::string const, T>>>
using dfx::Utils::StringMap = std::map<std::string, T, TransparentLess, Allocator>

Convenience alias for an ordered map keyed by std::string with transparent lookup.

Template Parameters
TValue type.
AllocatorAllocator type for std::pair<const std::string, T>.

Uses TransparentLess (i.e. std::less<>) to allow heterogeneous lookup with std::string_view.

◆ TransparentLess

using dfx::Utils::TransparentLess = std::less<>

Comparator alias enabling transparent ordered lookup.

std::less<> is a transparent comparator. When used as the comparator of std::map<std::string, ...>, it enables lookups using std::string_view without allocating a temporary std::string.

◆ UnorderedStringMap

template<typename T, typename Allocator = std::allocator<std::pair<std::string const, T>>>
using dfx::Utils::UnorderedStringMap = std::unordered_map<std::string, T, TransparentHash, TransparentEqual, Allocator>

Convenience alias for an unordered map keyed by std::string with transparent lookup.

Template Parameters
TValue type.
AllocatorAllocator type for std::pair<const std::string, T>.

Uses TransparentHash and TransparentEqual to allow heterogeneous lookup with std::string_view.

Function Documentation

◆ __b_assert_expr()

template<typename T>
bool dfx::Utils::__b_assert_expr ( T && expr)
constexpr

Helper used by B_ASSERT to validate and evaluate the asserted expression.

This function exists mainly to improve diagnostics:

  • It static-asserts that the first argument is not a string literal / array (common mistake: missing boolean condition in B_ASSERT).
  • It static-asserts that the expression is convertible to bool.
Returns
The boolean value of expr.

◆ boolFromString()

std::optional< bool > dfx::Utils::boolFromString ( std::string_view str)
noexcept

Parse a boolean value from text.

Parameters
strInput text.
Returns
  • std::optional<bool> containing the parsed boolean on success
  • std::nullopt if the string is not recognized as a boolean value

The accepted spellings are "true" and "false" only in a case-insensitive manner. Any other value will return std::nullopt

◆ boolToString()

std::string_view dfx::Utils::boolToString ( bool value)
noexcept

Convert a boolean value to a stable textual representation.

Parameters
valueBoolean value.
Returns
A string view representing the boolean.

This will return the string "true" or "false" depending on the input value

◆ formatBytes()

std::string dfx::Utils::formatBytes ( std::uint64_t byteCount,
std::size_t precision = 2 )

Format a byte count as a human-readable string.

Parameters
byteCountNumber of bytes.
precisionNumber of fractional digits to include.
Returns
A formatted string (e.g., using KB/MB/GB-like units).

◆ isOneOf() [1/2]

template<typename T1, typename T2>
bool dfx::Utils::isOneOf ( T1 val,
std::initializer_list< T2 const > list )

Test whether val is equal to one of the elements in list.

Template Parameters
T1Value type.
T2List element type.
Parameters
valValue to test.
listCandidate values.
Returns
true if val matches an element of list, otherwise false.

This performs a linear search using operator==.

◆ isOneOf() [2/2]

template<typename T1, typename T2>
bool dfx::Utils::isOneOf ( T1 val,
std::span< T2 const > list )

Test whether val is equal to one of the elements in list.

Template Parameters
T1Value type.
T2List element type.
Parameters
valValue to test.
listCandidate values as a span.
Returns
true if val matches an element of list, otherwise false.

This performs a linear search using operator==.

◆ join() [1/2]

template<typename It, typename Sent>
requires std::forward_iterator<It> && std::sentinel_for<Sent, It>
auto dfx::Utils::join ( It first,
Sent last,
std::string_view delimiter )
constexprnoexcept

Create a JoinedView from an iterator/sentinel pair and a delimiter.

Convenience overload that builds a std::ranges::subrange and forwards to join(range, delimiter).

Template Parameters
ItForward iterator type.
SentSentinel type for It.
Parameters
firstBeginning of the sequence.
lastEnd sentinel of the sequence.
delimiterDelimiter placed between consecutive elements.
Returns
A JoinedView over the created subrange.

◆ join() [2/2]

template<std::ranges::forward_range Range>
JoinedView< Range > dfx::Utils::join ( Range const & range,
std::string_view delimiter )
constexprnoexcept

Create a JoinedView from a range and a delimiter.

This is the recommended entry point.

Template Parameters
RangeA forward range type.
Parameters
rangeInput range to join.
delimiterDelimiter placed between consecutive elements.
Returns
A JoinedView<Range> holding a copy of range and the delimiter view.

◆ ltrim()

std::string & dfx::Utils::ltrim ( std::string & s)
noexcept

Trim leading whitespace from a string in-place.

Parameters
sString to trim.
Returns
Reference to s.

Removes whitespace at the beginning of s.

◆ rtrim()

std::string & dfx::Utils::rtrim ( std::string & s)
noexcept

Trim trailing whitespace from a string in-place.

Parameters
sString to trim.
Returns
Reference to s.

Removes whitespace at the end of s.

◆ safeInvoke() [1/2]

template<typename F, typename ... Args>
requires std::invocable<F, Args...>
auto dfx::Utils::safeInvoke ( std::shared_ptr< spdlog::logger > logger,
F && f,
Args &&... args )
noexcept

Invoke a callable and catch/log all exceptions.

This helper is meant for "best-effort" code paths such as callbacks, destructors, or logging hooks where throwing is not desirable. Any exception is swallowed.

  • Catches Exception and prints the full nested stack via Exception::printToLogger.
  • Catches std::exception and logs what().
  • Catches everything else and logs a generic message.

If the callable returns a value, the return type is std::optional<Result>:

  • engaged on success
  • disengaged on error
Template Parameters
FCallable type.
ArgsArgument types.
Parameters
loggerLogger to use; if null, errors are swallowed silently.
fCallable to invoke.
argsArguments forwarded to the callable.

◆ safeInvoke() [2/2]

template<typename F, typename ... Args>
requires std::invocable<F, Args...>
auto dfx::Utils::safeInvoke ( std::string const & loggerName,
F && f,
Args &&... args )
noexcept

Convenience overload of safeInvoke using a logger name. Internally resolves the logger via spdlog::get(loggerName) and then calls the logger-pointer overload.

◆ split()

std::vector< std::string > dfx::Utils::split ( std::string const & str,
std::string_view sep = " ",
bool skipEmpty = true,
bool shouldTrim = false )

Split a string into substrings separated by sep.

Parameters
strInput string to split.
sepSeparator string. May contain multiple characters.
skipEmptyIf true, empty tokens are omitted.
shouldTrimIf true, each produced token is trimmed (whitespace) before being returned.
Returns
A vector of owning strings representing the split tokens.
Note
This overload returns owning std::string tokens. If you want zero-allocation views over the original text, use splitView.

◆ splitView()

std::vector< std::string_view > dfx::Utils::splitView ( std::string_view str,
std::string_view sep = " ",
bool skipEmpty = true,
bool shouldTrim = false )

Split a string view into substring views separated by sep.

Parameters
strInput string view to split.
sepSeparator string. May contain multiple characters.
skipEmptyIf true, empty tokens are omitted.
shouldTrimIf true, each produced token view is trimmed (whitespace) before being returned.
Returns
A vector of std::string_view tokens.
Warning
The returned views refer to the input str. The caller must ensure the referenced character storage remains valid for as long as the returned views are used.

◆ strsignal()

std::string_view dfx::Utils::strsignal ( int sig)
noexcept

Convert a signal number to a stringified version.

Unlike strsignal(3), this function doesn't return a description of the signal number but the stringified version of its macro. Example:

Signal dfx::Utils::strsignal strsignal(3)
SIGKILL "SIGKILL" "Killed"
SIGTERM "SIGTERM" "Terminated"
SIGSEGV "SIGSEGV" "Segmentation fault"

This function returns the string "Unknown" for unrecognized signal number

Parameters
sigSignal number (e.g., SIGTERM).
Returns
A string view describing the signal.

◆ toLower()

std::string dfx::Utils::toLower ( std::string_view str)

Convert a string to lower case.

Parameters
strInput text.
Returns
A new owning string with characters converted to lower case.

◆ toUpper()

std::string dfx::Utils::toUpper ( std::string_view str)

Convert a string to upper case.

Parameters
strInput text.
Returns
A new owning string with characters converted to upper case.

◆ trim()

std::string & dfx::Utils::trim ( std::string & s)
noexcept

Trim leading and trailing whitespace from a string in-place.

Parameters
sString to trim.
Returns
Reference to s.

Equivalent to applying rtrim then ltrim.

◆ trimView()

std::string_view & dfx::Utils::trimView ( std::string_view & str)
noexcept

Trim leading and trailing whitespace from a string view by adjusting its boundaries.

Parameters
strString view to trim.
Returns
Reference to str (updated to the trimmed view).

This does not modify the underlying character storage; it only adjusts the view.