Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
SubstanceAmount.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_SUBSTANCE_AMOUNT_HPP
26 #define PHQ_SUBSTANCE_AMOUNT_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Unit/SubstanceAmount.hpp"
34 
35 namespace PhQ {
36 
37 /// \brief Amount of substance. Typically measured in moles (mol).
38 template <typename NumericType = double>
39 class SubstanceAmount : public DimensionalScalar<Unit::SubstanceAmount, NumericType> {
40 public:
41  /// \brief Default constructor. Constructs a substance amount with an uninitialized value.
42  SubstanceAmount() = default;
43 
44  /// \brief Constructor. Constructs a substance amount with a given value expressed in a given
45  /// substance amount unit.
46  SubstanceAmount(const NumericType value, const Unit::SubstanceAmount unit)
47  : DimensionalScalar<Unit::SubstanceAmount, NumericType>(value, unit) {}
48 
49  /// \brief Destructor. Destroys this substance amount.
50  ~SubstanceAmount() noexcept = default;
51 
52  /// \brief Copy constructor. Constructs a substance amount by copying another one.
53  constexpr SubstanceAmount(const SubstanceAmount<NumericType>& other) = default;
54 
55  /// \brief Copy constructor. Constructs a substance amount by copying another one.
56  template <typename OtherNumericType>
57  explicit constexpr SubstanceAmount(const SubstanceAmount<OtherNumericType>& other)
58  : SubstanceAmount(static_cast<NumericType>(other.Value())) {}
59 
60  /// \brief Move constructor. Constructs a substance amount by moving another one.
61  constexpr SubstanceAmount(SubstanceAmount<NumericType>&& other) noexcept = default;
62 
63  /// \brief Copy assignment operator. Assigns this substance amount by copying another one.
65  const SubstanceAmount<NumericType>& other) = default;
66 
67  /// \brief Copy assignment operator. Assigns this substance amount by copying another one.
68  template <typename OtherNumericType>
70  const SubstanceAmount<OtherNumericType>& other) {
71  this->value = static_cast<NumericType>(other.Value());
72  return *this;
73  }
74 
75  /// \brief Move assignment operator. Assigns this substance amount by moving another one.
77  SubstanceAmount<NumericType>&& other) noexcept = default;
78 
79  /// \brief Statically creates a substance amount of zero.
80  [[nodiscard]] static constexpr SubstanceAmount<NumericType> Zero() {
81  return SubstanceAmount<NumericType>{static_cast<NumericType>(0)};
82  }
83 
84  /// \brief Statically creates a substance amount with a given value expressed in a given substance
85  /// amount unit.
86  template <Unit::SubstanceAmount Unit>
87  [[nodiscard]] static constexpr SubstanceAmount<NumericType> Create(const NumericType value) {
89  ConvertStatically<Unit::SubstanceAmount, Unit, Standard<Unit::SubstanceAmount>>(value)};
90  }
91 
93  const SubstanceAmount<NumericType>& substance_amount) const {
94  return SubstanceAmount<NumericType>{this->value + substance_amount.value};
95  }
96 
98  const SubstanceAmount<NumericType>& substance_amount) const {
99  return SubstanceAmount<NumericType>{this->value - substance_amount.value};
100  }
101 
102  constexpr SubstanceAmount<NumericType> operator*(const NumericType number) const {
103  return SubstanceAmount<NumericType>{this->value * number};
104  }
105 
106  constexpr SubstanceAmount<NumericType> operator/(const NumericType number) const {
107  return SubstanceAmount<NumericType>{this->value / number};
108  }
109 
110  constexpr NumericType operator/(
111  const SubstanceAmount<NumericType>& substance_amount) const noexcept {
112  return this->value / substance_amount.value;
113  }
114 
115  constexpr void operator+=(const SubstanceAmount<NumericType>& substance_amount) noexcept {
116  this->value += substance_amount.value;
117  }
118 
119  constexpr void operator-=(const SubstanceAmount<NumericType>& substance_amount) noexcept {
120  this->value -= substance_amount.value;
121  }
122 
123  constexpr void operator*=(const NumericType number) noexcept {
124  this->value *= number;
125  }
126 
127  constexpr void operator/=(const NumericType number) noexcept {
128  this->value /= number;
129  }
130 
131 private:
132  /// \brief Constructor. Constructs a substance amount with a given value expressed in the standard
133  /// substance amount unit.
134  explicit constexpr SubstanceAmount(const NumericType value)
135  : DimensionalScalar<Unit::SubstanceAmount, NumericType>(value) {}
136 };
137 
138 template <typename NumericType>
139 inline constexpr bool operator==(
140  const SubstanceAmount<NumericType>& left, const SubstanceAmount<NumericType>& right) noexcept {
141  return left.Value() == right.Value();
142 }
143 
144 template <typename NumericType>
145 inline constexpr bool operator!=(
146  const SubstanceAmount<NumericType>& left, const SubstanceAmount<NumericType>& right) noexcept {
147  return left.Value() != right.Value();
148 }
149 
150 template <typename NumericType>
151 inline constexpr bool operator<(
152  const SubstanceAmount<NumericType>& left, const SubstanceAmount<NumericType>& right) noexcept {
153  return left.Value() < right.Value();
154 }
155 
156 template <typename NumericType>
157 inline constexpr bool operator>(
158  const SubstanceAmount<NumericType>& left, const SubstanceAmount<NumericType>& right) noexcept {
159  return left.Value() > right.Value();
160 }
161 
162 template <typename NumericType>
163 inline constexpr bool operator<=(
164  const SubstanceAmount<NumericType>& left, const SubstanceAmount<NumericType>& right) noexcept {
165  return left.Value() <= right.Value();
166 }
167 
168 template <typename NumericType>
169 inline constexpr bool operator>=(
170  const SubstanceAmount<NumericType>& left, const SubstanceAmount<NumericType>& right) noexcept {
171  return left.Value() >= right.Value();
172 }
173 
174 template <typename NumericType>
175 inline std::ostream& operator<<(
176  std::ostream& stream, const SubstanceAmount<NumericType>& substance_amount) {
177  stream << substance_amount.Print();
178  return stream;
179 }
180 
181 template <typename NumericType>
183  const NumericType number, const SubstanceAmount<NumericType>& substance_amount) {
184  return substance_amount * number;
185 }
186 
187 } // namespace PhQ
188 
189 namespace std {
190 
191 template <typename NumericType>
192 struct hash<PhQ::SubstanceAmount<NumericType>> {
193  inline size_t operator()(const PhQ::SubstanceAmount<NumericType>& substance_amount) const {
194  return hash<NumericType>()(substance_amount.Value());
195  }
196 };
197 
198 } // namespace std
199 
200 #endif // PHQ_SUBSTANCE_AMOUNT_HPP
Abstract base class that represents any dimensional scalar physical quantity. Such a physical quantit...
double value
Value of this physical quantity expressed in its standard unit of measure.
constexpr double Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr Unit::SubstanceAmount 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...
Amount of substance. Typically measured in moles (mol).
constexpr NumericType operator/(const SubstanceAmount< NumericType > &substance_amount) const noexcept
constexpr void operator*=(const NumericType number) noexcept
static constexpr SubstanceAmount< NumericType > Zero()
Statically creates a substance amount of zero.
constexpr SubstanceAmount< NumericType > & operator=(const SubstanceAmount< OtherNumericType > &other)
Copy assignment operator. Assigns this substance amount by copying another one.
SubstanceAmount()=default
Default constructor. Constructs a substance amount with an uninitialized value.
~SubstanceAmount() noexcept=default
Destructor. Destroys this substance amount.
constexpr SubstanceAmount(SubstanceAmount< NumericType > &&other) noexcept=default
Move constructor. Constructs a substance amount by moving another one.
constexpr SubstanceAmount< NumericType > operator/(const NumericType number) const
constexpr SubstanceAmount< NumericType > & operator=(SubstanceAmount< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this substance amount by moving another one.
static constexpr SubstanceAmount< NumericType > Create(const NumericType value)
Statically creates a substance amount with a given value expressed in a given substance amount unit.
SubstanceAmount(const NumericType value, const Unit::SubstanceAmount unit)
Constructor. Constructs a substance amount with a given value expressed in a given substance amount u...
constexpr void operator+=(const SubstanceAmount< NumericType > &substance_amount) noexcept
constexpr SubstanceAmount< NumericType > operator+(const SubstanceAmount< NumericType > &substance_amount) const
constexpr SubstanceAmount(const NumericType value)
Constructor. Constructs a substance amount with a given value expressed in the standard substance amo...
constexpr SubstanceAmount< NumericType > operator*(const NumericType number) const
constexpr void operator/=(const NumericType number) noexcept
constexpr void operator-=(const SubstanceAmount< NumericType > &substance_amount) noexcept
constexpr SubstanceAmount< NumericType > & operator=(const SubstanceAmount< NumericType > &other)=default
Copy assignment operator. Assigns this substance amount by copying another one.
constexpr SubstanceAmount< NumericType > operator-(const SubstanceAmount< NumericType > &substance_amount) const
SubstanceAmount
Amount of substance units.
Namespace that encompasses all of the Physical Quantities library's content.
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
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)