Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
ElectricCurrent.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_ELECTRIC_CURRENT_HPP
26 #define PHQ_ELECTRIC_CURRENT_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "ElectricCharge.hpp"
34 #include "Frequency.hpp"
35 #include "Time.hpp"
36 #include "Unit/ElectricCurrent.hpp"
37 
38 namespace PhQ {
39 
40 /// \brief Electric current, also known as amperage. Represents a flow of electric charge or a time
41 /// rate of change of electric charge.
42 template <typename NumericType = double>
43 class ElectricCurrent : public DimensionalScalar<Unit::ElectricCurrent, NumericType> {
44 public:
45  /// \brief Default constructor. Constructs an electric current with an uninitialized value.
46  ElectricCurrent() = default;
47 
48  /// \brief Constructor. Constructs an electric current with a given value expressed in a given
49  /// electric current unit.
50  ElectricCurrent(const NumericType value, const Unit::ElectricCurrent unit)
51  : DimensionalScalar<Unit::ElectricCurrent, NumericType>(value, unit) {}
52 
53  /// \brief Constructor. Constructs an electric current from a given electric charge and time using
54  /// the definition of electric current.
55  constexpr ElectricCurrent(
56  const ElectricCharge<NumericType>& electric_charge, const Time<NumericType>& time)
57  : ElectricCurrent<NumericType>(electric_charge.Value() / time.Value()) {}
58 
59  /// \brief Constructor. Constructs an electric current from a given electric charge and frequency
60  /// using the definition of electric current.
61  constexpr ElectricCurrent(
62  const ElectricCharge<NumericType>& electric_charge, const Frequency<NumericType>& frequency)
63  : ElectricCurrent<NumericType>(electric_charge.Value() * frequency.Value()) {}
64 
65  /// \brief Destructor. Destroys this electric current.
66  ~ElectricCurrent() noexcept = default;
67 
68  /// \brief Copy constructor. Constructs an electric current by copying another one.
69  constexpr ElectricCurrent(const ElectricCurrent<NumericType>& other) = default;
70 
71  /// \brief Copy constructor. Constructs an electric current by copying another one.
72  template <typename OtherNumericType>
73  explicit constexpr ElectricCurrent(const ElectricCurrent<OtherNumericType>& other)
74  : ElectricCurrent(static_cast<NumericType>(other.Value())) {}
75 
76  /// \brief Move constructor. Constructs an electric current by moving another one.
77  constexpr ElectricCurrent(ElectricCurrent<NumericType>&& other) noexcept = default;
78 
79  /// \brief Copy assignment operator. Assigns this electric current by copying another one.
81  const ElectricCurrent<NumericType>& other) = default;
82 
83  /// \brief Copy assignment operator. Assigns this electric current by copying another one.
84  template <typename OtherNumericType>
86  const ElectricCurrent<OtherNumericType>& other) {
87  this->value = static_cast<NumericType>(other.Value());
88  return *this;
89  }
90 
91  /// \brief Move assignment operator. Assigns this electric current by moving another one.
93  ElectricCurrent<NumericType>&& other) noexcept = default;
94 
95  /// \brief Statically creates an electric current of zero.
96  [[nodiscard]] static constexpr ElectricCurrent<NumericType> Zero() {
97  return ElectricCurrent<NumericType>{static_cast<NumericType>(0)};
98  }
99 
100  /// \brief Statically creates an electric current with a given value expressed in a given electric
101  /// current unit.
102  template <Unit::ElectricCurrent Unit>
103  [[nodiscard]] static constexpr ElectricCurrent<NumericType> Create(const NumericType value) {
105  ConvertStatically<Unit::ElectricCurrent, Unit, Standard<Unit::ElectricCurrent>>(value)};
106  }
107 
109  const ElectricCurrent<NumericType>& electric_current) const {
110  return ElectricCurrent<NumericType>{this->value + electric_current.value};
111  }
112 
114  const ElectricCurrent<NumericType>& electric_current) const {
115  return ElectricCurrent<NumericType>{this->value - electric_current.value};
116  }
117 
118  constexpr ElectricCurrent<NumericType> operator*(const NumericType number) const {
119  return ElectricCurrent<NumericType>{this->value * number};
120  }
121 
123  return ElectricCharge<NumericType>{*this, time};
124  }
125 
126  constexpr ElectricCurrent<NumericType> operator/(const NumericType number) const {
127  return ElectricCurrent<NumericType>{this->value / number};
128  }
129 
130  constexpr NumericType operator/(
131  const ElectricCurrent<NumericType>& electric_current) const noexcept {
132  return this->value / electric_current.value;
133  }
134 
136  return ElectricCharge<NumericType>{*this, frequency};
137  }
138 
140  const ElectricCharge<NumericType>& electric_charge) const {
141  return Frequency<NumericType>{*this, electric_charge};
142  }
143 
144  constexpr void operator+=(const ElectricCurrent<NumericType>& electric_current) noexcept {
145  this->value += electric_current.value;
146  }
147 
148  constexpr void operator-=(const ElectricCurrent<NumericType>& electric_current) noexcept {
149  this->value -= electric_current.value;
150  }
151 
152  constexpr void operator*=(const NumericType number) noexcept {
153  this->value *= number;
154  }
155 
156  constexpr void operator/=(const NumericType number) noexcept {
157  this->value /= number;
158  }
159 
160 private:
161  /// \brief Constructor. Constructs an electric current with a given value expressed in the
162  /// standard electric current unit.
163  explicit constexpr ElectricCurrent(const NumericType value)
164  : DimensionalScalar<Unit::ElectricCurrent, NumericType>(value) {}
165 };
166 
167 template <typename NumericType>
168 inline constexpr bool operator==(
169  const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
170  return left.Value() == right.Value();
171 }
172 
173 template <typename NumericType>
174 inline constexpr bool operator!=(
175  const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
176  return left.Value() != right.Value();
177 }
178 
179 template <typename NumericType>
180 inline constexpr bool operator<(
181  const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
182  return left.Value() < right.Value();
183 }
184 
185 template <typename NumericType>
186 inline constexpr bool operator>(
187  const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
188  return left.Value() > right.Value();
189 }
190 
191 template <typename NumericType>
192 inline constexpr bool operator<=(
193  const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
194  return left.Value() <= right.Value();
195 }
196 
197 template <typename NumericType>
198 inline constexpr bool operator>=(
199  const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
200  return left.Value() >= right.Value();
201 }
202 
203 template <typename NumericType>
204 inline std::ostream& operator<<(
205  std::ostream& stream, const ElectricCurrent<NumericType>& electric_current) {
206  stream << electric_current.Print();
207  return stream;
208 }
209 
210 template <typename NumericType>
212  const NumericType number, const ElectricCurrent<NumericType>& electric_current) {
213  return electric_current * number;
214 }
215 
216 template <typename NumericType>
217 inline constexpr Time<NumericType>::Time(const ElectricCharge<NumericType>& electric_charge,
218  const ElectricCurrent<NumericType>& electric_current)
219  : Time<NumericType>(electric_charge.Value() / electric_current.Value()) {}
220 
221 template <typename NumericType>
222 inline constexpr Frequency<NumericType>::Frequency(
223  const ElectricCurrent<NumericType>& electric_current,
224  const ElectricCharge<NumericType>& electric_charge)
225  : Frequency<NumericType>(electric_current.Value() / electric_charge.Value()) {}
226 
227 template <typename NumericType>
229  const ElectricCurrent<NumericType>& electric_current, const Time<NumericType>& time)
230  : ElectricCharge<NumericType>(electric_current.Value() * time.Value()) {}
231 
232 template <typename NumericType>
234  const ElectricCurrent<NumericType>& electric_current, const Frequency<NumericType>& frequency)
235  : ElectricCharge<NumericType>(electric_current.Value() / frequency.Value()) {}
236 
237 template <typename NumericType>
238 inline constexpr ElectricCharge<NumericType> Time<NumericType>::operator*(
239  const ElectricCurrent<NumericType>& electric_current) const {
240  return ElectricCharge<NumericType>{electric_current, *this};
241 }
242 
243 template <typename NumericType>
244 inline constexpr ElectricCurrent<NumericType> Frequency<NumericType>::operator*(
245  const ElectricCharge<NumericType>& electric_charge) const {
246  return ElectricCurrent<NumericType>{electric_charge, *this};
247 }
248 
249 template <typename NumericType>
250 inline constexpr ElectricCurrent<NumericType> ElectricCharge<NumericType>::operator*(
251  const Frequency<NumericType>& frequency) const {
252  return ElectricCurrent<NumericType>{*this, frequency};
253 }
254 
255 template <typename NumericType>
256 inline constexpr ElectricCurrent<NumericType> ElectricCharge<NumericType>::operator/(
257  const Time<NumericType>& time) const {
258  return ElectricCurrent<NumericType>{*this, time};
259 }
260 
261 template <typename NumericType>
262 inline constexpr Time<NumericType> ElectricCharge<NumericType>::operator/(
263  const ElectricCurrent<NumericType>& electric_current) const {
264  return Time<NumericType>{*this, electric_current};
265 }
266 
267 } // namespace PhQ
268 
269 namespace std {
270 
271 template <typename NumericType>
272 struct hash<PhQ::ElectricCurrent<NumericType>> {
273  inline size_t operator()(const PhQ::ElectricCurrent<NumericType>& electric_current) const {
274  return hash<NumericType>()(electric_current.Value());
275  }
276 };
277 
278 } // namespace std
279 
280 #endif // PHQ_ELECTRIC_CURRENT_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::ElectricCurrent 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...
Electric charge.
Electric current, also known as amperage. Represents a flow of electric charge or a time rate of chan...
constexpr ElectricCurrent< NumericType > & operator=(ElectricCurrent< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this electric current by moving another one.
~ElectricCurrent() noexcept=default
Destructor. Destroys this electric current.
constexpr void operator/=(const NumericType number) noexcept
constexpr Frequency< NumericType > operator/(const ElectricCharge< NumericType > &electric_charge) const
constexpr void operator*=(const NumericType number) noexcept
constexpr ElectricCurrent< NumericType > operator+(const ElectricCurrent< NumericType > &electric_current) const
constexpr ElectricCurrent< NumericType > & operator=(const ElectricCurrent< NumericType > &other)=default
Copy assignment operator. Assigns this electric current by copying another one.
constexpr void operator+=(const ElectricCurrent< NumericType > &electric_current) noexcept
static constexpr ElectricCurrent< NumericType > Create(const NumericType value)
Statically creates an electric current with a given value expressed in a given electric current unit.
constexpr ElectricCurrent(const ElectricCharge< NumericType > &electric_charge, const Time< NumericType > &time)
Constructor. Constructs an electric current from a given electric charge and time using the definitio...
constexpr ElectricCurrent(const ElectricCharge< NumericType > &electric_charge, const Frequency< NumericType > &frequency)
Constructor. Constructs an electric current from a given electric charge and frequency using the defi...
constexpr ElectricCharge< NumericType > operator/(const Frequency< NumericType > &frequency) const
constexpr ElectricCurrent< NumericType > operator/(const NumericType number) const
constexpr void operator-=(const ElectricCurrent< NumericType > &electric_current) noexcept
ElectricCurrent(const NumericType value, const Unit::ElectricCurrent unit)
Constructor. Constructs an electric current with a given value expressed in a given electric current ...
constexpr ElectricCurrent< NumericType > operator-(const ElectricCurrent< NumericType > &electric_current) const
constexpr ElectricCharge< NumericType > operator*(const Time< NumericType > &time) const
constexpr NumericType operator/(const ElectricCurrent< NumericType > &electric_current) const noexcept
static constexpr ElectricCurrent< NumericType > Zero()
Statically creates an electric current of zero.
ElectricCurrent()=default
Default constructor. Constructs an electric current with an uninitialized value.
constexpr ElectricCurrent< NumericType > operator*(const NumericType number) const
constexpr ElectricCurrent(const NumericType value)
Constructor. Constructs an electric current with a given value expressed in the standard electric cur...
constexpr ElectricCurrent< NumericType > & operator=(const ElectricCurrent< OtherNumericType > &other)
Copy assignment operator. Assigns this electric current by copying another one.
constexpr ElectricCurrent(ElectricCurrent< NumericType > &&other) noexcept=default
Move constructor. Constructs an electric current by moving another one.
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
ElectricCurrent
Electric current units.
Frequency
Frequency units.
Definition: Frequency.hpp:53
ElectricCharge
Electric charge units.
Time
Time units.
Definition: Time.hpp:53
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 Dyad< NumericType > operator/(const Dyad< NumericType > &dyad, const OtherNumericType number)
Definition: Dyad.hpp:696
constexpr ElectricCurrent< NumericType > operator*(const NumericType number, const ElectricCurrent< NumericType > &electric_current)
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)