Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
Length.hpp
Go to the documentation of this file.
1// Copyright © 2020-2024 Alexandre Coderre-Chabot
2//
3// This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4// models, and units of measure for scientific computing.
5//
6// Physical Quantities is hosted at:
7// https://github.com/acodcha/phq
8//
9// Physical Quantities is licensed under the MIT License:
10// https://mit-license.org
11//
12// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13// associated documentation files (the "Software"), to deal in the Software without restriction,
14// including without limitation the rights to use, copy, modify, merge, publish, distribute,
15// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16// furnished to do so, subject to the following conditions:
17// - The above copyright notice and this permission notice shall be included in all copies or
18// substantial portions of the Software.
19// - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25#ifndef PHQ_DIMENSION_LENGTH_HPP
26#define PHQ_DIMENSION_LENGTH_HPP
27
28#include <cstddef>
29#include <cstdint>
30#include <functional>
31#include <iostream>
32#include <string>
33#include <string_view>
34
35namespace PhQ::Dimension {
36
37/// \brief Base physical dimension of length. Typically denoted "L". One of seven independent base
38/// physical dimensions that form the physical dimension set of any unit of measure or physical
39/// quantity. Part of PhQ::Dimensions.
40class Length {
41public:
42 /// \brief Default constructor. Constructs a base physical dimension of length with a value of
43 /// zero.
44 constexpr Length() = default;
45
46 /// \brief Constructor. Constructs a base physical dimension of length with a given value.
47 explicit constexpr Length(const int8_t value) : value(value) {}
48
49 /// \brief Destructor. Destroys this base physical dimension of length.
50 ~Length() noexcept = default;
51
52 /// \brief Copy constructor. Constructs a base physical dimension of length by copying another
53 /// one.
54 constexpr Length(const Length& other) = default;
55
56 /// \brief Copy assignment operator. Assigns the value of this base physical dimension of length
57 /// by copying from another one.
58 constexpr Length& operator=(const Length& other) = default;
59
60 /// \brief Move constructor. Constructs a base physical dimension of length by moving another one.
61 constexpr Length(Length&& other) noexcept = default;
62
63 /// \brief Move assignment operator. Assigns the value of this base physical dimension of length
64 /// by moving another one.
65 constexpr Length& operator=(Length&& other) noexcept = default;
66
67 /// \brief Value of this base physical dimension.
68 [[nodiscard]] constexpr int8_t Value() const noexcept {
69 return value;
70 }
71
72 /// \brief Abbreviation of this base physical dimension.
73 static std::string_view Abbreviation() noexcept {
74 return "L";
75 }
76
77 /// \brief Label of this base physical dimension.
78 static std::string_view Label() noexcept {
79 return "Length";
80 }
81
82 /// \brief Prints this base physical dimension as a string.
83 [[nodiscard]] std::string Print() const noexcept {
84 if (value == 0) {
85 return {};
86 }
87 std::string string{Abbreviation()};
88 if (value > 1) {
89 string.append("^" + std::to_string(value));
90 } else if (value < 0) {
91 string.append("^(" + std::to_string(value) + ")");
92 }
93 return string;
94 }
95
96private:
97 /// \brief Value of this base physical dimension.
98 int8_t value{0};
99};
100
101inline constexpr bool operator==(const Length& left, const Length& right) noexcept {
102 return left.Value() == right.Value();
103}
104
105inline constexpr bool operator!=(const Length& left, const Length& right) noexcept {
106 return left.Value() != right.Value();
107}
108
109inline constexpr bool operator<(const Length& left, const Length& right) noexcept {
110 return left.Value() < right.Value();
111}
112
113inline constexpr bool operator>(const Length& left, const Length& right) noexcept {
114 return left.Value() > right.Value();
115}
116
117inline constexpr bool operator<=(const Length& left, const Length& right) noexcept {
118 return left.Value() <= right.Value();
119}
120
121inline constexpr bool operator>=(const Length& left, const Length& right) noexcept {
122 return left.Value() >= right.Value();
123}
124
125inline std::ostream& operator<<(std::ostream& stream, const Length& length) {
126 stream << length.Print();
127 return stream;
128}
129
130} // namespace PhQ::Dimension
131
132namespace std {
133
134template <>
135struct hash<PhQ::Dimension::Length> {
136 inline size_t operator()(const PhQ::Dimension::Length& length) const {
137 return hash<int8_t>()(length.Value());
138 }
139};
140
141} // namespace std
142
143#endif // PHQ_DIMENSION_LENGTH_HPP
Base physical dimension of length. Typically denoted "L". One of seven independent base physical dime...
Definition Length.hpp:40
~Length() noexcept=default
Destructor. Destroys this base physical dimension of length.
constexpr int8_t Value() const noexcept
Value of this base physical dimension.
Definition Length.hpp:68
static std::string_view Abbreviation() noexcept
Abbreviation of this base physical dimension.
Definition Length.hpp:73
constexpr Length(const int8_t value)
Constructor. Constructs a base physical dimension of length with a given value.
Definition Length.hpp:47
static std::string_view Label() noexcept
Label of this base physical dimension.
Definition Length.hpp:78
constexpr Length()=default
Default constructor. Constructs a base physical dimension of length with a value of zero.
int8_t value
Value of this base physical dimension.
Definition Length.hpp:98
std::string Print() const noexcept
Prints this base physical dimension as a string.
Definition Length.hpp:83
Namespace that contains base physical dimensions.
Definition Base.hpp:72
constexpr bool operator!=(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator>(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator<(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator<=(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator==(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator>=(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
std::ostream & operator<<(std::ostream &stream, const ElectricCurrent &current)
Namespace that encompasses all of the Physical Quantities library's content.