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
Memory.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_MEMORY_HPP
26#define PHQ_MEMORY_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "Unit/Memory.hpp"
34
35namespace PhQ {
36
37// Forward declaration for class PhQ::Memory.
38template <typename NumericType>
39class Time;
40
41// Forward declaration for class PhQ::Memory.
42template <typename NumericType>
43class Frequency;
44
45// Forward declaration for class PhQ::Memory.
46template <typename NumericType>
47class MemoryRate;
48
49/// \brief Computer memory. For the time rate of change of computer memory, see PhQ::MemoryRate; see
50/// also PhQ::Time and PhQ::Frequency.
51template <typename NumericType = double>
52class Memory : public DimensionalScalar<Unit::Memory, NumericType> {
53public:
54 /// \brief Default constructor. Constructs a memory quantity with an uninitialized value.
55 Memory() = default;
56
57 /// \brief Constructor. Constructs a memory quantity with a given value expressed in a given
58 /// memory unit.
59 Memory(const NumericType value, const Unit::Memory unit)
60 : DimensionalScalar<Unit::Memory, NumericType>(value, unit) {}
61
62 /// \brief Constructor. Constructs a memory quantity from a given memory rate and time duration
63 /// using the definition of memory rate.
64 constexpr Memory(const MemoryRate<NumericType>& memory_rate, const Time<NumericType>& time);
65
66 /// \brief Constructor. Constructs a memory quantity from a given memory rate and frequency using
67 /// the definition of memory rate.
68 constexpr Memory(
69 const MemoryRate<NumericType>& memory_rate, const Frequency<NumericType>& frequency);
70
71 /// \brief Destructor. Destroys this memory quantity.
72 ~Memory() noexcept = default;
73
74 /// \brief Copy constructor. Constructs a memory quantity by copying another one.
75 constexpr Memory(const Memory<NumericType>& other) = default;
76
77 /// \brief Copy constructor. Constructs a memory quantity by copying another one.
78 template <typename OtherNumericType>
79 explicit constexpr Memory(const Memory<OtherNumericType>& other)
80 : Memory(static_cast<NumericType>(other.Value())) {}
81
82 /// \brief Move constructor. Constructs a memory quantity by moving another one.
83 constexpr Memory(Memory<NumericType>&& other) noexcept = default;
84
85 /// \brief Copy assignment operator. Assigns this memory quantity by copying another one.
86 constexpr Memory<NumericType>& operator=(const Memory<NumericType>& other) = default;
87
88 /// \brief Copy assignment operator. Assigns this memory quantity by copying another one.
89 template <typename OtherNumericType>
91 this->value = static_cast<NumericType>(other.Value());
92 return *this;
93 }
94
95 /// \brief Move assignment operator. Assigns this memory quantity by moving another one.
96 constexpr Memory<NumericType>& operator=(Memory<NumericType>&& other) noexcept = default;
97
98 /// \brief Statically creates a memory quantity of zero.
99 [[nodiscard]] static constexpr Memory<NumericType> Zero() {
100 return Memory<NumericType>{static_cast<NumericType>(0)};
101 }
102
103 /// \brief Statically creates a memory quantity with a given value expressed in a given memory
104 /// unit.
105 template <Unit::Memory Unit>
106 [[nodiscard]] static constexpr Memory<NumericType> Create(const NumericType value) {
107 return Memory<NumericType>{
108 ConvertStatically<Unit::Memory, Unit, Standard<Unit::Memory>>(value)};
109 }
110
111 constexpr Memory<NumericType> operator+(const Memory<NumericType>& memory) const {
112 return Memory<NumericType>{this->value + memory.value};
113 }
114
115 constexpr Memory<NumericType> operator-(const Memory<NumericType>& memory) const {
116 return Memory<NumericType>{this->value - memory.value};
117 }
118
119 constexpr Memory<NumericType> operator*(const NumericType number) const {
120 return Memory<NumericType>{this->value * number};
121 }
122
123 constexpr MemoryRate<NumericType> operator*(const Frequency<NumericType>& frequency) const;
124
125 constexpr Memory<NumericType> operator/(const NumericType number) const {
126 return Memory<NumericType>{this->value / number};
127 }
128
129 constexpr MemoryRate<NumericType> operator/(const Time<NumericType>& time) const;
130
131 constexpr Time<NumericType> operator/(const MemoryRate<NumericType>& memory_rate) const;
132
133 constexpr NumericType operator/(const Memory<NumericType>& memory) const noexcept {
134 return this->value / memory.value;
135 }
136
137 constexpr void operator+=(const Memory<NumericType>& memory) noexcept {
138 this->value += memory.value;
139 }
140
141 constexpr void operator-=(const Memory<NumericType>& memory) noexcept {
142 this->value -= memory.value;
143 }
144
145 constexpr void operator*=(const NumericType number) noexcept {
146 this->value *= number;
147 }
148
149 constexpr void operator/=(const NumericType number) noexcept {
150 this->value /= number;
151 }
152
153private:
154 /// \brief Constructor. Constructs a memory quantity with a given value expressed in the standard
155 /// memory unit.
156 explicit constexpr Memory(const NumericType value)
157 : DimensionalScalar<Unit::Memory, NumericType>(value) {}
158};
159
160template <typename NumericType>
161inline constexpr bool operator==(
162 const Memory<NumericType>& left, const Memory<NumericType>& right) noexcept {
163 return left.Value() == right.Value();
164}
165
166template <typename NumericType>
167inline constexpr bool operator!=(
168 const Memory<NumericType>& left, const Memory<NumericType>& right) noexcept {
169 return left.Value() != right.Value();
170}
171
172template <typename NumericType>
173inline constexpr bool operator<(
174 const Memory<NumericType>& left, const Memory<NumericType>& right) noexcept {
175 return left.Value() < right.Value();
176}
177
178template <typename NumericType>
179inline constexpr bool operator>(
180 const Memory<NumericType>& left, const Memory<NumericType>& right) noexcept {
181 return left.Value() > right.Value();
182}
183
184template <typename NumericType>
185inline constexpr bool operator<=(
186 const Memory<NumericType>& left, const Memory<NumericType>& right) noexcept {
187 return left.Value() <= right.Value();
188}
189
190template <typename NumericType>
191inline constexpr bool operator>=(
192 const Memory<NumericType>& left, const Memory<NumericType>& right) noexcept {
193 return left.Value() >= right.Value();
194}
195
196template <typename NumericType>
197inline std::ostream& operator<<(std::ostream& stream, const Memory<NumericType>& memory) {
198 stream << memory.Print();
199 return stream;
200}
201
202template <typename NumericType>
204 const NumericType number, const Memory<NumericType>& memory) {
205 return memory * number;
206}
207
208} // namespace PhQ
209
210namespace std {
211
212template <typename NumericType>
213struct hash<PhQ::Memory<NumericType>> {
214 inline size_t operator()(const PhQ::Memory<NumericType>& memory) const {
215 return hash<NumericType>()(memory.Value());
216 }
217};
218
219} // namespace std
220
221#endif // PHQ_MEMORY_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...
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition Frequency.hpp:40
Computer memory rate. Can represent the time rate of change of memory or a memory transfer speed; see...
Computer memory. For the time rate of change of computer memory, see PhQ::MemoryRate; see also PhQ::T...
Definition Memory.hpp:52
constexpr Memory< NumericType > & operator=(Memory< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this memory quantity by moving another one.
constexpr void operator*=(const NumericType number) noexcept
Definition Memory.hpp:145
constexpr Memory(Memory< NumericType > &&other) noexcept=default
Move constructor. Constructs a memory quantity by moving another one.
constexpr void operator+=(const Memory< NumericType > &memory) noexcept
Definition Memory.hpp:137
constexpr Memory< NumericType > operator*(const NumericType number) const
Definition Memory.hpp:119
constexpr void operator-=(const Memory< NumericType > &memory) noexcept
Definition Memory.hpp:141
constexpr NumericType operator/(const Memory< NumericType > &memory) const noexcept
Definition Memory.hpp:133
constexpr Memory< NumericType > operator/(const NumericType number) const
Definition Memory.hpp:125
~Memory() noexcept=default
Destructor. Destroys this memory quantity.
static constexpr Memory< NumericType > Zero()
Statically creates a memory quantity of zero.
Definition Memory.hpp:99
constexpr Memory< NumericType > operator-(const Memory< NumericType > &memory) const
Definition Memory.hpp:115
constexpr void operator/=(const NumericType number) noexcept
Definition Memory.hpp:149
constexpr Memory< NumericType > & operator=(const Memory< NumericType > &other)=default
Copy assignment operator. Assigns this memory quantity by copying another one.
static constexpr Memory< NumericType > Create(const NumericType value)
Statically creates a memory quantity with a given value expressed in a given memory unit.
Definition Memory.hpp:106
constexpr Memory(const NumericType value)
Constructor. Constructs a memory quantity with a given value expressed in the standard memory unit.
Definition Memory.hpp:156
constexpr Memory< NumericType > & operator=(const Memory< OtherNumericType > &other)
Copy assignment operator. Assigns this memory quantity by copying another one.
Definition Memory.hpp:90
Memory(const NumericType value, const Unit::Memory unit)
Constructor. Constructs a memory quantity with a given value expressed in a given memory unit.
Definition Memory.hpp:59
constexpr Memory< NumericType > operator+(const Memory< NumericType > &memory) const
Definition Memory.hpp:111
Memory()=default
Default constructor. Constructs a memory quantity with an uninitialized value.
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
Frequency
Frequency units.
Definition Frequency.hpp:53
Memory
Computer memory units.
Definition Memory.hpp:53
MemoryRate
Computer memory rate unit. Can represent the time rate of change of memory or a memory transfer speed...
Time
Time units.
Definition Time.hpp:53
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 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