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
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Core/Meta.hpp>
|
||||
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -9,54 +11,156 @@
|
||||
namespace Void::Argparse
|
||||
{
|
||||
|
||||
// ── Parser ────────────────────────────────────────────────────────────────────
|
||||
// Fluent command-line argument parser.
|
||||
//
|
||||
// Supports:
|
||||
// --flag, -f boolean flag (default false, set to true by presence)
|
||||
// --key=value option with value (= or space separator)
|
||||
// --key value option with value
|
||||
// positional args collected in declaration order
|
||||
// --help / -h auto-generated help text
|
||||
// ── Parser
|
||||
// ────────────────────────────────────────────────────────────────────
|
||||
// Fluent command-line argument parser.
|
||||
//
|
||||
// Supports:
|
||||
// --flag, -f boolean flag (default false, set to true by
|
||||
// presence)
|
||||
// --key=value option with value (= or space separator)
|
||||
// --key value option with value
|
||||
// positional args collected in declaration order
|
||||
// --help / -h auto-generated help text
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
explicit Parser(std::string program, std::string description = {});
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
|
||||
// Add a boolean flag.
|
||||
// long_name: e.g. "--verbose" short_name: e.g. "-v" (short_name may be "")
|
||||
Parser& flag(std::string_view long_name, std::string_view short_name,
|
||||
std::string_view description, bool& out);
|
||||
explicit Parser(std::string program, std::string description = {});
|
||||
|
||||
// Add an option that takes a value. If not provided, out stays nullopt.
|
||||
Parser& option(std::string_view long_name, std::string_view short_name,
|
||||
std::string_view description, std::optional<std::string>& out);
|
||||
// Add a boolean flag.
|
||||
// long_name: e.g. "--verbose" short_name: e.g. "-v" (short_name
|
||||
// may be "")
|
||||
Parser& flag(std::string_view long_name,
|
||||
std::string_view short_name,
|
||||
std::string_view description, bool& out);
|
||||
|
||||
// Add an option with a default value. out is always set after parse().
|
||||
Parser& option_default(std::string_view long_name, std::string_view short_name,
|
||||
std::string_view description, std::string& out,
|
||||
std::string_view default_value = "");
|
||||
// Add an option that takes a value. If not provided, out stays
|
||||
// nullopt.
|
||||
Parser& option(std::string_view long_name,
|
||||
std::string_view short_name,
|
||||
std::string_view description,
|
||||
std::optional<std::string>& out);
|
||||
|
||||
// Add a required positional argument.
|
||||
Parser& positional(std::string_view name, std::string_view description, std::string& out);
|
||||
// Add an option with a default value. out is always set after
|
||||
// parse().
|
||||
Parser& option_default(std::string_view long_name,
|
||||
std::string_view short_name,
|
||||
std::string_view description,
|
||||
std::string& out,
|
||||
std::string_view default_value = "");
|
||||
|
||||
// Parse argc/argv. Returns error message on failure.
|
||||
[[nodiscard]] std::expected<void, std::string>
|
||||
parse(int argc, const char* const* argv) const;
|
||||
// Add a required positional argument.
|
||||
Parser& positional(std::string_view name,
|
||||
std::string_view description, std::string& out);
|
||||
|
||||
// Returns formatted help text.
|
||||
[[nodiscard]] std::string help() const;
|
||||
// Parse argc/argv. Returns error message on failure.
|
||||
[[nodiscard]] std::expected<void, std::string>
|
||||
parse(int argc, const char* const* argv) const;
|
||||
|
||||
private:
|
||||
struct FlagEntry { std::string lng, sht, desc; bool* out; };
|
||||
struct OptionEntry{ std::string lng, sht, desc, def; std::optional<std::string>* out_opt; std::string* out_str; };
|
||||
struct PosEntry { std::string name, desc; std::string* out; };
|
||||
// Returns formatted help text.
|
||||
[[nodiscard]] std::string help() const;
|
||||
|
||||
std::string program_, description_;
|
||||
std::vector<FlagEntry> flags_;
|
||||
std::vector<OptionEntry> options_;
|
||||
std::vector<PosEntry> positionals_;
|
||||
};
|
||||
private:
|
||||
|
||||
struct FlagEntry {
|
||||
std::string lng, sht, desc;
|
||||
bool* out;
|
||||
};
|
||||
struct OptionEntry {
|
||||
std::string lng, sht, desc, def;
|
||||
std::optional<std::string>* out_opt;
|
||||
std::string* out_str;
|
||||
};
|
||||
struct PosEntry {
|
||||
std::string name, desc;
|
||||
std::string* out;
|
||||
};
|
||||
|
||||
std::string program_, description_;
|
||||
std::vector<FlagEntry> flags_;
|
||||
std::vector<OptionEntry> options_;
|
||||
std::vector<PosEntry> positionals_;
|
||||
};
|
||||
|
||||
// ── reflective options struct
|
||||
// ───────────────────────────────────────────────── Define a struct,
|
||||
// annotate its fields, and parse straight into it:
|
||||
//
|
||||
// struct Opts {
|
||||
// [[= arg::Short{'v'}]] [[= arg::Help{"verbose output"}]] bool
|
||||
// verbose = false;
|
||||
// [[= arg::Help{"output path"}]] std::string
|
||||
// out = "a.out"; std::optional<std::string> config; // --config
|
||||
// <v>
|
||||
// [[= arg::Positional{}]] std::string input;
|
||||
// };
|
||||
// auto o = Argparse::parse<Opts>(argc, argv); // std::expected<Opts,
|
||||
// string>
|
||||
//
|
||||
// Field → argument: bool → flag, std::string → option with default (the
|
||||
// field's initializer), std::optional<std::string> → optional option, a
|
||||
// field tagged
|
||||
// [[= arg::Positional]] → positional. Other field types are ignored.
|
||||
|
||||
struct Short {
|
||||
char c;
|
||||
};
|
||||
struct Help {
|
||||
Void::Meta::Str text;
|
||||
};
|
||||
struct Name {
|
||||
Void::Meta::Str text;
|
||||
}; // long-name override (without "--")
|
||||
struct Positional {
|
||||
};
|
||||
|
||||
#ifdef VOID_HAS_META_REFLECTION
|
||||
|
||||
template <typename Opts>
|
||||
[[nodiscard]] std::expected<Opts, std::string>
|
||||
parse(int argc, const char* const* argv, std::string program = "app",
|
||||
std::string description = {})
|
||||
{
|
||||
Opts out{};
|
||||
Parser parser{std::move(program), std::move(description)};
|
||||
|
||||
template for (constexpr auto m : Void::Meta::members_v<Opts>)
|
||||
{
|
||||
using MT = [:std::meta::type_of(m):];
|
||||
constexpr auto nm = Void::Meta::annotation_of<Name>(m);
|
||||
const std::string lng =
|
||||
std::string{"--"} +
|
||||
(nm.has_value() ? std::string{nm->text.view()}
|
||||
: std::string{std::meta::identifier_of(m)});
|
||||
constexpr auto sh = Void::Meta::annotation_of<Short>(m);
|
||||
const std::string sht =
|
||||
sh.has_value() ? (std::string{"-"} + sh->c) : std::string{};
|
||||
constexpr auto hp = Void::Meta::annotation_of<Help>(m);
|
||||
const std::string desc =
|
||||
hp.has_value() ? std::string{hp->text.view()} : std::string{};
|
||||
|
||||
if constexpr (Void::Meta::has_annotation<Positional>(m)) {
|
||||
parser.positional(std::meta::identifier_of(m), desc, out.[:m:]);
|
||||
} else if constexpr (std::same_as<MT, bool>) {
|
||||
parser.flag(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (Void::Meta::is_optional_v<MT>) {
|
||||
parser.option(lng, sht, desc, out.[:m:]);
|
||||
} else if constexpr (std::same_as<MT, std::string>) {
|
||||
const std::string def = out.[:m:];
|
||||
parser.option_default(lng, sht, desc, out.[:m:], def);
|
||||
}
|
||||
// other field types: ignored
|
||||
}
|
||||
|
||||
auto r = parser.parse(argc, argv);
|
||||
if (!r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
#endif // VOID_HAS_META_REFLECTION
|
||||
|
||||
} // namespace Void::Argparse
|
||||
Reference in new issue
Block a user