|
microfmt
Zero-allocation C++ formatting for constrained environments
|
The metadata map types attach heterogeneous, formatted properties to diagnostic objects without allocation or virtual dispatch.
metadata_map is a type-erased, non-owning generator view. It stores a void * context and a metadata_next_fn_t callback. Each get_next call asks the callback to populate one property_entry; returning false ends iteration.
concrete_metadata_map manages a fixed-capacity table of property_entry objects supplied by the caller. It does not allocate the table or copy the property values. Use it when metadata is assembled locally and then exposed as a metadata_map.
set returns false when a new key would exceed the supplied capacity. Setting an existing key updates its borrowed value and formatter without increasing size(). Values can have different formattable types.
The keys, entry storage, values, concrete map, and iteration state are all caller-owned. They must remain alive until the view has finished formatting. Both keys and values are borrowed; value_ref prevents temporary values but does not extend lvalue lifetimes.
Formatting consumes the current generator state. Call make_view(state) again to reset the state before another traversal:
Do not create a view from a temporary concrete_metadata_map; the rvalue overload is deleted. A metadata_map constructed directly from a callback similarly requires its callback context to outlive all calls to get_next.
For metadata produced incrementally, implement a noexcept callback with this signature:
Populate out.key, out.val_ptr, and out.print_fn, then advance the context. The callback controls ordering and termination. A null context or callback makes get_next return false. If either the value pointer or print function in an entry is null, that entry formats as null.
The formatter emits JSON-like diagnostic text. Keys are surrounded with quotes but are not escaped, so use trusted, prevalidated diagnostic keys.
microfmt::md::write_metadata_table consumes a metadata_map and writes each property as a row in a two-column Markdown table. Supply a character scratch buffer large enough for the longest formatted value:
See examples/metadata_table_demo.cpp for a complete application that streams the generated document to standard output.