Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
TransportEnergyConsumption.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_TRANSPORT_ENERGY_CONSUMPTION_HPP
26 #define PHQ_TRANSPORT_ENERGY_CONSUMPTION_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Length.hpp"
34 #include "Power.hpp"
35 #include "Speed.hpp"
37 
38 namespace PhQ {
39 
40 /// \brief Transport energy consumption, also known as energy consumption in transport. A measure of
41 /// energy use per distance traveled. Energy consumption in transport is often measured in joules
42 /// per metre (J/m), kilowatt-hours per kilometre (kW·hr/km), or kilowatt-hours per mile (kW·hr/mi).
43 template <typename NumericType = double>
45  : public DimensionalScalar<Unit::TransportEnergyConsumption, NumericType> {
46 public:
47  /// \brief Default constructor. Constructs a transport energy consumption with an uninitialized
48  /// value.
50 
51  /// \brief Constructor. Constructs a transport energy consumption with a given value expressed in
52  /// a given transport energy consumption unit.
54  : DimensionalScalar<Unit::TransportEnergyConsumption, NumericType>(value, unit) {}
55 
56  /// \brief Constructor. Constructs a transport energy consumption from a given energy and length
57  /// using the definition of transport energy consumption.
59  const Energy<NumericType>& energy, const Length<NumericType>& length)
60  : TransportEnergyConsumption<NumericType>(energy.Value() / length.Value()) {}
61 
62  /// \brief Destructor. Destroys this transport energy consumption.
63  ~TransportEnergyConsumption() noexcept = default;
64 
65  /// \brief Copy constructor. Constructs a transport energy consumption by copying another one.
67  const TransportEnergyConsumption<NumericType>& other) = default;
68 
69  /// \brief Copy constructor. Constructs a transport energy consumption by copying another one.
70  template <typename OtherNumericType>
71  explicit constexpr TransportEnergyConsumption(
72  const TransportEnergyConsumption<OtherNumericType>& other)
73  : TransportEnergyConsumption(static_cast<NumericType>(other.Value())) {}
74 
75  /// \brief Move constructor. Constructs a transport energy consumption by moving another one.
77  TransportEnergyConsumption<NumericType>&& other) noexcept = default;
78 
79  /// \brief Copy assignment operator. Assigns this transport energy consumption by copying another
80  /// one.
82  const TransportEnergyConsumption<NumericType>& other) = default;
83 
84  /// \brief Copy assignment operator. Assigns this transport energy consumption by copying another
85  /// one.
86  template <typename OtherNumericType>
89  this->value = static_cast<NumericType>(other.Value());
90  return *this;
91  }
92 
93  /// \brief Move assignment operator. Assigns this transport energy consumption by moving another
94  /// one.
96  TransportEnergyConsumption<NumericType>&& other) noexcept = default;
97 
98  /// \brief Statically creates a transport energy consumption of zero.
99  [[nodiscard]] static constexpr TransportEnergyConsumption<NumericType> Zero() {
100  return TransportEnergyConsumption<NumericType>{static_cast<NumericType>(0)};
101  }
102 
103  /// \brief Statically creates a transport energy consumption with a given value expressed in a
104  /// given transport energy consumption unit.
105  template <Unit::TransportEnergyConsumption Unit>
106  [[nodiscard]] static constexpr TransportEnergyConsumption<NumericType> Create(
107  const NumericType value) {
110  Standard<Unit::TransportEnergyConsumption>>(value)};
111  }
112 
114  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
116  this->value + transport_energy_consumption.value};
117  }
118 
120  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
122  this->value - transport_energy_consumption.value};
123  }
124 
125  constexpr TransportEnergyConsumption<NumericType> operator*(const NumericType number) const {
126  return TransportEnergyConsumption<NumericType>{this->value * number};
127  }
128 
129  constexpr Energy<NumericType> operator*(const Length<NumericType> length) const {
130  return Energy<NumericType>{*this, length};
131  }
132 
133  constexpr Power<NumericType> operator*(const Speed<NumericType> speed) const {
134  return Power<NumericType>{*this, speed};
135  }
136 
137  constexpr TransportEnergyConsumption<NumericType> operator/(const NumericType number) const {
138  return TransportEnergyConsumption<NumericType>{this->value / number};
139  }
140 
141  constexpr NumericType operator/(
142  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const noexcept {
143  return this->value / transport_energy_consumption.value;
144  }
145 
146  constexpr void operator+=(
147  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) noexcept {
148  this->value += transport_energy_consumption.value;
149  }
150 
151  constexpr void operator-=(
152  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) noexcept {
153  this->value -= transport_energy_consumption.value;
154  }
155 
156  constexpr void operator*=(const NumericType number) noexcept {
157  this->value *= number;
158  }
159 
160  constexpr void operator/=(const NumericType number) noexcept {
161  this->value /= number;
162  }
163 
164 private:
165  /// \brief Constructor. Constructs a transport energy consumption with a given value expressed in
166  /// the standard transport energy consumption unit.
167  explicit constexpr TransportEnergyConsumption(const NumericType value)
169 };
170 
171 template <typename NumericType>
172 inline constexpr bool operator==(const TransportEnergyConsumption<NumericType>& left,
173  const TransportEnergyConsumption<NumericType>& right) noexcept {
174  return left.Value() == right.Value();
175 }
176 
177 template <typename NumericType>
178 inline constexpr bool operator!=(const TransportEnergyConsumption<NumericType>& left,
179  const TransportEnergyConsumption<NumericType>& right) noexcept {
180  return left.Value() != right.Value();
181 }
182 
183 template <typename NumericType>
184 inline constexpr bool operator<(const TransportEnergyConsumption<NumericType>& left,
185  const TransportEnergyConsumption<NumericType>& right) noexcept {
186  return left.Value() < right.Value();
187 }
188 
189 template <typename NumericType>
190 inline constexpr bool operator>(const TransportEnergyConsumption<NumericType>& left,
191  const TransportEnergyConsumption<NumericType>& right) noexcept {
192  return left.Value() > right.Value();
193 }
194 
195 template <typename NumericType>
196 inline constexpr bool operator<=(const TransportEnergyConsumption<NumericType>& left,
197  const TransportEnergyConsumption<NumericType>& right) noexcept {
198  return left.Value() <= right.Value();
199 }
200 
201 template <typename NumericType>
202 inline constexpr bool operator>=(const TransportEnergyConsumption<NumericType>& left,
203  const TransportEnergyConsumption<NumericType>& right) noexcept {
204  return left.Value() >= right.Value();
205 }
206 
207 template <typename NumericType>
208 inline std::ostream& operator<<(
209  std::ostream& stream,
210  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) {
211  stream << transport_energy_consumption.Print();
212  return stream;
213 }
214 
215 template <typename NumericType>
217  const NumericType number,
218  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) {
219  return transport_energy_consumption * number;
220 }
221 
222 template <typename NumericType>
223 inline constexpr Length<NumericType>::Length(
224  const Energy<NumericType>& energy,
225  const TransportEnergyConsumption<NumericType>& transport_energy_consumption)
226  : Length<NumericType>(energy.Value() / transport_energy_consumption.Value()) {}
227 
228 template <typename NumericType>
229 inline constexpr Energy<NumericType>::Energy(
230  const TransportEnergyConsumption<NumericType>& transport_energy_consumption,
231  const Length<NumericType>& length)
232  : Energy<NumericType>(transport_energy_consumption.Value() * length.Value()) {}
233 
234 template <typename NumericType>
235 inline constexpr Power<NumericType>::Power(
236  const TransportEnergyConsumption<NumericType>& transport_energy_consumption,
237  const Speed<NumericType>& speed)
238  : Power<NumericType>(speed.Value() * transport_energy_consumption.Value()) {}
239 
240 template <typename NumericType>
241 inline constexpr Energy<NumericType> Length<NumericType>::operator*(
242  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
243  return Energy<NumericType>{transport_energy_consumption, *this};
244 }
245 
246 template <typename NumericType>
247 inline constexpr Power<NumericType> Speed<NumericType>::operator*(
248  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
249  return Power<NumericType>{transport_energy_consumption, *this};
250 }
251 
252 template <typename NumericType>
253 inline constexpr TransportEnergyConsumption<NumericType> Energy<NumericType>::operator/(
254  const Length<NumericType>& length) const {
255  return TransportEnergyConsumption<NumericType>{*this, length};
256 }
257 
258 template <typename NumericType>
259 inline constexpr Length<NumericType> Energy<NumericType>::operator/(
260  const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
261  return Length<NumericType>{*this, transport_energy_consumption};
262 }
263 
264 } // namespace PhQ
265 
266 namespace std {
267 
268 template <typename NumericType>
269 struct hash<PhQ::TransportEnergyConsumption<NumericType>> {
270  inline size_t operator()(
271  const PhQ::TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
272  return hash<NumericType>()(transport_energy_consumption.Value());
273  }
274 };
275 
276 } // namespace std
277 
278 #endif // PHQ_TRANSPORT_ENERGY_CONSUMPTION_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::TransportEnergyConsumption 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...
Energy physical quantity. Can represent any kind of energy, such as kinetic energy,...
Definition: Energy.hpp:73
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition: Length.hpp:111
Length()=default
Default constructor. Constructs a length with an uninitialized value.
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition: Power.hpp:51
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
Transport energy consumption, also known as energy consumption in transport. A measure of energy use ...
~TransportEnergyConsumption() noexcept=default
Destructor. Destroys this transport energy consumption.
constexpr TransportEnergyConsumption< NumericType > operator/(const NumericType number) const
constexpr TransportEnergyConsumption(const NumericType value)
Constructor. Constructs a transport energy consumption with a given value expressed in the standard t...
static constexpr TransportEnergyConsumption< NumericType > Zero()
Statically creates a transport energy consumption of zero.
constexpr TransportEnergyConsumption(TransportEnergyConsumption< NumericType > &&other) noexcept=default
Move constructor. Constructs a transport energy consumption by moving another one.
constexpr TransportEnergyConsumption< NumericType > & operator=(const TransportEnergyConsumption< NumericType > &other)=default
Copy assignment operator. Assigns this transport energy consumption by copying another one.
constexpr NumericType operator/(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) const noexcept
TransportEnergyConsumption()=default
Default constructor. Constructs a transport energy consumption with an uninitialized value.
constexpr TransportEnergyConsumption< NumericType > operator-(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) const
constexpr TransportEnergyConsumption(const Energy< NumericType > &energy, const Length< NumericType > &length)
Constructor. Constructs a transport energy consumption from a given energy and length using the defin...
constexpr void operator-=(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) noexcept
constexpr TransportEnergyConsumption< NumericType > operator*(const NumericType number) const
static constexpr TransportEnergyConsumption< NumericType > Create(const NumericType value)
Statically creates a transport energy consumption with a given value expressed in a given transport e...
constexpr TransportEnergyConsumption< NumericType > & operator=(TransportEnergyConsumption< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this transport energy consumption by moving another one.
constexpr void operator+=(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) noexcept
constexpr Energy< NumericType > operator*(const Length< NumericType > length) const
constexpr void operator/=(const NumericType number) noexcept
TransportEnergyConsumption(const NumericType value, const Unit::TransportEnergyConsumption unit)
Constructor. Constructs a transport energy consumption with a given value expressed in a given transp...
constexpr TransportEnergyConsumption< NumericType > operator+(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) const
constexpr TransportEnergyConsumption< NumericType > & operator=(const TransportEnergyConsumption< OtherNumericType > &other)
Copy assignment operator. Assigns this transport energy consumption by copying another one.
constexpr void operator*=(const NumericType number) noexcept
constexpr Power< NumericType > operator*(const Speed< NumericType > speed) const
Power
Power units.
Definition: Power.hpp:53
Length
Length units.
Definition: Length.hpp:53
Energy
Energy units.
Definition: Energy.hpp:53
TransportEnergyConsumption
Energy consumption in transport 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 NumericType ConvertStatically(const NumericType value)
Converts a value expressed in a given unit of measure to a new unit of measure. Returns the converted...
Definition: Unit.hpp:278
constexpr Acceleration< NumericType > operator*(const NumericType number, const Acceleration< NumericType > &acceleration)
constexpr bool operator>(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr TransportEnergyConsumption< NumericType > operator*(const NumericType number, const TransportEnergyConsumption< NumericType > &transport_energy_consumption)
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 bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)