microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
reloco_debug.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
80#include <microfmt/microfmt.hpp>
81#include <reloco/any.hpp>
82#include <reloco/error.hpp>
83#include <reloco/flat_hash_map.hpp>
84#include <reloco/flat_hash_set.hpp>
85#include <reloco/rc.hpp>
86
87#include "reloco.hpp" // formatter<reloco::type_id>, reused by Debug<any> below
88
89namespace reloco {
90
104template <> struct Debug<any> {
105 static void format(const any &val, const sink &out) noexcept {
106 out.write("any(");
107 microfmt::format_to(out, "{}", val.type_id());
108 out.put(')');
109 }
110};
111
118template <> struct Debug<error> {
119 static void format(const error &val, const sink &out) noexcept { microfmt::formatter<error>{}.format(val, out); }
120};
121
131template <typename T, typename E> struct Debug<expected<T, E>> {
132 static void format(const expected<T, E> &val, const sink &out) noexcept {
133 if (val.has_value()) {
134 microfmt::format_to(out, "Ok({})", microfmt::as_debug(val.value()));
135 } else {
136 microfmt::format_to(out, "Err({})", microfmt::as_debug(val.error()));
137 }
138 }
139};
140
149template <typename E> struct Debug<expected<void, E>> {
150 static void format(const expected<void, E> &val, const sink &out) noexcept {
151 if (val.has_value()) {
152 out.write("Ok()");
153 } else {
154 microfmt::format_to(out, "Err({})", microfmt::as_debug(val.error()));
155 }
156 }
157};
158
168template <typename T> struct Debug<rc<T>> {
169 static void format(const rc<T> &val, const sink &out) noexcept {
170 microfmt::format_to(out, "rc(use_count: {}) {{ ", val.use_count());
171 if (!val) {
172 out.write("(null)");
173 } else {
174 microfmt::format_to(out, "{}", microfmt::as_debug(*val.get()));
175 }
176 out.write(" }");
177 }
178};
179
187template <typename T> struct Debug<weak_rc<T>> {
188 static void format(const weak_rc<T> &val, const sink &out) noexcept {
189 microfmt::format_to(out, "rc(use_count: {}) {{ ", val.use_count());
190 auto locked = val.lock();
191 if (!locked.has_value()) {
192 out.write("(expired)");
193 } else {
194 microfmt::format_to(out, "{}", microfmt::as_debug(*locked.value().get()));
195 }
196 out.write(" }");
197 }
198};
199
208template <typename T, typename Hash, typename KeyEqual> struct Debug<flat_hash_set<T, Hash, KeyEqual>> {
209 static void format(const flat_hash_set<T, Hash, KeyEqual> &val, const sink &out) noexcept {
210 microfmt::format_to(out, "flat_hash_set(size: {}, capacity: {}, load_factor_permille: {}) [", val.size(),
211 val.capacity(), val.load_factor_permille());
212 bool is_first = true;
213 for (const auto &elem : val) {
214 if (!is_first) {
215 out.write(", ");
216 }
217 is_first = false;
218 microfmt::format_to(out, "{}", microfmt::as_debug(elem));
219 }
220 out.put(']');
221 }
222};
223
228template <typename Key, typename Mapped, typename Hash, typename KeyEqual>
229struct Debug<flat_hash_map<Key, Mapped, Hash, KeyEqual>> {
230 static void format(const flat_hash_map<Key, Mapped, Hash, KeyEqual> &val, const sink &out) noexcept {
231 microfmt::format_to(out, "flat_hash_map(size: {}, capacity: {}, load_factor_permille: {}) {{", val.size(),
232 val.capacity(), val.load_factor_permille());
233 bool is_first = true;
234 for (const auto &entry : val) {
235 if (!is_first) {
236 out.write(", ");
237 }
238 is_first = false;
239 microfmt::format_to(out, "{}: {}", microfmt::as_debug(entry.first), microfmt::as_debug(entry.second));
240 }
241 out.put('}');
242 }
243};
244
245} // namespace reloco
Core formatting primitives, sinks, and customization point.
RELOCO_CONSTEXPR20 void format_to(const sink &out, microfmt::string_view fmt, const Args &...args) noexcept
Definition microfmt.hpp:1766
constexpr auto as_debug(const T &value RELOCO_LIFETIMEBOUND) noexcept
Wraps value so it always renders through reloco's Debug<T> customization point when one exists,...
Definition microfmt.hpp:1702
Definition debug_float.hpp:36
Single re-export of every `reloco` type and annotation microfmt builds on.
Definition microfmt.hpp:985
static void format(const any &val, const sink &out) noexcept
Definition reloco_debug.hpp:105
static void format(const error &val, const sink &out) noexcept
Definition reloco_debug.hpp:119
static void format(const expected< T, E > &val, const sink &out) noexcept
Definition reloco_debug.hpp:132
static void format(const expected< void, E > &val, const sink &out) noexcept
Definition reloco_debug.hpp:150
static void format(const flat_hash_map< Key, Mapped, Hash, KeyEqual > &val, const sink &out) noexcept
Definition reloco_debug.hpp:230
static void format(const flat_hash_set< T, Hash, KeyEqual > &val, const sink &out) noexcept
Definition reloco_debug.hpp:209
static void format(const rc< T > &val, const sink &out) noexcept
Definition reloco_debug.hpp:169
static void format(const weak_rc< T > &val, const sink &out) noexcept
Definition reloco_debug.hpp:188