Global Access and Testing
detent::global
Section titled “detent::global”#include "detent/global.hh"
namespace detent::global {void init(InitOptions options);void shutdown();
Result<std::int64_t, KnobError> get(std::string_view name, std::optional<std::string_view> hash_val = std::nullopt, std::optional<std::string_view> switch_val = std::nullopt);template <typename T>Result<T, KnobError> get_as(/* same as get */);std::int64_t get_or_panic(/* same as get */);HealthSnapshot health();}A thin adapter over one process-wide Client, for code that can’t easily have
a client injected.
init()constructs the client and throws on failure, like the constructor. Calling it twice throwsstd::logic_error.- Reading before
init()returnsUninitialized. shutdown()releases the client and stops its updater; it’s mainly for test teardown.- A thread-local test client, installed by
TestKnobs, takes precedence over the process-wide one.
Prefer passing a Client& (or a narrower interface of your own) where you
can: it makes dependencies explicit and tests simpler.
detent::testing
Section titled “detent::testing”#include "detent/testing.hh"
namespace detent::testing {class TestKnobs {public: explicit TestKnobs(Snapshot snapshot); [[nodiscard]] TestGuard set_thread_local() &&;};
class TestGuard { // move-onlypublic: ~TestGuard(); // restores the previous thread-local client};}detent::Snapshot snap;snap.knobs["checkout:new_path"] = detent::KnobDef{.default_value = 1};
{ auto guard = detent::testing::TestKnobs(snap).set_thread_local(); // detent::global::* reads `snap` on this thread. { auto inner = detent::testing::TestKnobs(other).set_thread_local(); // ...now reads `other`. } // Back to `snap`.}// Back to whatever was active before.Guards nest, are scoped to the calling thread, and restore the previous state when destroyed, so one test can’t leak knobs into another.
CLI Overrides
Section titled “CLI Overrides”#include "detent/overrides.hh"
Result<OverrideTable, std::string> parse_cli_overrides(const std::vector<std::string>& raw_pairs);The client calls this for InitOptions::cli_overrides; you can call it
yourself to validate flags early. Each entry is name=value:
trueandfalse(lowercase only) are 1 and 0.- Anything else must parse as a signed base-10 64-bit integer.
- Duplicate names fail, naming the offending entry.
Overrides are immutable after startup. When there are none, the read path pays one null-pointer check.