Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Temperature.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_TEMPERATURE_HPP
26 #define PHQ_DIMENSION_TEMPERATURE_HPP
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <iostream>
32 #include <string>
33 #include <string_view>
34 
35 namespace PhQ::Dimension {
36 
37 /// \brief Base physical dimension of temperature. Typically denoted "Θ". One of seven independent
38 /// base physical dimensions that form the physical dimension set of any unit of measure or physical
39 /// quantity. Part of PhQ::Dimensions.
40 class Temperature {
41 public:
42  /// \brief Default constructor. Constructs a base physical dimension of temperature with a value
43  /// of zero.
44  constexpr Temperature() = default;
45 
46  /// \brief Constructor. Constructs a base physical dimension of temperature with a given value.
47  explicit constexpr Temperature(const int8_t value) : value(value) {}
48 
49  /// \brief Destructor. Destroys this base physical dimension of temperature.
50  ~Temperature() noexcept = default;
51 
52  /// \brief Copy constructor. Constructs a base physical dimension of temperature by copying
53  /// another one.
54  constexpr Temperature(const Temperature& other) = default;
55 
56  /// \brief Copy assignment operator. Assigns the value of this base physical dimension of
57  /// temperature by copying from another one.
58  constexpr Temperature& operator=(const Temperature& other) = default;
59 
60  /// \brief Move constructor. Constructs a base physical dimension of temperature by moving another
61  /// one.
62  constexpr Temperature(Temperature&& other) noexcept = default;
63 
64  /// \brief Move assignment operator. Assigns the value of this base physical dimension of
65  /// temperature by moving another one.
66  constexpr Temperature& operator=(Temperature&& other) noexcept = default;
67 
68  /// \brief Value of this base physical dimension.
69  [[nodiscard]] constexpr int8_t Value() const noexcept {
70  return value;
71  }
72 
73  /// \brief Abbreviation of this base physical dimension.
74  static std::string_view Abbreviation() noexcept {
75  return "Θ";
76  }
77 
78  /// \brief Label of this base physical dimension.
79  static std::string_view Label() noexcept {
80  return "Temperature";
81  }
82 
83  /// \brief Prints this base physical dimension as a string.
84  [[nodiscard]] std::string Print() const noexcept {
85  if (value == 0) {
86  return {};
87  }
88  std::string string{Abbreviation()};
89  if (value > 1) {
90  string.append("^" + std::to_string(value));
91  } else if (value < 0) {
92  string.append("^(" + std::to_string(value) + ")");
93  }
94  return string;
95  }
96 
97 private:
98  /// \brief Value of this base physical dimension.
99  int8_t value{0};
100 };
101 
102 inline constexpr bool operator==(const Temperature& left, const Temperature& right) noexcept {
103  return left.Value() == right.Value();
104 }
105 
106 inline constexpr bool operator!=(const Temperature& left, const Temperature& right) noexcept {
107  return left.Value() != right.Value();
108 }
109 
110 inline constexpr bool operator<(const Temperature& left, const Temperature& right) noexcept {
111  return left.Value() < right.Value();
112 }
113 
114 inline constexpr bool operator>(const Temperature& left, const Temperature& right) noexcept {
115  return left.Value() > right.Value();
116 }
117 
118 inline constexpr bool operator<=(const Temperature& left, const Temperature& right) noexcept {
119  return left.Value() <= right.Value();
120 }
121 
122 inline constexpr bool operator>=(const Temperature& left, const Temperature& right) noexcept {
123  return left.Value() >= right.Value();
124 }
125 
126 inline std::ostream& operator<<(std::ostream& stream, const Temperature& temperature) {
127  stream << temperature.Print();
128  return stream;
129 }
130 
131 } // namespace PhQ::Dimension
132 
133 namespace std {
134 
135 template <>
136 struct hash<PhQ::Dimension::Temperature> {
137  inline size_t operator()(const PhQ::Dimension::Temperature& temperature) const {
138  return hash<int8_t>()(temperature.Value());
139  }
140 };
141 
142 } // namespace std
143 
144 #endif // PHQ_DIMENSION_TEMPERATURE_HPP
Base physical dimension of temperature. Typically denoted "Θ". One of seven independent base physical...
Definition: Temperature.hpp:40
constexpr int8_t Value() const noexcept
Value of this base physical dimension.
Definition: Temperature.hpp:69
static std::string_view Label() noexcept
Label of this base physical dimension.
Definition: Temperature.hpp:79
constexpr Temperature()=default
Default constructor. Constructs a base physical dimension of temperature with a value of zero.
constexpr Temperature(const int8_t value)
Constructor. Constructs a base physical dimension of temperature with a given value.
Definition: Temperature.hpp:47
std::string Print() const noexcept
Prints this base physical dimension as a string.
Definition: Temperature.hpp:84
static std::string_view Abbreviation() noexcept
Abbreviation of this base physical dimension.
Definition: Temperature.hpp:74
~Temperature() noexcept=default
Destructor. Destroys this base physical dimension of temperature.
int8_t value
Value of this base physical dimension.
Definition: Temperature.hpp:99
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)
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
Definition: Temperature.hpp:55
Namespace that encompasses all of the Physical Quantities library's content.