Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
DimensionalScalar.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_DIMENSIONAL_SCALAR_HPP
26 #define PHQ_DIMENSIONAL_SCALAR_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 #include <string>
32 #include <type_traits>
33 
34 #include "Base.hpp"
35 #include "Dimensions.hpp"
36 #include "Unit.hpp"
37 
38 namespace PhQ {
39 
40 /// \brief Abstract base class that represents any dimensional scalar physical quantity. Such a
41 /// physical quantity is composed of a value and a unit of measure where the value is a scalar
42 /// number.
43 /// \tparam UnitType Unit of measure enumeration type.
44 /// \tparam NumericType Floating-point numeric type: float, double, or long double. Defaults to
45 /// double if unspecified.
46 template <typename UnitType, typename NumericType = double>
48  static_assert(std::is_floating_point<NumericType>::value,
49  "The NumericType template parameter of a physical quantity must be a numeric "
50  "floating-point type: float, double, or long double.");
51 
52 public:
53  /// \brief Physical dimension set of this physical quantity.
54  [[nodiscard]] static constexpr const PhQ::Dimensions& Dimensions() {
55  return PhQ::RelatedDimensions<UnitType>;
56  }
57 
58  /// \brief Standard unit of measure for this physical quantity. This physical quantity's value is
59  /// stored internally in this unit of measure.
60  [[nodiscard]] static constexpr UnitType Unit() {
61  return PhQ::Standard<UnitType>;
62  }
63 
64  /// \brief Value of this physical quantity expressed in its standard unit of measure.
65  [[nodiscard]] constexpr NumericType Value() const noexcept {
66  return value;
67  }
68 
69  /// \brief Value of this physical quantity expressed in a given unit of measure.
70  [[nodiscard]] NumericType Value(const UnitType unit) const {
71  return PhQ::Convert(value, PhQ::Standard<UnitType>, unit);
72  }
73 
74  /// \brief Value of this physical quantity expressed in a given unit of measure. This method can
75  /// be evaluated statically at compile-time.
76  template <UnitType NewUnit>
77  [[nodiscard]] constexpr NumericType StaticValue() const {
78  return PhQ::ConvertStatically<UnitType, PhQ::Standard<UnitType>, NewUnit>(value);
79  }
80 
81  /// \brief Returns the value of this physical quantity expressed in its standard unit of measure
82  /// as a mutable value.
83  [[nodiscard]] constexpr NumericType& MutableValue() noexcept {
84  return value;
85  }
86 
87  /// \brief Sets the value of this physical quantity expressed in its standard unit of measure to
88  /// the given value.
89  constexpr void SetValue(const NumericType value) noexcept {
90  this->value = value;
91  }
92 
93  /// \brief Prints this physical quantity as a string. This physical quantity's value is expressed
94  /// in its standard unit of measure.
95  [[nodiscard]] std::string Print() const {
96  return PhQ::Print(value).append(" ").append(PhQ::Abbreviation(PhQ::Standard<UnitType>));
97  }
98 
99  /// \brief Prints this physical quantity as a string. This physical quantity's value is expressed
100  /// in the given unit of measure.
101  [[nodiscard]] std::string Print(const UnitType unit) const {
102  return PhQ::Print(Value(unit)).append(" ").append(PhQ::Abbreviation(unit));
103  }
104 
105  /// \brief Serializes this physical quantity as a JSON message. This physical quantity's value is
106  /// expressed in its standard unit of measure.
107  [[nodiscard]] std::string JSON() const {
108  return std::string{"{\"value\":"}
109  .append(PhQ::Print(value))
110  .append(R"(,"unit":")")
111  .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
112  .append("\"}");
113  }
114 
115  /// \brief Serializes this physical quantity as a JSON message. This physical quantity's value is
116  /// expressed in the given unit of measure.
117  [[nodiscard]] std::string JSON(const UnitType unit) const {
118  return std::string{"{\"value\":"}
119  .append(PhQ::Print(Value(unit)))
120  .append(R"(,"unit":")")
121  .append(PhQ::Abbreviation(unit))
122  .append("\"}");
123  }
124 
125  /// \brief Serializes this physical quantity as an XML message. This physical quantity's value is
126  /// expressed in its standard unit of measure.
127  [[nodiscard]] std::string XML() const {
128  return std::string{"<value>"}
129  .append(PhQ::Print(value))
130  .append("</value><unit>")
131  .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
132  .append("</unit>");
133  }
134 
135  /// \brief Serializes this physical quantity as an XML message. This physical quantity's value is
136  /// expressed in the given unit of measure.
137  [[nodiscard]] std::string XML(const UnitType unit) const {
138  return std::string{"<value>"}
139  .append(PhQ::Print(Value(unit)))
140  .append("</value><unit>")
141  .append(PhQ::Abbreviation(unit))
142  .append("</unit>");
143  }
144 
145  /// \brief Serializes this physical quantity as a YAML message. This physical quantity's value is
146  /// expressed in its standard unit of measure.
147  [[nodiscard]] std::string YAML() const {
148  return std::string{"{value:"}
149  .append(PhQ::Print(value))
150  .append(",unit:\"")
151  .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
152  .append("\"}");
153  }
154 
155  /// \brief Serializes this physical quantity as a YAML message. This physical quantity's value is
156  /// expressed in the given unit of measure.
157  [[nodiscard]] std::string YAML(const UnitType unit) const {
158  return std::string{"{value:"}
159  .append(PhQ::Print(Value(unit)))
160  .append(",unit:\"")
161  .append(PhQ::Abbreviation(unit))
162  .append("\"}");
163  }
164 
165 protected:
166  /// \brief Default constructor. Constructs a dimensional scalar physical quantity with an
167  /// uninitialized value.
168  DimensionalScalar() = default;
169 
170  /// \brief Constructor. Constructs a dimensional scalar physical quantity with a given value
171  /// expressed in its standard unit of measure.
172  explicit constexpr DimensionalScalar(const NumericType value) : value(value) {}
173 
174  /// \brief Constructor. Constructs a dimensional scalar physical quantity with a given value
175  /// expressed in a given unit of measure.
176  DimensionalScalar(const NumericType value, const UnitType unit) : value(value) {
177  PhQ::ConvertInPlace(this->value, unit, PhQ::Standard<UnitType>);
178  }
179 
180  /// \brief Destructor. Destroys this dimensional scalar physical quantity.
181  ~DimensionalScalar() noexcept = default;
182 
183  /// \brief Copy constructor. Constructs a dimensional scalar physical quantity by copying another
184  /// one.
185  constexpr DimensionalScalar(const DimensionalScalar<UnitType, NumericType>& other) = default;
186 
187  /// \brief Copy constructor. Constructs a dimensional scalar physical quantity by copying another
188  /// one.
189  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
190  /// automatically.
191  template <typename OtherNumericType>
192  explicit constexpr DimensionalScalar(const DimensionalScalar<UnitType, OtherNumericType>& other)
193  : value(static_cast<NumericType>(other.Value())) {}
194 
195  /// \brief Move constructor. Constructs a dimensional scalar physical quantity by moving another
196  /// one.
197  constexpr DimensionalScalar(DimensionalScalar<UnitType, NumericType>&& other) noexcept = default;
198 
199  /// \brief Copy assignment operator. Assigns this dimensional scalar physical quantity by copying
200  /// another one.
202  const DimensionalScalar<UnitType, NumericType>& other) = default;
203 
204  /// \brief Copy assignment operator. Assigns this dimensional scalar physical quantity by copying
205  /// another one.
206  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
207  /// automatically.
208  template <typename OtherNumericType>
211  value = static_cast<NumericType>(other.Value());
212  return *this;
213  }
214 
215  /// \brief Move assignment operator. Assigns this dimensional scalar physical quantity by moving
216  /// another one.
218  DimensionalScalar<UnitType, NumericType>&& other) noexcept = default;
219 
220  /// \brief Value of this physical quantity expressed in its standard unit of measure.
221  NumericType value;
222 };
223 
224 } // namespace PhQ
225 
226 #endif // PHQ_DIMENSIONAL_SCALAR_HPP
Abstract base class that represents any dimensional scalar physical quantity. Such a physical quantit...
std::string XML(const UnitType unit) const
Serializes this physical quantity as an XML message. This physical quantity's value is expressed in t...
std::string YAML(const UnitType unit) const
Serializes this physical quantity as a YAML message. This physical quantity's value is expressed in t...
constexpr void SetValue(const NumericType value) noexcept
Sets the value of this physical quantity expressed in its standard unit of measure to the given value...
~DimensionalScalar() noexcept=default
Destructor. Destroys this dimensional scalar physical quantity.
constexpr DimensionalScalar(DimensionalScalar< UnitType, NumericType > &&other) noexcept=default
Move constructor. Constructs a dimensional scalar physical quantity by moving another one.
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
std::string Print(const UnitType unit) const
Prints this physical quantity as a string. This physical quantity's value is expressed in the given u...
constexpr DimensionalScalar< UnitType, NumericType > & operator=(DimensionalScalar< UnitType, NumericType > &&other) noexcept=default
Move assignment operator. Assigns this dimensional scalar physical quantity by moving another one.
constexpr DimensionalScalar< UnitType, NumericType > & operator=(const DimensionalScalar< UnitType, NumericType > &other)=default
Copy assignment operator. Assigns this dimensional scalar physical quantity by copying another one.
constexpr DimensionalScalar(const NumericType value)
Constructor. Constructs a dimensional scalar physical quantity with a given value expressed in its st...
NumericType Value(const UnitType unit) const
Value of this physical quantity expressed in a given unit of measure.
std::string JSON() const
Serializes this physical quantity as a JSON message. This physical quantity's value is expressed in i...
constexpr DimensionalScalar< UnitType, NumericType > & operator=(const DimensionalScalar< UnitType, OtherNumericType > &other)
Copy assignment operator. Assigns this dimensional scalar physical quantity by copying another one.
constexpr NumericType StaticValue() const
Value of this physical quantity expressed in a given unit of measure. This method can be evaluated st...
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 YAML() const
Serializes this physical quantity as a YAML message. This physical quantity's value is expressed in i...
DimensionalScalar(const NumericType value, const UnitType unit)
Constructor. Constructs a dimensional scalar physical quantity with a given value expressed in a give...
std::string JSON(const UnitType unit) const
Serializes this physical quantity as a JSON message. This physical quantity's value is expressed in t...
std::string XML() const
Serializes this physical quantity as an XML message. This physical quantity's value is expressed in i...
DimensionalScalar()=default
Default constructor. Constructs a dimensional scalar physical quantity with an uninitialized value.
constexpr NumericType & MutableValue() noexcept
Returns the value of this physical quantity expressed in its standard unit of measure as a mutable va...
static constexpr const PhQ::Dimensions & Dimensions()
Physical dimension set of this physical quantity.
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
Physical dimension set of a unit of measure or physical quantity. Composed of the seven independent b...
Definition: Dimensions.hpp:49
Namespace that encompasses all of the Physical Quantities library's content.
NumericType Convert(const NumericType value, const Unit original_unit, const Unit new_unit)
Converts a value expressed in a given unit of measure to a new unit of measure. Returns the converted...
Definition: Unit.hpp:210
void ConvertInPlace(NumericType &value, const Unit original_unit, const Unit new_unit)
Converts a value expressed in a given unit of measure to a new unit of measure. The conversion is per...
Definition: Unit.hpp:128
std::string_view Abbreviation(const Enumeration enumeration)
Returns the abbreviation of a given enumeration value. For example, PhQ::Abbreviation(PhQ::Unit::Time...
Definition: Base.hpp:89
std::string Print(const NumericType value)
Prints a given floating-point number as a string. Prints enough digits to represent the number exactl...
Definition: Base.hpp:170