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
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_TEMPERATURE_HPP
26#define PHQ_TEMPERATURE_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
34#include "Unit/Temperature.hpp"
35
36namespace PhQ {
37
38/// \brief Temperature. For a temperature difference, see PhQ::TemperatureDifference. For the
39/// gradient of temperature, see PhQ::TemperatureGradient.
40template <typename NumericType = double>
41class Temperature : public DimensionalScalar<Unit::Temperature, NumericType> {
42public:
43 /// \brief Default constructor. Constructs a temperature with an uninitialized value.
44 Temperature() = default;
45
46 /// \brief Constructor. Constructs a temperature with a given value expressed in a given
47 /// temperature unit.
48 Temperature(const NumericType value, const Unit::Temperature unit)
49 : DimensionalScalar<Unit::Temperature, NumericType>(value, unit) {}
50
51 /// \brief Destructor. Destroys this temperature.
52 ~Temperature() noexcept = default;
53
54 /// \brief Copy constructor. Constructs a temperature by copying another one.
55 constexpr Temperature(const Temperature<NumericType>& other) = default;
56
57 /// \brief Copy constructor. Constructs a temperature by copying another one.
58 template <typename OtherNumericType>
59 explicit constexpr Temperature(const Temperature<OtherNumericType>& other)
60 : Temperature(static_cast<NumericType>(other.Value())) {}
61
62 /// \brief Move constructor. Constructs a temperature by moving another one.
63 constexpr Temperature(Temperature<NumericType>&& other) noexcept = default;
64
65 /// \brief Copy assignment operator. Assigns this temperature by copying another one.
67
68 /// \brief Copy assignment operator. Assigns this temperature by copying another one.
69 template <typename OtherNumericType>
71 this->value = static_cast<NumericType>(other.Value());
72 return *this;
73 }
74
75 /// \brief Move assignment operator. Assigns this temperature by moving another one.
77 Temperature<NumericType>&& other) noexcept = default;
78
79 /// \brief Statically creates a temperature of absolute zero.
80 [[nodiscard]] static constexpr Temperature<NumericType> Zero() {
81 return Temperature<NumericType>{static_cast<NumericType>(0)};
82 }
83
84 /// \brief Statically creates a temperature with a given value expressed in a given temperature
85 /// unit.
86 template <Unit::Temperature Unit>
87 [[nodiscard]] static constexpr Temperature<NumericType> Create(const NumericType value) {
89 ConvertStatically<Unit::Temperature, Unit, Standard<Unit::Temperature>>(value)};
90 }
91
92 constexpr Temperature<NumericType> operator+(const Temperature<NumericType>& temperature) const {
93 return Temperature<NumericType>{this->value + temperature.value};
94 }
95
97 const TemperatureDifference<NumericType>& temperature_difference) const {
98 return Temperature<NumericType>{this->value + temperature_difference.Value()};
99 }
100
102 const Temperature<NumericType>& temperature) const {
103 return TemperatureDifference<NumericType>{this->value - temperature.value};
104 }
105
107 const TemperatureDifference<NumericType>& temperature_difference) const {
108 return Temperature<NumericType>{this->value - temperature_difference.Value()};
109 }
110
111 constexpr Temperature<NumericType> operator*(const NumericType number) const {
112 return Temperature<NumericType>{this->value * number};
113 }
114
115 constexpr Temperature<NumericType> operator/(const NumericType number) const {
116 return Temperature<NumericType>{this->value / number};
117 }
118
119 constexpr NumericType operator/(const Temperature<NumericType>& temperature) const noexcept {
120 return this->value / temperature.value;
121 }
122
123 constexpr void operator+=(const Temperature<NumericType>& temperature) noexcept {
124 this->value += temperature.value;
125 }
126
127 constexpr void operator+=(
128 const TemperatureDifference<NumericType>& temperature_difference) noexcept {
129 this->value += temperature_difference.Value();
130 }
131
132 constexpr void operator-=(const Temperature<NumericType>& temperature) noexcept {
133 this->value -= temperature.value;
134 }
135
136 constexpr void operator-=(
137 const TemperatureDifference<NumericType>& temperature_difference) noexcept {
138 this->value -= temperature_difference.Value();
139 }
140
141 constexpr void operator*=(const NumericType number) noexcept {
142 this->value *= number;
143 }
144
145 constexpr void operator/=(const NumericType number) noexcept {
146 this->value /= number;
147 }
148
149private:
150 /// \brief Constructor. Constructs a temperature with a given value expressed in the standard
151 /// temperature unit.
152 explicit constexpr Temperature(const NumericType value)
153 : DimensionalScalar<Unit::Temperature, NumericType>(value) {}
154
155 template <typename OtherNumericType>
157};
158
159template <typename NumericType>
160inline constexpr bool operator==(
161 const Temperature<NumericType>& left, const Temperature<NumericType>& right) noexcept {
162 return left.Value() == right.Value();
163}
164
165template <typename NumericType>
166inline constexpr bool operator!=(
167 const Temperature<NumericType>& left, const Temperature<NumericType>& right) noexcept {
168 return left.Value() != right.Value();
169}
170
171template <typename NumericType>
172inline constexpr bool operator<(
173 const Temperature<NumericType>& left, const Temperature<NumericType>& right) noexcept {
174 return left.Value() < right.Value();
175}
176
177template <typename NumericType>
178inline constexpr bool operator>(
179 const Temperature<NumericType>& left, const Temperature<NumericType>& right) noexcept {
180 return left.Value() > right.Value();
181}
182
183template <typename NumericType>
184inline constexpr bool operator<=(
185 const Temperature<NumericType>& left, const Temperature<NumericType>& right) noexcept {
186 return left.Value() <= right.Value();
187}
188
189template <typename NumericType>
190inline constexpr bool operator>=(
191 const Temperature<NumericType>& left, const Temperature<NumericType>& right) noexcept {
192 return left.Value() >= right.Value();
193}
194
195template <typename NumericType>
196inline std::ostream& operator<<(std::ostream& stream, const Temperature<NumericType>& temperature) {
197 stream << temperature.Print();
198 return stream;
199}
200
201template <typename NumericType>
203 const NumericType number, const Temperature<NumericType>& temperature) {
204 return temperature * number;
205}
206
207template <typename NumericType>
209 const Temperature<NumericType>& temperature) const {
210 return Temperature<NumericType>{this->value + temperature.Value()};
211}
212
213template <typename NumericType>
215 const Temperature<NumericType>& temperature) const {
216 return Temperature<NumericType>{this->value - temperature.Value()};
217}
218
219} // namespace PhQ
220
221namespace std {
222
223template <typename NumericType>
224struct hash<PhQ::Temperature<NumericType>> {
225 inline size_t operator()(const PhQ::Temperature<NumericType>& temperature) const {
226 return hash<NumericType>()(temperature.Value());
227 }
228};
229
230} // namespace std
231
232#endif // PHQ_TEMPERATURE_HPP
Abstract base class that represents any dimensional scalar physical quantity. Such a physical quantit...
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
constexpr NumericType Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr UnitType Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
Temperature difference. Not to be confused with temperature; see PhQ::Temperature....
constexpr Temperature< NumericType > operator+(const Temperature< NumericType > &temperature) const
constexpr Temperature< NumericType > operator-(const Temperature< NumericType > &temperature) const
Temperature. For a temperature difference, see PhQ::TemperatureDifference. For the gradient of temper...
constexpr void operator+=(const Temperature< NumericType > &temperature) noexcept
constexpr void operator-=(const TemperatureDifference< NumericType > &temperature_difference) noexcept
constexpr Temperature< NumericType > operator/(const NumericType number) const
constexpr void operator*=(const NumericType number) noexcept
Temperature()=default
Default constructor. Constructs a temperature with an uninitialized value.
static constexpr Temperature< NumericType > Zero()
Statically creates a temperature of absolute zero.
constexpr Temperature< NumericType > operator*(const NumericType number) const
constexpr Temperature< NumericType > & operator=(const Temperature< NumericType > &other)=default
Copy assignment operator. Assigns this temperature by copying another one.
static constexpr Temperature< NumericType > Create(const NumericType value)
Statically creates a temperature with a given value expressed in a given temperature unit.
constexpr Temperature(const NumericType value)
Constructor. Constructs a temperature with a given value expressed in the standard temperature unit.
constexpr Temperature< NumericType > operator+(const TemperatureDifference< NumericType > &temperature_difference) const
constexpr void operator+=(const TemperatureDifference< NumericType > &temperature_difference) noexcept
constexpr NumericType operator/(const Temperature< NumericType > &temperature) const noexcept
constexpr void operator/=(const NumericType number) noexcept
~Temperature() noexcept=default
Destructor. Destroys this temperature.
constexpr void operator-=(const Temperature< NumericType > &temperature) noexcept
constexpr Temperature< NumericType > & operator=(Temperature< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this temperature by moving another one.
constexpr Temperature< NumericType > operator+(const Temperature< NumericType > &temperature) const
constexpr TemperatureDifference< NumericType > operator-(const Temperature< NumericType > &temperature) const
constexpr Temperature< NumericType > & operator=(const Temperature< OtherNumericType > &other)
Copy assignment operator. Assigns this temperature by copying another one.
constexpr Temperature(Temperature< NumericType > &&other) noexcept=default
Move constructor. Constructs a temperature by moving another one.
Temperature(const NumericType value, const Unit::Temperature unit)
Constructor. Constructs a temperature with a given value expressed in a given temperature unit.
constexpr Temperature< NumericType > operator-(const TemperatureDifference< NumericType > &temperature_difference) const
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)
constexpr bool operator<(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator<=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr Acceleration< NumericType > operator*(const NumericType number, const Acceleration< NumericType > &acceleration)
constexpr bool operator==(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept