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