Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Mass.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_DIMENSION_MASS_HPP
26 #define PHQ_DIMENSION_MASS_HPP
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <iostream>
32 #include <string>
33 #include <string_view>
34 
35 namespace PhQ::Dimension {
36 
37 /// \brief Base physical dimension of mass. Typically denoted "M". One of seven independent base
38 /// physical dimensions that form the physical dimension set of any unit of measure or physical
39 /// quantity. Part of PhQ::Dimensions.
40 class Mass {
41 public:
42  /// \brief Default constructor. Constructs a base physical dimension of mass with a value of zero.
43  constexpr Mass() = default;
44 
45  /// \brief Constructor. Constructs a base physical dimension of mass with a given value.
46  explicit constexpr Mass(const int8_t value) : value(value) {}
47 
48  /// \brief Destructor. Destroys this base physical dimension of mass.
49  ~Mass() noexcept = default;
50 
51  /// \brief Copy constructor. Constructs a base physical dimension of mass by copying another one.
52  constexpr Mass(const Mass& other) = default;
53 
54  /// \brief Copy assignment operator. Assigns the value of this base physical dimension of mass by
55  /// copying from another one.
56  constexpr Mass& operator=(const Mass& other) = default;
57 
58  /// \brief Move constructor. Constructs a base physical dimension of mass by moving another one.
59  constexpr Mass(Mass&& other) noexcept = default;
60 
61  /// \brief Move assignment operator. Assigns the value of this base physical dimension of mass by
62  /// moving another one.
63  constexpr Mass& operator=(Mass&& other) noexcept = default;
64 
65  /// \brief Value of this base physical dimension.
66  [[nodiscard]] constexpr int8_t Value() const noexcept {
67  return value;
68  }
69 
70  /// \brief Abbreviation of this base physical dimension.
71  static std::string_view Abbreviation() noexcept {
72  return "M";
73  }
74 
75  /// \brief Label of this base physical dimension.
76  static std::string_view Label() noexcept {
77  return "Mass";
78  }
79 
80  /// \brief Prints this base physical dimension as a string.
81  [[nodiscard]] std::string Print() const noexcept {
82  if (value == 0) {
83  return {};
84  }
85  std::string string{Abbreviation()};
86  if (value > 1) {
87  string.append("^" + std::to_string(value));
88  } else if (value < 0) {
89  string.append("^(" + std::to_string(value) + ")");
90  }
91  return string;
92  }
93 
94 private:
95  /// \brief Value of this base physical dimension.
96  int8_t value{0};
97 };
98 
99 inline constexpr bool operator==(const Mass& left, const Mass& right) noexcept {
100  return left.Value() == right.Value();
101 }
102 
103 inline constexpr bool operator!=(const Mass& left, const Mass& right) noexcept {
104  return left.Value() != right.Value();
105 }
106 
107 inline constexpr bool operator<(const Mass& left, const Mass& right) noexcept {
108  return left.Value() < right.Value();
109 }
110 
111 inline constexpr bool operator>(const Mass& left, const Mass& right) noexcept {
112  return left.Value() > right.Value();
113 }
114 
115 inline constexpr bool operator<=(const Mass& left, const Mass& right) noexcept {
116  return left.Value() <= right.Value();
117 }
118 
119 inline constexpr bool operator>=(const Mass& left, const Mass& right) noexcept {
120  return left.Value() >= right.Value();
121 }
122 
123 inline std::ostream& operator<<(std::ostream& stream, const Mass& mass) {
124  stream << mass.Print();
125  return stream;
126 }
127 
128 } // namespace PhQ::Dimension
129 
130 namespace std {
131 
132 template <>
133 struct hash<PhQ::Dimension::Mass> {
134  inline size_t operator()(const PhQ::Dimension::Mass& mass) const {
135  return hash<int8_t>()(mass.Value());
136  }
137 };
138 
139 } // namespace std
140 
141 #endif // PHQ_DIMENSION_MASS_HPP
Base physical dimension of mass. Typically denoted "M". One of seven independent base physical dimens...
Definition: Mass.hpp:40
~Mass() noexcept=default
Destructor. Destroys this base physical dimension of mass.
int8_t value
Value of this base physical dimension.
Definition: Mass.hpp:96
constexpr Mass()=default
Default constructor. Constructs a base physical dimension of mass with a value of zero.
constexpr Mass(const int8_t value)
Constructor. Constructs a base physical dimension of mass with a given value.
Definition: Mass.hpp:46
static std::string_view Label() noexcept
Label of this base physical dimension.
Definition: Mass.hpp:76
static std::string_view Abbreviation() noexcept
Abbreviation of this base physical dimension.
Definition: Mass.hpp:71
std::string Print() const noexcept
Prints this base physical dimension as a string.
Definition: Mass.hpp:81
constexpr int8_t Value() const noexcept
Value of this base physical dimension.
Definition: Mass.hpp:66
Namespace that contains base physical dimensions.
Definition: Base.hpp:72
constexpr bool operator!=(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator>(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator<(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator<=(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator==(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
constexpr bool operator>=(const ElectricCurrent &left, const ElectricCurrent &right) noexcept
std::ostream & operator<<(std::ostream &stream, const ElectricCurrent &current)
Mass
Mass units.
Definition: Mass.hpp:53
Namespace that encompasses all of the Physical Quantities library's content.