Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
ElectricCharge.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_CHARGE_HPP
26 #define PHQ_ELECTRIC_CHARGE_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Unit/ElectricCharge.hpp"
34 
35 namespace PhQ {
36 
37 // Forward declaration for class PhQ::ElectricCharge.
38 template <typename NumericType>
39 class ElectricCurrent;
40 
41 // Forward declaration for class PhQ::ElectricCharge.
42 template <typename NumericType>
43 class Frequency;
44 
45 // Forward declaration for class PhQ::ElectricCharge.
46 template <typename NumericType>
47 class Time;
48 
49 /// \brief Electric charge.
50 template <typename NumericType = double>
51 class ElectricCharge : public DimensionalScalar<Unit::ElectricCharge, NumericType> {
52 public:
53  /// \brief Default constructor. Constructs an electric charge with an uninitialized value.
54  ElectricCharge() = default;
55 
56  /// \brief Constructor. Constructs an electric charge with a given value expressed in a given
57  /// electric charge unit.
58  ElectricCharge(const NumericType value, const Unit::ElectricCharge unit)
59  : DimensionalScalar<Unit::ElectricCharge, NumericType>(value, unit) {}
60 
61  /// \brief Constructor. Constructs an electric charge from a given electric current and time using
62  /// the definition of electric current.
63  constexpr ElectricCharge(
64  const ElectricCurrent<NumericType>& electric_current, const Time<NumericType>& time);
65 
66  /// \brief Constructor. Constructs an electric charge from a given electric current and frequency
67  /// using the definition of electric current.
68  constexpr ElectricCharge(const ElectricCurrent<NumericType>& electric_current,
69  const Frequency<NumericType>& frequency);
70 
71  /// \brief Destructor. Destroys this electric charge.
72  ~ElectricCharge() noexcept = default;
73 
74  /// \brief Copy constructor. Constructs an electric charge by copying another one.
75  constexpr ElectricCharge(const ElectricCharge<NumericType>& other) = default;
76 
77  /// \brief Copy constructor. Constructs an electric charge by copying another one.
78  template <typename OtherNumericType>
79  explicit constexpr ElectricCharge(const ElectricCharge<OtherNumericType>& other)
80  : ElectricCharge(static_cast<NumericType>(other.Value())) {}
81 
82  /// \brief Move constructor. Constructs an electric charge by moving another one.
83  constexpr ElectricCharge(ElectricCharge<NumericType>&& other) noexcept = default;
84 
85  /// \brief Copy assignment operator. Assigns this electric charge by copying another one.
87  const ElectricCharge<NumericType>& other) = default;
88 
89  /// \brief Copy assignment operator. Assigns this electric charge by copying another one.
90  template <typename OtherNumericType>
92  this->value = static_cast<NumericType>(other.Value());
93  return *this;
94  }
95 
96  /// \brief Move assignment operator. Assigns this electric charge by moving another one.
98  ElectricCharge<NumericType>&& other) noexcept = default;
99 
100  /// \brief Statically creates an electric charge of zero.
101  [[nodiscard]] static constexpr ElectricCharge<NumericType> Zero() {
102  return ElectricCharge<NumericType>{static_cast<NumericType>(0)};
103  }
104 
105  /// \brief Statically creates an electric charge with a given value expressed in a given electric
106  /// charge unit.
107  template <Unit::ElectricCharge Unit>
108  [[nodiscard]] static constexpr ElectricCharge<NumericType> Create(const NumericType value) {
110  ConvertStatically<Unit::ElectricCharge, Unit, Standard<Unit::ElectricCharge>>(value)};
111  }
112 
114  const ElectricCharge<NumericType>& electric_charge) const {
115  return ElectricCharge<NumericType>{this->value + electric_charge.value};
116  }
117 
119  const ElectricCharge<NumericType>& electric_charge) const {
120  return ElectricCharge<NumericType>{this->value - electric_charge.value};
121  }
122 
123  constexpr ElectricCharge<NumericType> operator*(const NumericType number) const {
124  return ElectricCharge<NumericType>{this->value * number};
125  }
126 
128 
129  constexpr ElectricCharge<NumericType> operator/(const NumericType number) const {
130  return ElectricCharge<NumericType>{this->value / number};
131  }
132 
133  constexpr NumericType operator/(
134  const ElectricCharge<NumericType>& electric_charge) const noexcept {
135  return this->value / electric_charge.value;
136  }
137 
139 
140  constexpr Time<NumericType> operator/(const ElectricCurrent<NumericType>& electric_current) const;
141 
142  constexpr void operator+=(const ElectricCharge<NumericType>& electric_charge) noexcept {
143  this->value += electric_charge.value;
144  }
145 
146  constexpr void operator-=(const ElectricCharge<NumericType>& electric_charge) noexcept {
147  this->value -= electric_charge.value;
148  }
149 
150  constexpr void operator*=(const NumericType number) noexcept {
151  this->value *= number;
152  }
153 
154  constexpr void operator/=(const NumericType number) noexcept {
155  this->value /= number;
156  }
157 
158 private:
159  /// \brief Constructor. Constructs an electric charge with a given value expressed in the standard
160  /// electric charge unit.
161  explicit constexpr ElectricCharge(const NumericType value)
162  : DimensionalScalar<Unit::ElectricCharge, NumericType>(value) {}
163 };
164 
165 template <typename NumericType>
166 inline constexpr bool operator==(
167  const ElectricCharge<NumericType>& left, const ElectricCharge<NumericType>& right) noexcept {
168  return left.Value() == right.Value();
169 }
170 
171 template <typename NumericType>
172 inline constexpr bool operator!=(
173  const ElectricCharge<NumericType>& left, const ElectricCharge<NumericType>& right) noexcept {
174  return left.Value() != right.Value();
175 }
176 
177 template <typename NumericType>
178 inline constexpr bool operator<(
179  const ElectricCharge<NumericType>& left, const ElectricCharge<NumericType>& right) noexcept {
180  return left.Value() < right.Value();
181 }
182 
183 template <typename NumericType>
184 inline constexpr bool operator>(
185  const ElectricCharge<NumericType>& left, const ElectricCharge<NumericType>& right) noexcept {
186  return left.Value() > right.Value();
187 }
188 
189 template <typename NumericType>
190 inline constexpr bool operator<=(
191  const ElectricCharge<NumericType>& left, const ElectricCharge<NumericType>& right) noexcept {
192  return left.Value() <= right.Value();
193 }
194 
195 template <typename NumericType>
196 inline constexpr bool operator>=(
197  const ElectricCharge<NumericType>& left, const ElectricCharge<NumericType>& right) noexcept {
198  return left.Value() >= right.Value();
199 }
200 
201 template <typename NumericType>
202 inline std::ostream& operator<<(
203  std::ostream& stream, const ElectricCharge<NumericType>& electric_charge) {
204  stream << electric_charge.Print();
205  return stream;
206 }
207 
208 template <typename NumericType>
210  const NumericType number, const ElectricCharge<NumericType>& electric_charge) {
211  return electric_charge * number;
212 }
213 
214 } // namespace PhQ
215 
216 namespace std {
217 
218 template <typename NumericType>
219 struct hash<PhQ::ElectricCharge<NumericType>> {
220  inline size_t operator()(const PhQ::ElectricCharge<NumericType>& electric_charge) const {
221  return hash<NumericType>()(electric_charge.Value());
222  }
223 };
224 
225 } // namespace std
226 
227 #endif // PHQ_ELECTRIC_CHARGE_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::ElectricCharge 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.
constexpr ElectricCharge< NumericType > & operator=(const ElectricCharge< OtherNumericType > &other)
Copy assignment operator. Assigns this electric charge by copying another one.
constexpr Time< NumericType > operator/(const ElectricCurrent< NumericType > &electric_current) const
constexpr void operator*=(const NumericType number) noexcept
ElectricCharge()=default
Default constructor. Constructs an electric charge with an uninitialized value.
static constexpr ElectricCharge< NumericType > Zero()
Statically creates an electric charge of zero.
constexpr void operator+=(const ElectricCharge< NumericType > &electric_charge) noexcept
constexpr ElectricCurrent< NumericType > operator*(const Frequency< NumericType > &frequency) const
constexpr ElectricCharge(const ElectricCurrent< NumericType > &electric_current, const Frequency< NumericType > &frequency)
Constructor. Constructs an electric charge from a given electric current and frequency using the defi...
constexpr ElectricCharge(ElectricCharge< NumericType > &&other) noexcept=default
Move constructor. Constructs an electric charge by moving another one.
static constexpr ElectricCharge< NumericType > Create(const NumericType value)
Statically creates an electric charge with a given value expressed in a given electric charge unit.
constexpr ElectricCharge< NumericType > & operator=(const ElectricCharge< NumericType > &other)=default
Copy assignment operator. Assigns this electric charge by copying another one.
constexpr NumericType operator/(const ElectricCharge< NumericType > &electric_charge) const noexcept
constexpr ElectricCharge< NumericType > operator/(const NumericType number) const
~ElectricCharge() noexcept=default
Destructor. Destroys this electric charge.
constexpr ElectricCharge(const NumericType value)
Constructor. Constructs an electric charge with a given value expressed in the standard electric char...
constexpr ElectricCharge< NumericType > operator+(const ElectricCharge< NumericType > &electric_charge) const
constexpr ElectricCurrent< NumericType > operator/(const Time< NumericType > &time) const
constexpr ElectricCharge< NumericType > operator-(const ElectricCharge< NumericType > &electric_charge) const
constexpr ElectricCharge(const ElectricCurrent< NumericType > &electric_current, const Time< NumericType > &time)
Constructor. Constructs an electric charge from a given electric current and time using the definitio...
constexpr void operator/=(const NumericType number) noexcept
ElectricCharge(const NumericType value, const Unit::ElectricCharge unit)
Constructor. Constructs an electric charge with a given value expressed in a given electric charge un...
constexpr ElectricCharge< NumericType > & operator=(ElectricCharge< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this electric charge by moving another one.
constexpr void operator-=(const ElectricCharge< NumericType > &electric_charge) noexcept
constexpr ElectricCharge< NumericType > operator*(const NumericType number) const
Electric current, also known as amperage. Represents a flow of electric charge or a time rate of chan...
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
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 bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)