CnxFormat struct
#include <include/Cnx/Format.h>
CnxFormat
is the Trait which allows extensible and composable string formatting of builtin and user-defined types.
CnxFormat
requires an implementation of is_specifer_valid
to validate and parse the format specifier corresponding with your type in a call to cnx_format
, format
, to format your type using the default system allocator for allocations, and format_with_allocator
, to format your type using a user-provided allocator for allocations.
To provide an implementation of CnxFormat
for your type, only three functions and the Trait implementation are required. The functions take the following signatures:
CnxFormatContext (*const your_is_specifier_valid)(const CnxFormat* restrict self, CnxStringView specifier); CnxString (*const your_format)(const CnxFormat* restrict self, CnxFormatContext context); CnxString (*const your_format_with_allocator)(const CnxFormat* restrict self, CnxFormatContext context, CnxAllocator allocator);
And providing the Trait implementation is as simple as:
ImplTraitFor(CnxFormat, your_type, your_is_specifier_valid, your_format, your_format_with_allocator);
In practice, you will probably be providing this Trait implementation in a header file, so you'll also probably want to mark it as static
and `__attr(maybe_unused)
__attr(maybe_unused) static ImplTraitFor(CnxFormat, your_type, your_is_specifier_valid, your_format, your_format_with_allocator);