Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
22
23#include "shamunits/Unit.hpp"
24#include <string_view>
25#include <span>
26#include <stdexcept>
27#include <string>
28
29namespace shamunits {
30
32 struct NamedUnit {
33 std::string_view long_name;
34 std::string_view short_name;
36
38 constexpr NamedUnit(std::string_view lname, std::string_view sname, Unit<> u) noexcept
39 : long_name(lname), short_name(sname), unit(u) {}
40 };
41
42} // namespace shamunits
43
44namespace shamunits::details {
45
47 constexpr const NamedUnit *find_named(
48 std::span<const NamedUnit> registry, std::string_view name) noexcept {
49 for (const NamedUnit &e : registry) {
50 if (e.long_name == name || e.short_name == name) {
51 return &e;
52 }
53 }
54 return nullptr;
55 }
56
59 std::span<const NamedUnit> registry,
60 std::string_view name,
61 std::string_view kind = "unit") {
62 if (const NamedUnit *e = find_named(registry, name); e != nullptr) {
63 return e->unit;
64 }
65 throw std::invalid_argument(
66 "unknown " + std::string(kind) + " name : '" + std::string(name) + "'");
67 }
68
70 constexpr bool names_are_valid(std::span<const NamedUnit> registry) noexcept {
71 for (std::size_t i = 0; i < registry.size(); i++) {
72 const NamedUnit &a = registry[i];
73 if (a.long_name.empty() || a.short_name.empty()) {
74 return false;
75 }
76 for (std::size_t j = i + 1; j < registry.size(); j++) {
77 const NamedUnit &b = registry[j];
78 if (a.long_name == b.long_name || a.long_name == b.short_name
79 || a.short_name == b.long_name || a.short_name == b.short_name) {
80 return false;
81 }
82 }
83 }
84 return true;
85 }
86
87} // namespace shamunits::details
A physical unit as a value: its SI dimension and its magnitude in SI base units.
namespace containing the units library
constexpr const NamedUnit * find_named(std::span< const NamedUnit > registry, std::string_view name) noexcept
Entry whose long or short name is name, or nullptr.
Definition registry.hpp:47
Unit unit_from_name(std::span< const NamedUnit > registry, std::string_view name, std::string_view kind="unit")
Unit named name in registry; throws std::invalid_argument when unknown.
Definition registry.hpp:58
constexpr bool names_are_valid(std::span< const NamedUnit > registry) noexcept
True when no name is empty and no name appears twice, long and short names together.
Definition registry.hpp:70
One registry entry: a unit and the two names it can be looked up by.
Definition registry.hpp:32
Unit unit
the unit, always in the reference precision (double)
Definition registry.hpp:35
std::string_view short_name
e.g. "m", "au"
Definition registry.hpp:34
std::string_view long_name
e.g. "metre", "astronomical_unit"
Definition registry.hpp:33
constexpr NamedUnit(std::string_view lname, std::string_view sname, Unit<> u) noexcept
Constructor, so registry rows stay compact: {"metre", "m", metre}.
Definition registry.hpp:38
A unit: its dimension and its magnitude expressed in SI base units, in precision Tscal.
Definition Unit.hpp:89