microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
concrete_metadata_map.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2// SPDX-License-Identifier: BSD-2-Clause
3
4#pragma once
5
9#include "../reloco.hpp"
10#include "metadata_map.hpp"
11#include <type_traits>
12
13namespace microfmt {
14
22public:
27 explicit constexpr concrete_metadata_map(span<property_entry> storage) noexcept : m_storage(storage), m_count(0) {}
28
37 template <typename T> constexpr bool set(microfmt::string_view key, value_ref<T> ref) noexcept {
38 const T *val_ptr = ref.get();
39
40 // Update existing key if found
41 for (std::size_t i = 0; i < m_count; ++i) {
42 if (m_storage[i].key == key) {
43 m_storage[i].val_ptr = static_cast<const void *>(val_ptr);
44 m_storage[i].print_fn = [](const void *ptr, const sink &out) noexcept {
45 using DecayedT = std::decay_t<T>;
47 fmt.format(*static_cast<const DecayedT *>(ptr), out);
48 };
49 return true;
50 }
51 }
52
53 // Insert new entry if storage span capacity permits
54 if (m_count < m_storage.size()) {
55 m_storage[m_count] =
56 property_entry{key, static_cast<const void *>(val_ptr), [](const void *ptr, const sink &out) noexcept {
57 using DecayedT = std::decay_t<T>;
59 fmt.format(*static_cast<const DecayedT *>(ptr), out);
60 }};
61 ++m_count;
62 return true;
63 }
64
65 return false; // Backing span capacity exceeded
66 }
67
68 [[nodiscard]] constexpr std::size_t size() const noexcept { return m_count; }
69 [[nodiscard]] constexpr std::size_t capacity() const noexcept { return m_storage.size(); }
70 [[nodiscard]] constexpr bool empty() const noexcept { return m_count == 0; }
71
75 struct RELOCO_POINTER span_iteration_state {
76 const property_entry *current{nullptr};
77 const property_entry *end{nullptr};
78 };
79
86 [[nodiscard]] constexpr metadata_map make_view(span_iteration_state &state) const & noexcept RELOCO_LIFETIMEBOUND {
87 state.current = m_storage.data();
88
89 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
90 state.end = m_storage.data() + m_count;
91
92 return metadata_map(&state, [](void *ctx, property_entry &out) noexcept -> bool {
93 auto *s = static_cast<span_iteration_state *>(ctx);
94 if (s == nullptr || s->current >= s->end) {
95 return false;
96 }
97 out = *s->current++;
98
99 return true;
100 });
101 RELOCO_END_UNSAFE_BUFFER_USAGE;
102 }
103
105
106private:
107 span<property_entry> m_storage;
108 std::size_t m_count{0};
109};
110
111} // namespace microfmt
Concrete mutable metadata map backed by an external span storage.
Definition concrete_metadata_map.hpp:21
constexpr metadata_map make_view(span_iteration_state &state) const &noexcept RELOCO_LIFETIMEBOUND
Exclusively creates a type-erased metadata_map view using caller-provided stack state.
Definition concrete_metadata_map.hpp:86
constexpr bool set(microfmt::string_view key, value_ref< T > ref) noexcept
Stores or updates a property safely with type erasure.
Definition concrete_metadata_map.hpp:37
constexpr bool empty() const noexcept
Definition concrete_metadata_map.hpp:70
constexpr std::size_t size() const noexcept
Definition concrete_metadata_map.hpp:68
metadata_map make_view(span_iteration_state &) const &&=delete
constexpr std::size_t capacity() const noexcept
Definition concrete_metadata_map.hpp:69
constexpr concrete_metadata_map(span< property_entry > storage) noexcept
Constructs a concrete metadata map over an external span buffer.
Definition concrete_metadata_map.hpp:27
Type-erased, non-owning metadata map view driven by a sequential next callback.
Definition metadata_map.hpp:56
#define MICROFMT_API_CLASS
Definition compat.hpp:181
Type-erased, zero-allocation metadata map generator view.
Definition fmt.hpp:17
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
Required stack state structure to drive iteration safely.
Definition concrete_metadata_map.hpp:75
Definition microfmt.hpp:985
Type-erased property entry synthesized during generation.
Definition metadata_map.hpp:24