Skip to content

Commit

Permalink
feat!: add number concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Sep 21, 2023
1 parent a356db7 commit bab7acf
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 107 deletions.
31 changes: 30 additions & 1 deletion example/include/ranged_representation.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template<std::movable T, MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_t
MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Max>
using is_in_range_t = decltype(is_in_range<T, Min, Max>);

template<std::movable T, MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Min,
template<mp_units::vector_space T, MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Min,
MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(std::convertible_to<T>) auto Max>
class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max>> {
public:
Expand All @@ -50,15 +50,44 @@ class ranged_representation : public validated_type<T, is_in_range_t<T, Min, Max
{
return ranged_representation(-this->value());
}

friend constexpr ranged_representation operator-(const ranged_representation& lhs, const ranged_representation& rhs)
{
return ranged_representation(lhs.value() - rhs.value());
}

constexpr ranged_representation& operator+=(ranged_representation that)
{
this->value() += that.value();
gsl_Expects(validate(this->value()));
return *this;
}
constexpr ranged_representation& operator-=(ranged_representation that)
{
this->value() -= that.value();
gsl_Expects(validate(this->value()));
return *this;
}
constexpr ranged_representation& operator*=(T rhs)
{
this->value() *= rhs;
gsl_Expects(validate(this->value()));
return *this;
}
};

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

template<typename T, auto Min, auto Max>
struct mp_units::number_scalar<ranged_representation<T, Min, Max>> : std::type_identity<T> {};

template<typename T, auto Min, auto Max>
inline constexpr bool mp_units::treat_as_floating_point<ranged_representation<T, Min, Max>> =
mp_units::treat_as_floating_point<T>;

static_assert(mp_units::vector_space<ranged_representation<int, 17, 29>>);

template<typename T, auto Min, auto Max>
struct MP_UNITS_STD_FMT::formatter<ranged_representation<T, Min, Max>> : formatter<T> {
template<typename FormatContext>
Expand Down
24 changes: 23 additions & 1 deletion example/measurement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace {

template<typename T>
template<mp_units::vector_space T>
class measurement {
public:
using value_type = T;
Expand Down Expand Up @@ -65,6 +65,18 @@ class measurement {
return measurement(lhs.value() - rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty()));
}

constexpr measurement& operator+=(measurement that)
{
value_ = *this + that;
return *this;
}

constexpr measurement& operator-=(measurement that)
{
value_ = *this - that;
return *this;
}

[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const measurement& rhs)
{
const auto val = lhs.value() * rhs.value();
Expand All @@ -78,6 +90,12 @@ class measurement {
return measurement(val, val * lhs.relative_uncertainty());
}

constexpr measurement& operator*=(const value_type& value)
{
value_ = *this * value;
return *this;
}

[[nodiscard]] friend constexpr measurement operator*(const value_type& value, const measurement& rhs)
{
const auto val = rhs.value() * value;
Expand Down Expand Up @@ -125,7 +143,11 @@ inline constexpr bool mp_units::is_scalar<measurement<T>> = true;
template<class T>
inline constexpr bool mp_units::is_vector<measurement<T>> = true;

template<typename T>
struct mp_units::number_scalar<measurement<T>> : std::type_identity<T> {};

static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::scalar>);
static_assert(mp_units::vector_space<measurement<double>>);

namespace {

Expand Down
1 change: 1 addition & 0 deletions src/core/include/mp-units/bits/external/type_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <mp-units/bits/external/hacks.h> // IWYU pragma: keep
#include <cstddef>
#include <type_traits>
#include <utility>

MP_UNITS_DIAGNOSTIC_PUSH
Expand Down
34 changes: 34 additions & 0 deletions src/core/include/mp-units/bits/fwd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <mp-units/bits/reference_concepts.h>
#include <mp-units/bits/representation_concepts.h>

namespace mp_units {

template<Reference auto R, RepresentationOf<get_quantity_spec(R).character> Rep>
requires vector_space<Rep>
class quantity;

} // namespace mp_units
4 changes: 1 addition & 3 deletions src/core/include/mp-units/bits/quantity_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@

#pragma once

#include <mp-units/bits/fwd.h>
#include <mp-units/bits/quantity_spec_concepts.h>
#include <mp-units/bits/reference_concepts.h>
#include <mp-units/bits/representation_concepts.h>
#include <mp-units/customization_points.h>

namespace mp_units {

template<Reference auto R, RepresentationOf<get_quantity_spec(R).character> Rep>
class quantity;

#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG < 17
template<auto R, typename Rep>
#else
Expand Down
1 change: 1 addition & 0 deletions src/core/include/mp-units/bits/quantity_point_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ concept PointOriginFor = PointOrigin<T> && QuantitySpecOf<std::remove_const_t<de

template<Reference auto R, PointOriginFor<get_quantity_spec(R)> auto PO,
RepresentationOf<get_quantity_spec(R).character> Rep>
requires vector_space<Rep>
class quantity_point;

#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG < 17
Expand Down
1 change: 1 addition & 0 deletions src/core/include/mp-units/customization_points.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <mp-units/bits/external/hacks.h>
#include <mp-units/bits/external/type_traits.h>
#include <mp-units/numbers.h>
#include <limits>
#include <type_traits>

Expand Down
Loading

0 comments on commit bab7acf

Please sign in to comment.