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
50
57{
58 // Comparison operators
59 friend bool operator==(const UnitSpec& lhs, const UnitSpec& rhs) noexcept
60 {
61 return lhs.category == rhs.category && lhs.name == rhs.name && lhs.namePlural == rhs.namePlural &&
62 lhs.abbreviation == rhs.abbreviation && lhs.f == rhs.f && lhs.x == rhs.x && lhs.y == rhs.y;
63 }
64
65 friend bool operator!=(const UnitSpec& lhs, const UnitSpec& rhs) noexcept { return !(lhs == rhs); }
66
67public:
69 std::string name;
70 std::string namePlural;
71 std::string abbreviation;
75};
76
78class Unit
79{
80 SEN_COPY_MOVE(Unit)
82
83public:
86 Unit(UnitSpec spec, Private notUsable) noexcept;
87 ~Unit() noexcept = default;
88
89public:
91 static std::unique_ptr<Unit> make(UnitSpec spec);
92
93 struct EnsureTag
94 {
95 };
96
97 static constexpr inline EnsureTag ensurePresent {};
98
99public:
101 [[nodiscard]] UnitCategory getCategory() const noexcept;
102
104 [[nodiscard]] std::string_view getName() const noexcept;
105
107 [[nodiscard]] std::string_view getNamePlural() const noexcept;
108
110 [[nodiscard]] std::string_view getAbbreviation() const noexcept;
111
113 [[nodiscard]] float64_t toSI(float64_t value) const noexcept;
114
116 [[nodiscard]] float64_t fromSI(float64_t value) const noexcept;
117
121 [[nodiscard]] Result<float64_t, std::string> fromString(const std::string& str) const noexcept;
122
124 [[nodiscard]] static float64_t convert(const Unit& from, const Unit& to, float64_t value) noexcept;
125
127 [[nodiscard]] static std::string_view getCategoryString(UnitCategory category) noexcept;
128
130 [[nodiscard]] MemberHash getHash() const noexcept;
131
132public:
133 [[nodiscard]] bool operator==(const Unit& other) const noexcept;
134 [[nodiscard]] bool operator!=(const Unit& other) const noexcept;
135
136private:
137 UnitSpec spec_;
138 MemberHash hash_;
139};
140
142
143//----------------------------------------------------------------------------------------------------------------------
144// Inline implementation
145//----------------------------------------------------------------------------------------------------------------------
146
148template <>
149struct impl::hash<UnitSpec>
150{
151 u32 operator()(const UnitSpec& spec) const noexcept
152 {
153 return hashCombine(hashSeed, spec.category, spec.name, spec.namePlural, spec.abbreviation, spec.f, spec.x, spec.y);
154 }
155};
156
157} // namespace sen
158
159#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:97
float64_t fromSI(float64_t value) const noexcept
Converts 'value' given in SI to this unit.
Definition unit.h:94
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:34
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:215
#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
@ torque
Definition unit.h:48
@ 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:57
std::string namePlural
Plural name (i.e. "meters").
Definition unit.h:70
float64_t x
Offset to convert to SI.
Definition unit.h:73
float64_t f
Multiplication factor to convert to SI.
Definition unit.h:72
std::string abbreviation
Unique abbreviation (i.e. "m").
Definition unit.h:71
std::string name
Plain name (i.e. "meter").
Definition unit.h:69
friend bool operator==(const UnitSpec &lhs, const UnitSpec &rhs) noexcept
Definition unit.h:59
friend bool operator!=(const UnitSpec &lhs, const UnitSpec &rhs) noexcept
Definition unit.h:65
float64_t y
Offset to convert to SI.
Definition unit.h:74
UnitCategory category
The type of measurement.
Definition unit.h:68