Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more

Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now
an alias for Edit<string> so count_op/apply work on line diffs; Memoized
uses std::apply instead of type-based get to handle duplicate arg types.

874/877 tests pass (3 are platform-gated plugin tests).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 23:49:09 +03:00
1 parent cb4dd58500
commit 58ff7b38a0
65 files changed
+8987 -37

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
#include <Void/Argparse/Argparse.hpp>
#include <sstream>
namespace Void::Argparse
{
Parser::Parser(std::string program, std::string description)
: program_{std::move(program)}, description_{std::move(description)}
{}
Parser& Parser::flag(std::string_view lng, std::string_view sht,
std::string_view desc, bool& out)
{
flags_.push_back({std::string{lng}, std::string{sht}, std::string{desc}, &out});
return *this;
}
Parser& Parser::option(std::string_view lng, std::string_view sht,
std::string_view desc, std::optional<std::string>& out)
{
options_.push_back({std::string{lng}, std::string{sht}, std::string{desc}, {}, &out, nullptr});
return *this;
}
Parser& Parser::option_default(std::string_view lng, std::string_view sht,
std::string_view desc, std::string& out,
std::string_view default_value)
{
out = std::string{default_value};
options_.push_back({std::string{lng}, std::string{sht}, std::string{desc},
std::string{default_value}, nullptr, &out});
return *this;
}
Parser& Parser::positional(std::string_view name, std::string_view desc, std::string& out)
{
positionals_.push_back({std::string{name}, std::string{desc}, &out});
return *this;
}
std::expected<void, std::string>
Parser::parse(int argc, const char* const* argv) const
{
std::size_t pos_idx = 0;
for (int i = 1; i < argc; ++i) {
const std::string_view arg{argv[i]};
// Help
if (arg == "--help" || arg == "-h") {
return std::unexpected{help()};
}
// Flag check
bool matched_flag = false;
for (const auto& f : flags_) {
if (arg == f.lng || (!f.sht.empty() && arg == f.sht)) {
*f.out = true;
matched_flag = true;
break;
}
}
if (matched_flag) continue;
// Option check: --key=value or --key value or -k value
bool matched_opt = false;
for (const auto& opt : options_) {
std::string_view value;
bool found = false;
// --key=value
if (arg.size() > opt.lng.size() + 1 &&
arg.substr(0, opt.lng.size()) == opt.lng &&
arg[opt.lng.size()] == '=')
{
value = arg.substr(opt.lng.size() + 1);
found = true;
}
// --key value or -k value
else if (arg == opt.lng || (!opt.sht.empty() && arg == opt.sht)) {
if (i + 1 >= argc)
return std::unexpected{"option '" + std::string{arg} + "' requires a value"};
value = argv[++i];
found = true;
}
if (found) {
const std::string val_str{value};
if (opt.out_opt) *opt.out_opt = val_str;
if (opt.out_str) *opt.out_str = val_str;
matched_opt = true;
break;
}
}
if (matched_opt) continue;
// Unknown --/- prefixed arg
if (arg.size() > 1 && arg[0] == '-')
return std::unexpected{"unknown option: " + std::string{arg}};
// Positional
if (pos_idx >= positionals_.size())
return std::unexpected{"unexpected positional argument: " + std::string{arg}};
*positionals_[pos_idx++].out = std::string{arg};
}
// Check required positionals
for (std::size_t j = pos_idx; j < positionals_.size(); ++j)
return std::unexpected{"missing required argument: <" + positionals_[j].name + ">"};
return {};
}
std::string Parser::help() const
{
std::ostringstream os;
os << "Usage: " << program_;
for (const auto& f : flags_) os << " [" << f.lng << "]";
for (const auto& o : options_) {
const std::string_view dn = std::string_view{o.lng}.substr(o.lng.size() > 2 ? 2 : 0);
os << " [" << o.lng << " <" << dn << ">]";
}
for (const auto& p : positionals_) os << " <" << p.name << ">";
os << "\n";
if (!description_.empty()) os << "\n" << description_ << "\n";
if (!positionals_.empty()) {
os << "\nArguments:\n";
for (const auto& p : positionals_)
os << " <" << p.name << "> " << p.desc << "\n";
}
if (!flags_.empty()) {
os << "\nFlags:\n";
for (const auto& f : flags_) {
os << " " << f.lng;
if (!f.sht.empty()) os << ", " << f.sht;
os << " " << f.desc << "\n";
}
}
if (!options_.empty()) {
os << "\nOptions:\n";
for (const auto& o : options_) {
// Derive a display name from the long option (strip leading --)
const std::string_view dname = std::string_view{o.lng}.substr(
o.lng.size() > 2 ? 2 : 0);
os << " " << o.lng;
if (!o.sht.empty()) os << ", " << o.sht;
os << " <" << dname << ">";
if (!o.def.empty()) os << " (default: " << o.def << ")";
os << " " << o.desc << "\n";
}
}
os << " --help, -h show this help\n";
return os.str();
}
} // namespace Void::Argparse
+172
View File
@@ -0,0 +1,172 @@
#include <Void/Plugin/Loader.hpp>
#if defined(__unix__) || defined(__APPLE__)
# include <dlfcn.h>
# define VOID_HAS_DLOPEN 1
#endif
#include <filesystem>
#include <system_error>
#include <utility>
namespace Void::Plugin
{
std::string_view to_string(LoadError e) noexcept
{
switch (e) {
case LoadError::FileNotFound: return "file not found";
case LoadError::DlopenFailed: return "dlopen failed";
case LoadError::SymbolMissing: return "required symbol missing";
case LoadError::AbiMismatch: return "ABI version mismatch";
case LoadError::CreateFailed: return "plugin create() returned null";
case LoadError::NotSupported: return "platform does not support dlopen";
}
return "unknown";
}
// ── internal helpers ──────────────────────────────────────────────────────────
void PluginLoader::destroy_current() noexcept
{
#ifdef VOID_HAS_DLOPEN
if (plugin_) {
plugin_->on_unload();
if (destroy_) destroy_(plugin_);
plugin_ = nullptr;
}
if (handle_) {
dlclose(handle_);
handle_ = nullptr;
}
destroy_ = nullptr;
transfer_ = nullptr;
#endif
}
std::expected<void, LoadError>
PluginLoader::load_from(const std::filesystem::path& path)
{
#ifdef VOID_HAS_DLOPEN
void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
if (!h) return std::unexpected{LoadError::DlopenFailed};
const auto sym_info = reinterpret_cast<info_fn> (dlsym(h, kInfoSymbol));
const auto sym_create = reinterpret_cast<create_fn> (dlsym(h, kCreateSymbol));
const auto sym_destroy = reinterpret_cast<destroy_fn>(dlsym(h, kDestroySymbol));
if (!sym_info || !sym_create || !sym_destroy) {
dlclose(h);
return std::unexpected{LoadError::SymbolMissing};
}
const PluginInfo info = sym_info();
if (info.abi_version != ABI_VERSION) {
dlclose(h);
return std::unexpected{LoadError::AbiMismatch};
}
void* raw = sym_create();
if (!raw) {
dlclose(h);
return std::unexpected{LoadError::CreateFailed};
}
handle_ = h;
plugin_ = static_cast<IPlugin*>(raw);
destroy_ = sym_destroy;
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
info_ = info;
return {};
#else
(void)path;
return std::unexpected{LoadError::NotSupported};
#endif
}
// ── public API ────────────────────────────────────────────────────────────────
std::expected<PluginLoader, LoadError>
PluginLoader::load(std::filesystem::path path)
{
if (!std::filesystem::exists(path))
return std::unexpected{LoadError::FileNotFound};
PluginLoader ldr;
ldr.path_ = path;
if (auto r = ldr.load_from(path); !r)
return std::unexpected{r.error()};
ldr.plugin_->on_load();
return ldr;
}
PluginLoader::~PluginLoader() { destroy_current(); }
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
: handle_ {std::exchange(o.handle_, nullptr)}
, plugin_ {std::exchange(o.plugin_, nullptr)}
, destroy_ {std::exchange(o.destroy_, nullptr)}
, transfer_{std::exchange(o.transfer_, nullptr)}
, info_ {o.info_}
, path_ {std::move(o.path_)}
{}
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
{
if (this != &o) {
destroy_current();
handle_ = std::exchange(o.handle_, nullptr);
plugin_ = std::exchange(o.plugin_, nullptr);
destroy_ = std::exchange(o.destroy_, nullptr);
transfer_ = std::exchange(o.transfer_, nullptr);
info_ = o.info_;
path_ = std::move(o.path_);
}
return *this;
}
std::expected<void, LoadError> PluginLoader::reload()
{
#ifdef VOID_HAS_DLOPEN
// Copy to a unique temp path so dlopen gets a fresh inode (bypasses the
// path-based cache) even if the file was replaced in-place.
const auto tmp = path_.parent_path()
/ (path_.filename().string() + ".void_hotswap");
{
std::error_code ec;
std::filesystem::copy_file(path_, tmp,
std::filesystem::copy_options::overwrite_existing, ec);
if (ec) return std::unexpected{LoadError::FileNotFound};
}
// Load new instance while old is still alive (for state transfer).
PluginLoader next;
next.path_ = path_;
if (auto r = next.load_from(tmp); !r) {
std::error_code ec;
std::filesystem::remove(tmp, ec);
return r;
}
// Transfer state with both instances alive.
if (plugin_ && next.plugin_ && next.transfer_)
next.transfer_(plugin_, next.plugin_);
// Destroy old, install new.
destroy_current();
*this = std::move(next);
plugin_->on_load();
std::error_code ec;
std::filesystem::remove(tmp, ec);
return {};
#else
return std::unexpected{LoadError::NotSupported};
#endif
}
} // namespace Void::Plugin
+3
View File
@@ -0,0 +1,3 @@
void_add_module(Plugin
DEPS_UNIX dl
)