Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection based, no app coupling), verified by the test suite (1103 passing): Data reflection: - Reflect.hpp now (de)serializes chrono::duration, chrono::time_point, std::variant (tagged), std::array, and non-string-keyed std::map; Meta.hpp gains is_time_point_v et al. Async: - EventBus gains Mode::Async (worker-thread dispatch + flush()). - ThreadPool gains a bounded queue, try_submit(), and rejection stats. New modules / containers: - Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker, ResourceMonitor. - Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V> (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats). - System: process/CPU/memory metrics (Metrics.hpp) and a streaming Process API. Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions, history states, deferred events), Configuration/Logging reflection helpers, and the accompanying test suites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
43b5a81504
commit
80293ee56f
93 files changed
+8500
-3312
No files matched your search
+213
-112
@@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Core/Meta.hpp>
|
||||
|
||||
#include <charconv>
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -9,130 +13,227 @@
|
||||
namespace Void::Csv
|
||||
{
|
||||
|
||||
using Row = std::vector<std::string>;
|
||||
using Table = std::vector<Row>;
|
||||
using Row = std::vector<std::string>;
|
||||
using Table = std::vector<Row>;
|
||||
|
||||
// ── parse ─────────────────────────────────────────────────────────────────────
|
||||
// RFC 4180-compatible parser. Fields may be quoted ("..."). CRLF and LF
|
||||
// both accepted as row terminators.
|
||||
// ── parse
|
||||
// ───────────────────────────────────────────────────────────────────── RFC
|
||||
// 4180-compatible parser. Fields may be quoted ("..."). CRLF and LF both
|
||||
// accepted as row terminators.
|
||||
|
||||
[[nodiscard]] inline Table parse(std::string_view text, char delim = ',')
|
||||
{
|
||||
Table result;
|
||||
Row row;
|
||||
std::string field;
|
||||
bool in_quote = false;
|
||||
[[nodiscard]] inline Table parse(std::string_view text, char delim = ',')
|
||||
{
|
||||
Table result;
|
||||
Row row;
|
||||
std::string field;
|
||||
bool in_quote = false;
|
||||
|
||||
for (std::size_t i = 0; i < text.size(); ++i) {
|
||||
const char c = text[i];
|
||||
for (std::size_t i = 0; i < text.size(); ++i) {
|
||||
const char c = text[i];
|
||||
|
||||
if (in_quote) {
|
||||
if (c == '"') {
|
||||
// Peek for doubled quote
|
||||
if (i + 1 < text.size() && text[i + 1] == '"') {
|
||||
field += '"'; ++i;
|
||||
} else {
|
||||
in_quote = false;
|
||||
}
|
||||
} else {
|
||||
field += c;
|
||||
}
|
||||
} else {
|
||||
if (c == '"') {
|
||||
in_quote = true;
|
||||
} else if (c == delim) {
|
||||
row.push_back(std::move(field));
|
||||
field.clear();
|
||||
} else if (c == '\n') {
|
||||
row.push_back(std::move(field));
|
||||
field.clear();
|
||||
result.push_back(std::move(row));
|
||||
row.clear();
|
||||
} else if (c == '\r') {
|
||||
// skip CR in CRLF
|
||||
} else {
|
||||
field += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (in_quote) {
|
||||
if (c == '"') {
|
||||
// Peek for doubled quote
|
||||
if (i + 1 < text.size() && text[i + 1] == '"') {
|
||||
field += '"';
|
||||
++i;
|
||||
} else {
|
||||
in_quote = false;
|
||||
}
|
||||
} else {
|
||||
field += c;
|
||||
}
|
||||
} else {
|
||||
if (c == '"') {
|
||||
in_quote = true;
|
||||
} else if (c == delim) {
|
||||
row.push_back(std::move(field));
|
||||
field.clear();
|
||||
} else if (c == '\n') {
|
||||
row.push_back(std::move(field));
|
||||
field.clear();
|
||||
result.push_back(std::move(row));
|
||||
row.clear();
|
||||
} else if (c == '\r') {
|
||||
// skip CR in CRLF
|
||||
} else {
|
||||
field += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flush last field/row
|
||||
row.push_back(std::move(field));
|
||||
if (!row.empty() && !(row.size() == 1 && row[0].empty())) {
|
||||
result.push_back(std::move(row));
|
||||
}
|
||||
// Flush last field/row
|
||||
row.push_back(std::move(field));
|
||||
if (!row.empty() && !(row.size() == 1 && row[0].empty())) {
|
||||
result.push_back(std::move(row));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── serialize ─────────────────────────────────────────────────────────────────
|
||||
// Produces RFC 4180 CSV. Fields that contain the delimiter, quote, or newline
|
||||
// are wrapped in double quotes; embedded quotes are doubled.
|
||||
// ── serialize
|
||||
// ─────────────────────────────────────────────────────────────────
|
||||
// Produces RFC 4180 CSV. Fields that contain the delimiter, quote, or
|
||||
// newline are wrapped in double quotes; embedded quotes are doubled.
|
||||
|
||||
[[nodiscard]] inline std::string serialize(const Table& table,
|
||||
char delim = ',')
|
||||
{
|
||||
std::string out;
|
||||
for (const auto& row : table) {
|
||||
for (std::size_t i = 0; i < row.size(); ++i) {
|
||||
const auto& field = row[i];
|
||||
const bool needs_quote =
|
||||
field.find(delim) != std::string::npos ||
|
||||
field.find('"') != std::string::npos ||
|
||||
field.find('\n') != std::string::npos ||
|
||||
field.find('\r') != std::string::npos;
|
||||
if (needs_quote) {
|
||||
out += '"';
|
||||
for (char c : field) {
|
||||
if (c == '"') out += '"';
|
||||
out += c;
|
||||
}
|
||||
out += '"';
|
||||
} else {
|
||||
out += field;
|
||||
}
|
||||
if (i + 1 < row.size()) out += delim;
|
||||
}
|
||||
out += '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
[[nodiscard]] inline std::string serialize(const Table& table,
|
||||
char delim = ',')
|
||||
{
|
||||
std::string out;
|
||||
for (const auto& row : table) {
|
||||
for (std::size_t i = 0; i < row.size(); ++i) {
|
||||
const auto& field = row[i];
|
||||
const bool needs_quote =
|
||||
field.find(delim) != std::string::npos ||
|
||||
field.find('"') != std::string::npos ||
|
||||
field.find('\n') != std::string::npos ||
|
||||
field.find('\r') != std::string::npos;
|
||||
if (needs_quote) {
|
||||
out += '"';
|
||||
for (char c : field) {
|
||||
if (c == '"') {
|
||||
out += '"';
|
||||
}
|
||||
out += c;
|
||||
}
|
||||
out += '"';
|
||||
} else {
|
||||
out += field;
|
||||
}
|
||||
if (i + 1 < row.size()) {
|
||||
out += delim;
|
||||
}
|
||||
}
|
||||
out += '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── with_header convenience ───────────────────────────────────────────────────
|
||||
// ── with_header convenience
|
||||
// ───────────────────────────────────────────────────
|
||||
|
||||
struct CsvWithHeader
|
||||
{
|
||||
Row header;
|
||||
Table rows;
|
||||
struct CsvWithHeader {
|
||||
Row header;
|
||||
Table rows;
|
||||
|
||||
[[nodiscard]] std::optional<std::size_t>
|
||||
col_index(std::string_view name) const
|
||||
{
|
||||
for (std::size_t i = 0; i < header.size(); ++i) {
|
||||
if (header[i] == name) return i;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
[[nodiscard]] std::optional<std::size_t>
|
||||
col_index(std::string_view name) const
|
||||
{
|
||||
for (std::size_t i = 0; i < header.size(); ++i) {
|
||||
if (header[i] == name) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<std::string_view>
|
||||
get(const Row& row, std::string_view name) const
|
||||
{
|
||||
auto idx = col_index(name);
|
||||
if (!idx || *idx >= row.size()) return std::nullopt;
|
||||
return row[*idx];
|
||||
}
|
||||
};
|
||||
[[nodiscard]] std::optional<std::string_view>
|
||||
get(const Row& row, std::string_view name) const
|
||||
{
|
||||
auto idx = col_index(name);
|
||||
if (!idx || *idx >= row.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return row[*idx];
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] inline std::optional<CsvWithHeader>
|
||||
parse_with_header(std::string_view text, char delim = ',')
|
||||
{
|
||||
auto table = parse(text, delim);
|
||||
if (table.empty()) return std::nullopt;
|
||||
CsvWithHeader result;
|
||||
result.header = std::move(table[0]);
|
||||
result.rows.assign(std::make_move_iterator(table.begin() + 1),
|
||||
std::make_move_iterator(table.end()));
|
||||
return result;
|
||||
}
|
||||
[[nodiscard]] inline std::optional<CsvWithHeader>
|
||||
parse_with_header(std::string_view text, char delim = ',')
|
||||
{
|
||||
auto table = parse(text, delim);
|
||||
if (table.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
CsvWithHeader result;
|
||||
result.header = std::move(table[0]);
|
||||
result.rows.assign(std::make_move_iterator(table.begin() + 1),
|
||||
std::make_move_iterator(table.end()));
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── reflective struct ↔ row
|
||||
// ─────────────────────────────────────────────────── Map a flat aggregate
|
||||
// to/from a CSV Row by reflecting its data members (column order =
|
||||
// declaration order; header = field names). Supported field types:
|
||||
// std::string, arithmetic, bool.
|
||||
|
||||
#ifdef VOID_HAS_META_REFLECTION
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
template <typename F>
|
||||
[[nodiscard]] std::string cell_to_string(const F& f)
|
||||
{
|
||||
if constexpr (std::same_as<F, std::string>) {
|
||||
return f;
|
||||
} else if constexpr (std::same_as<F, bool>) {
|
||||
return f ? "true" : "false";
|
||||
} else if constexpr (std::is_arithmetic_v<F>) {
|
||||
return std::format("{}", f);
|
||||
} else {
|
||||
return Void::Meta::repr(f);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
[[nodiscard]] bool cell_from_string(std::string_view s, F& f)
|
||||
{
|
||||
if constexpr (std::same_as<F, std::string>) {
|
||||
f = std::string{s};
|
||||
return true;
|
||||
} else if constexpr (std::same_as<F, bool>) {
|
||||
f = (s == "true" || s == "1");
|
||||
return true;
|
||||
} else if constexpr (std::is_integral_v<F>) {
|
||||
return std::from_chars(s.data(), s.data() + s.size(), f).ec ==
|
||||
std::errc{};
|
||||
} else if constexpr (std::is_floating_point_v<F>) {
|
||||
return std::from_chars(s.data(), s.data() + s.size(), f).ec ==
|
||||
std::errc{};
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
/// The header row (field names) for T.
|
||||
template <typename T> [[nodiscard]] Row header_row()
|
||||
{
|
||||
Row h;
|
||||
for (auto name : Void::Meta::field_names<T>()) {
|
||||
h.emplace_back(name);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Stringify a struct into a CSV Row (declaration order).
|
||||
template <typename T> [[nodiscard]] Row to_row(const T& v)
|
||||
{
|
||||
Row r;
|
||||
Void::Meta::for_each_field(v, [&](std::string_view, const auto& f) {
|
||||
r.push_back(Detail::cell_to_string(f));
|
||||
});
|
||||
return r;
|
||||
}
|
||||
|
||||
/// Parse a CSV Row back into a struct (by position). Returns nullopt if any
|
||||
/// cell fails to convert.
|
||||
template <typename T>
|
||||
[[nodiscard]] std::optional<T> from_row(const Row& row)
|
||||
{
|
||||
T out{};
|
||||
std::size_t i = 0;
|
||||
bool ok = true;
|
||||
Void::Meta::for_each_field(out, [&](std::string_view, auto& f) {
|
||||
if (i < row.size()) {
|
||||
ok = Detail::cell_from_string(row[i], f) && ok;
|
||||
}
|
||||
++i;
|
||||
});
|
||||
return ok ? std::optional<T>{std::move(out)} : std::nullopt;
|
||||
}
|
||||
|
||||
#endif // VOID_HAS_META_REFLECTION
|
||||
|
||||
} // namespace Void::Csv
|
||||
Reference in new issue
Block a user