gtopt::UnitRegistry class

Public static functions

static auto instance() -> const UnitRegistry&

Constructors, destructors, conversion operators

UnitRegistry(const std::filesystem::path& json_path) explicit
Construct from a JSON file. Throws on read or parse error.
UnitRegistry(std::string_view json_content) explicit

Public functions

auto class_agnostic_unit_for(std::string_view canonical, std::string_view dialect) const -> std::optional<std::string_view> noexcept
auto size() const -> std::size_t noexcept
Number of (class, canonical, dialect) entries the registry holds.
auto source_path() const -> const std::optional<std::filesystem::path>& noexcept
auto unit_for(std::string_view class_name, std::string_view canonical) const -> std::optional<std::string_view> noexcept
auto unit_for(std::string_view class_name, std::string_view canonical, std::string_view dialect) const -> std::optional<std::string_view> noexcept

Function documentation

static const UnitRegistry& gtopt::UnitRegistry::instance()

Process-wide singleton, lazily loaded on first call. The map is const after construction, so reads are thread-safe.

gtopt::UnitRegistry::UnitRegistry(std::string_view json_content) explicit

Construct from an explicit JSON string (tests + the instance() boot path). Throws std::runtime_error on parse error.

std::optional<std::string_view> gtopt::UnitRegistry::class_agnostic_unit_for(std::string_view canonical, std::string_view dialect) const noexcept

Class-agnostic lookup: returns the single unit string if every registered (class, canonical, dialect) entry for the (canonical, dialect) pair agrees on the same unit. Returns nullopt when the canonical has no entries in the given dialect, or when distinct entries disagree on the unit.

Used by canonicalize_json_keys where the alias rewrite is class-blind — the JSON tokeniser does not know which element type the key belongs to. Ambiguity (multiple classes, different units) is treated as "skip the unit-mismatch warning" rather than a hard error so the rewrite path stays robust.

const std::optional<std::filesystem::path>& gtopt::UnitRegistry::source_path() const noexcept

Path the registry was loaded from, or nullopt if loaded from an explicit JSON string or the built-in fallback.

std::optional<std::string_view> gtopt::UnitRegistry::unit_for(std::string_view class_name, std::string_view canonical) const noexcept

Convenience overload assuming the gtopt-native dialect. Useful at the LP-construction layer where canonical names are already in scope and the dialect is implied.

std::optional<std::string_view> gtopt::UnitRegistry::unit_for(std::string_view class_name, std::string_view canonical, std::string_view dialect) const noexcept

Look up the unit string for a (class, canonical, dialect) tuple. Returns nullopt when the entry is not registered — callers should fall through to canonical / no-unit handling, never assume.