Sen API
Sen Libraries
Loading...
Searching...
No Matches
unit.h
Go to the documentation of this file.
1// === unit.h ==========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_META_UNIT_H
9#define SEN_CORE_META_UNIT_H
10
11// sen
15#include "sen/core/meta/type.h"
16
17// std
18#include <memory>
19#include <string>
20#include <string_view>
21
22namespace sen
23{
24
27
29enum class UnitCategory
30{
31 // Base measurements
33 mass, // Note: Base unit is gram (g), not kilogram (kg). This keeps internal consistency with density (g/cm3), though
34 // it differs from the SI-canonical base (kg)
42
43 // Derived measurements
50};
51
58{
59 // Comparison operators
60 friend bool operator==(const UnitSpec& lhs, const UnitSpec& rhs) noexcept
61 {
62 return lhs.category == rhs.category && lhs.name == rhs.name && lhs.namePlural == rhs.namePlural &&
63 lhs.abbreviation == rhs.abbreviation && lhs.f == rhs.f && lhs.x == rhs.x && lhs.y == rhs.y;
64 }
65
66 friend bool operator!=(const UnitSpec& lhs, const UnitSpec& rhs) noexcept { return !(lhs == rhs); }
67
68public:
70 std::string name;
71 std::string namePlural;
72 std::string abbreviation;
76};
77
79class Unit
80{
81 SEN_COPY_MOVE(Unit)
83
84public:
87 Unit(UnitSpec spec, Private notUsable) noexcept;
88 ~Unit() noexcept = default;
89
90public:
92 static std::unique_ptr<Unit> make(UnitSpec spec);
93
94 struct EnsureTag
95 {
96 };
97
98 static constexpr inline EnsureTag ensurePresent {};
99
100public:
102 [[nodiscard]] UnitCategory getCategory() const noexcept;
103
105 [[nodiscard]] std::string_view getName() const noexcept;
106
108 [[nodiscard]] std::string_view getNamePlural() const noexcept;
109
111 [[nodiscard]] std::string_view getAbbreviation() const noexcept;
112
114 [[nodiscard]] float64_t toSI(float64_t value) const noexcept;
115
117 [[nodiscard]] float64_t fromSI(float64_t value) const noexcept;
118
122 [[nodiscard]] Result<float64_t, std::string> fromString(const std::string& str) const noexcept;
123
125 [[nodiscard]] static float64_t convert(const Unit& from, const Unit& to, float64_t value) noexcept;
126
128 [[nodiscard]] static std::string_view getCategoryString(UnitCategory category) noexcept;
129
131 [[nodiscard]] MemberHash getHash() const noexcept;
132
133public:
134 [[nodiscard]] bool operator==(const Unit& other) const noexcept;
135 [[nodiscard]] bool operator!=(const Unit& other) const noexcept;
136
137private:
138 UnitSpec spec_;
139 MemberHash hash_;
140};
141
143
144//----------------------------------------------------------------------------------------------------------------------
145// Inline implementation
146//----------------------------------------------------------------------------------------------------------------------
147
149template <>
150struct impl::hash<UnitSpec>
151{
152 u32 operator()(const UnitSpec& spec) const noexcept
153 {
154 return hashCombine(hashSeed, spec.category, spec.name, spec.namePlural, spec.abbreviation, spec.f, spec.x, spec.y);
155 }
156};
157
158} // namespace sen
159
160#endif // SEN_CORE_META_UNIT_H
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Unit(UnitSpec spec, Private notUsable) noexcept
Constructor is only usable by this class. Use make (see below).
Result< float64_t, std::string > fromString(const std::string &str) const noexcept
Parses a string, searching for a valid unit specification within the same category....
~Unit() noexcept=default
static float64_t convert(const Unit &from, const Unit &to, float64_t value) noexcept
Converts a a value between units.
std::string_view getAbbreviation() const noexcept
Unique abbreviation.
static std::unique_ptr< Unit > make(UnitSpec spec)
Moves the spec into a member.
std::string_view getName() const noexcept
Plain name.
UnitCategory getCategory() const noexcept
Measurement type.
float64_t toSI(float64_t value) const noexcept
Converts 'value' given in this unit to an equivalent SI unit.
std::string_view getNamePlural() const noexcept
Plural name.
static std::string_view getCategoryString(UnitCategory category) noexcept
A string that describes the category.
MemberHash getHash() const noexcept
Returns a unique hash computed for the UnitSpec at compilation time.
static constexpr EnsureTag ensurePresent
Tag to request that a unit is required.
Definition unit.h:98
float64_t fromSI(float64_t value) const noexcept
Converts 'value' given in SI to this unit.
Definition unit.h:95
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:36
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:226
#define SEN_PRIVATE_TAG
Definition compiler_macros.h:490
UnitCategory
Type of measurement.
Definition unit.h:30
@ time
Definition unit.h:35
@ angularAcceleration
Definition unit.h:48
@ length
Definition unit.h:32
@ density
Definition unit.h:38
@ area
Definition unit.h:40
@ angle
Definition unit.h:36
@ velocity
Definition unit.h:45
@ force
Definition unit.h:41
@ acceleration
Definition unit.h:47
@ mass
Definition unit.h:33
@ pressure
Definition unit.h:39
@ torque
Definition unit.h:49
@ temperature
Definition unit.h:37
@ angularVelocity
Definition unit.h:46
@ frequency
Definition unit.h:44
double float64_t
Definition numbers.h:17
uint32_t u32
Definition numbers.h:25
This file contains functions related to hashing and compression. This is mainly used by Sen internals...
Definition assert.h:19
STL namespace.
The hash of a member.
Definition type.h:39
Data about a given unit definition. Given f, x, y:
Definition unit.h:58
std::string namePlural
Plural name (i.e. "meters").
Definition unit.h:71
float64_t x
Offset to convert to SI.
Definition unit.h:74
float64_t f
Multiplication factor to convert to SI.
Definition unit.h:73
std::string abbreviation
Unique abbreviation (i.e. "m").
Definition unit.h:72
std::string name
Plain name (i.e. "meter").
Definition unit.h:70
friend bool operator==(const UnitSpec &lhs, const UnitSpec &rhs) noexcept
Definition unit.h:60
friend bool operator!=(const UnitSpec &lhs, const UnitSpec &rhs) noexcept
Definition unit.h:66
float64_t y
Offset to convert to SI.
Definition unit.h:75
UnitCategory category
The type of measurement.
Definition unit.h:69