Skip to content

Commit 4c6ab51

Browse files
committed
feat!: add number concepts
1 parent a356db7 commit 4c6ab51

File tree

16 files changed

+472
-107
lines changed

16 files changed

+472
-107
lines changed

example/include/ranged_representation.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ template<std::movable T, MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_t
3838
MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Max>
3939
using is_in_range_t = decltype(is_in_range<T, Min, Max>);
4040

41-
template<std::movable T, MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Min,
41+
template<mp_units::vector_space T, MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Min,
4242
MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Max>
4343
class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max>> {
4444
public:
@@ -50,15 +50,44 @@ class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max
5050
{
5151
return ranged_representation(-this->value());
5252
}
53+
54+
friend constexpr ranged_representation operator-(const ranged_representation& lhs, const ranged_representation& rhs)
55+
{
56+
return ranged_representation(lhs.value() - rhs.value());
57+
}
58+
59+
constexpr ranged_representation& operator+=(ranged_representation that)
60+
{
61+
this->value() += that.value();
62+
gsl_Expects(validate(this->value()));
63+
return *this;
64+
}
65+
constexpr ranged_representation& operator-=(ranged_representation that)
66+
{
67+
this->value() -= that.value();
68+
gsl_Expects(validate(this->value()));
69+
return *this;
70+
}
71+
constexpr ranged_representation& operator*=(T rhs)
72+
{
73+
this->value() *= rhs;
74+
gsl_Expects(validate(this->value()));
75+
return *this;
76+
}
5377
};
5478

5579
template<typename T, auto Min, auto Max>
5680
inline constexpr bool mp_units::is_scalar<ranged_representation<T, Min, Max>> = mp_units::is_scalar<T>;
5781

82+
template<typename T, auto Min, auto Max>
83+
struct mp_units::number_scalar<ranged_representation<T, Min, Max>> : std::type_identity<T> {};
84+
5885
template<typename T, auto Min, auto Max>
5986
inline constexpr bool mp_units::treat_as_floating_point<ranged_representation<T, Min, Max>> =
6087
mp_units::treat_as_floating_point<T>;
6188

89+
static_assert(mp_units::vector_space<ranged_representation<int, 17, 29>>);
90+
6291
template<typename T, auto Min, auto Max>
6392
struct MP_UNITS_STD_FMT::formatter<ranged_representation<T, Min, Max>> : formatter<T> {
6493
template<typename FormatContext>

example/measurement.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
namespace {
3232

33-
template<typename T>
33+
template<mp_units::vector_space T>
3434
class measurement {
3535
public:
3636
using value_type = T;
@@ -65,6 +65,18 @@ class measurement {
6565
return measurement(lhs.value() - rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty()));
6666
}
6767

68+
constexpr measurement& operator+=(measurement that)
69+
{
70+
value_ = *this + that;
71+
return *this;
72+
}
73+
74+
constexpr measurement& operator-=(measurement that)
75+
{
76+
value_ = *this - that;
77+
return *this;
78+
}
79+
6880
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const measurement& rhs)
6981
{
7082
const auto val = lhs.value() * rhs.value();
@@ -78,6 +90,12 @@ class measurement {
7890
return measurement(val, val * lhs.relative_uncertainty());
7991
}
8092

93+
constexpr measurement& operator*=(const value_type& value)
94+
{
95+
value_ = *this * value;
96+
return *this;
97+
}
98+
8199
[[nodiscard]] friend constexpr measurement operator*(const value_type& value, const measurement& rhs)
82100
{
83101
const auto val = rhs.value() * value;
@@ -125,7 +143,11 @@ inline constexpr bool mp_units::is_scalar<measurement<T>> = true;
125143
template<class T>
126144
inline constexpr bool mp_units::is_vector<measurement<T>> = true;
127145

146+
template<typename T>
147+
struct mp_units::number_scalar<measurement<T>> : std::type_identity<T> {};
148+
128149
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::scalar>);
150+
static_assert(mp_units::vector_space<measurement<double>>);
129151

130152
namespace {
131153

src/core/include/mp-units/bits/external/type_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <mp-units/bits/external/hacks.h> // IWYU pragma: keep
2626
#include <cstddef>
27+
#include <type_traits>
2728
#include <utility>
2829

2930
MP_UNITS_DIAGNOSTIC_PUSH

src/core/include/mp-units/bits/fwd.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2018 Mateusz Pusz
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
#pragma once
24+
25+
#include <mp-units/bits/reference_concepts.h>
26+
#include <mp-units/bits/representation_concepts.h>
27+
28+
namespace mp_units {
29+
30+
template<Reference auto R, RepresentationOf<get_quantity_spec(R).character> Rep>
31+
requires vector_space<Rep>
32+
class quantity;
33+
34+
} // namespace mp_units

0 commit comments

Comments
 (0)