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
49
56{
57 // Comparison operators
58 friend bool operator==(const UnitSpec& lhs, const UnitSpec& rhs) noexcept
59 {
60 return lhs.category == rhs.category && lhs.name == rhs.name && lhs.namePlural == rhs.namePlural &&
61 lhs.abbreviation == rhs.abbreviation && lhs.f == rhs.f && lhs.x == rhs.x && lhs.y == rhs.y;
62 }
63
64 friend bool operator!=(const UnitSpec& lhs, const UnitSpec& rhs) noexcept { return !(lhs == rhs); }
65
66public:
68 std::string name;
69 std::string namePlural;
70 std::string abbreviation;
74};
75
77class Unit
78{
79 SEN_COPY_MOVE(Unit)
81
82public:
85 Unit(UnitSpec spec, Private notUsable) noexcept;
86 ~Unit() noexcept = default;
87
88public:
90 static std::unique_ptr<Unit> make(UnitSpec spec);
91
92 struct EnsureTag
93 {
94 };
95
96 static constexpr inline EnsureTag ensurePresent {};
97
98public:
100 [[nodiscard]] UnitCategory getCategory() const noexcept;
101
103 [[nodiscard]] std::string_view getName() const noexcept;
104
106 [[nodiscard]] std::string_view getNamePlural() const noexcept;
107
109 [[nodiscard]] std::string_view getAbbreviation() const noexcept;
110
112 [[nodiscard]] float64_t toSI(float64_t value) const noexcept;
113
115 [[nodiscard]] float64_t fromSI(float64_t value) const noexcept;
116
120 [[nodiscard]] Result<float64_t, std::string> fromString(const std::string& str) const noexcept;
121
123 [[nodiscard]] static float64_t convert(const Unit& from, const Unit& to, float64_t value) noexcept;
124
126 [[nodiscard]] static std::string_view getCategoryString(UnitCategory category) noexcept;
127
129 [[nodiscard]] MemberHash getHash() const noexcept;
130
131public:
132 [[nodiscard]] bool operator==(const Unit& other) const noexcept;
133 [[nodiscard]] bool operator!=(const Unit& other) const noexcept;
134
135private:
136 UnitSpec spec_;
137 MemberHash hash_;
138};
139
141
142//----------------------------------------------------------------------------------------------------------------------
143// Inline implementation
144//----------------------------------------------------------------------------------------------------------------------
145
147template <>
148struct impl::hash<UnitSpec>
149{
150 u32 operator()(const UnitSpec& spec) const noexcept
151 {
152 return hashCombine(hashSeed, spec.category, spec.name, spec.namePlural, spec.abbreviation, spec.f, spec.x, spec.y);
153 }
154};
155
156} // namespace sen
157
158#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:96
float64_t fromSI(float64_t value) const noexcept
Converts 'value' given in SI to this unit.
Definition unit.h:93
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:33
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:214
#define SEN_PRIVATE_TAG
Definition compiler_macros.h:490
UnitCategory
Type of measurement.
Definition unit.h:30
@ time
Definition unit.h:34
@ angularAcceleration
Definition unit.h:47
@ length
Definition unit.h:32
@ density
Definition unit.h:37
@ area
Definition unit.h:39
@ angle
Definition unit.h:35
@ velocity
Definition unit.h:44
@ force
Definition unit.h:40
@ acceleration
Definition unit.h:46
@ mass
Definition unit.h:33
@ pressure
Definition unit.h:38
@ temperature
Definition unit.h:36
@ angularVelocity
Definition unit.h:45
@ frequency
Definition unit.h:43
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:17
STL namespace.
The hash of a member.
Definition type.h:39
Data about a given unit definition. Given f, x, y:
Definition unit.h:56
std::string namePlural
Plural name (i.e. "meters").
Definition unit.h:69
float64_t x
Offset to convert to SI.
Definition unit.h:72
float64_t f
Multiplication factor to convert to SI.
Definition unit.h:71
std::string abbreviation
Unique abbreviation (i.e. "m").
Definition unit.h:70
std::string name
Plain name (i.e. "meter").
Definition unit.h:68
friend bool operator==(const UnitSpec &lhs, const UnitSpec &rhs) noexcept
Definition unit.h:58
friend bool operator!=(const UnitSpec &lhs, const UnitSpec &rhs) noexcept
Definition unit.h:64
float64_t y
Offset to convert to SI.
Definition unit.h:73
UnitCategory category
The type of measurement.
Definition unit.h:67