Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
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
38namespace 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).
43template <typename NumericType = double>
45 : public DimensionalScalar<Unit::TransportEnergyConsumption, NumericType> {
46public:
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.
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
164private:
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
171template <typename NumericType>
172inline constexpr bool operator==(const TransportEnergyConsumption<NumericType>& left,
173 const TransportEnergyConsumption<NumericType>& right) noexcept {
174 return left.Value() == right.Value();
175}
176
177template <typename NumericType>
178inline constexpr bool operator!=(const TransportEnergyConsumption<NumericType>& left,
179 const TransportEnergyConsumption<NumericType>& right) noexcept {
180 return left.Value() != right.Value();
181}
182
183template <typename NumericType>
184inline constexpr bool operator<(const TransportEnergyConsumption<NumericType>& left,
185 const TransportEnergyConsumption<NumericType>& right) noexcept {
186 return left.Value() < right.Value();
187}
188
189template <typename NumericType>
190inline constexpr bool operator>(const TransportEnergyConsumption<NumericType>& left,
191 const TransportEnergyConsumption<NumericType>& right) noexcept {
192 return left.Value() > right.Value();
193}
194
195template <typename NumericType>
196inline constexpr bool operator<=(const TransportEnergyConsumption<NumericType>& left,
197 const TransportEnergyConsumption<NumericType>& right) noexcept {
198 return left.Value() <= right.Value();
199}
200
201template <typename NumericType>
202inline constexpr bool operator>=(const TransportEnergyConsumption<NumericType>& left,
203 const TransportEnergyConsumption<NumericType>& right) noexcept {
204 return left.Value() >= right.Value();
205}
206
207template <typename NumericType>
208inline 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
215template <typename NumericType>
217 const NumericType number,
218 const TransportEnergyConsumption<NumericType>& transport_energy_consumption) {
219 return transport_energy_consumption * number;
220}
221
222template <typename NumericType>
224 const Energy<NumericType>& energy,
225 const TransportEnergyConsumption<NumericType>& transport_energy_consumption)
226 : Length<NumericType>(energy.Value() / transport_energy_consumption.Value()) {}
227
228template <typename NumericType>
230 const TransportEnergyConsumption<NumericType>& transport_energy_consumption,
231 const Length<NumericType>& length)
232 : Energy<NumericType>(transport_energy_consumption.Value() * length.Value()) {}
233
234template <typename NumericType>
236 const TransportEnergyConsumption<NumericType>& transport_energy_consumption,
237 const Speed<NumericType>& speed)
238 : Power<NumericType>(speed.Value() * transport_energy_consumption.Value()) {}
239
240template <typename NumericType>
242 const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
243 return Energy<NumericType>{transport_energy_consumption, *this};
244}
245
246template <typename NumericType>
248 const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
249 return Power<NumericType>{transport_energy_consumption, *this};
250}
251
252template <typename NumericType>
257
258template <typename NumericType>
260 const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const {
261 return Length<NumericType>{*this, transport_energy_consumption};
262}
263
264} // namespace PhQ
265
266namespace std {
267
268template <typename NumericType>
269struct 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...
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
constexpr NumericType Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr UnitType 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
constexpr Energy< NumericType > operator/(const NumericType number) const
Definition Energy.hpp:155
Energy()=default
Default constructor. Constructs an energy quantity with an uninitialized value.
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.
constexpr Length< NumericType > operator*(const NumericType number) const
Definition Length.hpp:198
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition Power.hpp:51
Power()=default
Default constructor. Constructs a power quantity with an uninitialized value.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition Speed.hpp:100
constexpr Speed< NumericType > operator*(const NumericType number) const
Definition Speed.hpp:205
Transport energy consumption, also known as energy consumption in transport. A measure of energy use ...
~TransportEnergyConsumption() noexcept=default
Destructor. Destroys this transport energy consumption.
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=(const TransportEnergyConsumption< NumericType > &other)=default
Copy assignment operator. Assigns this transport energy consumption by copying another one.
constexpr TransportEnergyConsumption(const NumericType value)
Constructor. Constructs a transport energy consumption with a given value expressed in the standard t...
constexpr TransportEnergyConsumption< NumericType > & operator=(TransportEnergyConsumption< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this transport energy consumption by moving another one.
constexpr TransportEnergyConsumption(TransportEnergyConsumption< NumericType > &&other) noexcept=default
Move constructor. Constructs a transport energy consumption by moving another one.
constexpr TransportEnergyConsumption< NumericType > & operator=(const TransportEnergyConsumption< OtherNumericType > &other)
Copy assignment operator. Assigns this transport energy consumption by copying another one.
constexpr TransportEnergyConsumption< NumericType > operator/(const NumericType number) const
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 NumericType number) const
constexpr Energy< NumericType > operator*(const Length< NumericType > length) 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 TransportEnergyConsumption< NumericType > &transport_energy_consumption) const
static constexpr TransportEnergyConsumption< NumericType > Zero()
Statically creates a transport energy consumption of zero.
constexpr TransportEnergyConsumption< NumericType > operator-(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) const
constexpr void operator+=(const TransportEnergyConsumption< NumericType > &transport_energy_consumption) noexcept
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 void operator*=(const NumericType number) noexcept
constexpr Power< NumericType > operator*(const Speed< NumericType > speed) const
TransportEnergyConsumption
Energy consumption in transport units.
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, 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 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 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