Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
|
|
namespace Void::Counted
|
|
{
|
|
|
|
// ── InstanceCounter<Tag> ──────────────────────────────────────────────────────
|
|
// Mix-in that counts how many instances of a class exist.
|
|
// Use a unique Tag type per class to keep counters separate.
|
|
//
|
|
// struct MyClass : InstanceCounter<MyClass> { ... };
|
|
// std::size_t alive = MyClass::instance_count(); // thread-safe
|
|
|
|
template <typename Tag>
|
|
class InstanceCounter
|
|
{
|
|
public:
|
|
InstanceCounter() noexcept { count_.fetch_add(1, std::memory_order_relaxed); }
|
|
~InstanceCounter() noexcept { count_.fetch_sub(1, std::memory_order_relaxed); }
|
|
|
|
InstanceCounter(const InstanceCounter&) noexcept { count_.fetch_add(1, std::memory_order_relaxed); }
|
|
InstanceCounter(InstanceCounter&&) noexcept { count_.fetch_add(1, std::memory_order_relaxed); }
|
|
InstanceCounter& operator=(const InstanceCounter&) noexcept { return *this; }
|
|
InstanceCounter& operator=(InstanceCounter&&) noexcept { return *this; }
|
|
|
|
[[nodiscard]] static std::size_t instance_count() noexcept
|
|
{
|
|
return count_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
private:
|
|
static std::atomic<std::size_t> count_;
|
|
};
|
|
|
|
template <typename Tag>
|
|
std::atomic<std::size_t> InstanceCounter<Tag>::count_{0};
|
|
|
|
// ── AtomicCounter ─────────────────────────────────────────────────────────────
|
|
// Standalone thread-safe counter with increment/decrement/reset.
|
|
|
|
class AtomicCounter
|
|
{
|
|
public:
|
|
explicit AtomicCounter(std::size_t initial = 0) noexcept : value_{initial} {}
|
|
|
|
std::size_t increment(std::size_t by = 1) noexcept
|
|
{
|
|
return value_.fetch_add(by, std::memory_order_relaxed) + by;
|
|
}
|
|
|
|
std::size_t decrement(std::size_t by = 1) noexcept
|
|
{
|
|
return value_.fetch_sub(by, std::memory_order_relaxed) - by;
|
|
}
|
|
|
|
void reset(std::size_t v = 0) noexcept { value_.store(v, std::memory_order_relaxed); }
|
|
|
|
[[nodiscard]] std::size_t get() const noexcept
|
|
{
|
|
return value_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
[[nodiscard]] std::size_t operator++(int) noexcept
|
|
{
|
|
return value_.fetch_add(1, std::memory_order_relaxed);
|
|
}
|
|
|
|
[[nodiscard]] std::size_t operator--(int) noexcept
|
|
{
|
|
return value_.fetch_sub(1, std::memory_order_relaxed);
|
|
}
|
|
|
|
AtomicCounter& operator++() noexcept { increment(); return *this; }
|
|
AtomicCounter& operator--() noexcept { decrement(); return *this; }
|
|
|
|
private:
|
|
std::atomic<std::size_t> value_;
|
|
};
|
|
|
|
// ── SeqId ────────────────────────────────────────────────────────────────────
|
|
// Monotonically increasing sequence ID generator.
|
|
// Each SeqId<Tag> has its own counter, starting from 1.
|
|
|
|
template <typename Tag = void>
|
|
class SeqId
|
|
{
|
|
public:
|
|
SeqId() : id_{next_.fetch_add(1, std::memory_order_relaxed)} {}
|
|
|
|
[[nodiscard]] std::size_t id() const noexcept { return id_; }
|
|
[[nodiscard]] static std::size_t next_id() noexcept
|
|
{
|
|
return next_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
private:
|
|
static std::atomic<std::size_t> next_;
|
|
std::size_t id_;
|
|
};
|
|
|
|
template <typename Tag>
|
|
std::atomic<std::size_t> SeqId<Tag>::next_{1};
|
|
|
|
} // namespace Void::Counted
|