Skip to content

Commit

Permalink
refactor: make_reference used to return values from reference opera…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
mpusz committed Feb 6, 2024
1 parent 1663aa0 commit 963256a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 74 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ add_mp_units_module(
include/mp-units/bits/get_associated_quantity.h
include/mp-units/bits/get_common_base.h
include/mp-units/bits/magnitude.h
include/mp-units/bits/make_reference.h
include/mp-units/bits/quantity_cast.h
include/mp-units/bits/quantity_concepts.h
include/mp-units/bits/quantity_point_concepts.h
Expand Down
39 changes: 39 additions & 0 deletions src/core/include/mp-units/bits/make_reference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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/quantity_concepts.h>
#include <mp-units/bits/reference_concepts.h>

namespace mp_units::detail {

template<QuantitySpec QS, Unit U>
[[nodiscard]] consteval Reference auto make_reference(QS, U u)
{
if constexpr (detail::QuantityKindSpec<QS>)
return u;
else
return reference<QS, U>{};
}

} // namespace mp_units::detail
12 changes: 1 addition & 11 deletions src/core/include/mp-units/quantity_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <mp-units/bits/external/type_name.h>
#include <mp-units/bits/external/type_traits.h>
#include <mp-units/bits/get_common_base.h>
#include <mp-units/bits/make_reference.h>
#include <mp-units/bits/quantity_concepts.h>
#include <mp-units/bits/quantity_spec_concepts.h>
#include <mp-units/bits/reference_concepts.h>
Expand All @@ -39,17 +40,6 @@ namespace mp_units {

namespace detail {


template<QuantitySpec QS, Unit U>
requires(!AssociatedUnit<U>) || UnitOf<U, QS{}>
[[nodiscard]] consteval Reference auto make_reference(QS, U u)
{
if constexpr (detail::QuantityKindSpec<QS>)
return u;
else
return reference<QS, U>{};
}

// TODO revise the note in the below comment
/**
* @brief Returns the most restrictive character from the list
Expand Down
84 changes: 21 additions & 63 deletions src/core/include/mp-units/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#pragma once

#include <mp-units/bits/external/hacks.h>
#include <mp-units/bits/get_associated_quantity.h>
#include <mp-units/bits/make_reference.h>
#include <mp-units/bits/quantity_concepts.h>
#include <mp-units/bits/reference_concepts.h>
#include <mp-units/bits/representation_concepts.h>
Expand Down Expand Up @@ -68,74 +68,44 @@ struct reference {
}

template<typename Q2, typename U2>
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} * Q2{})>,
std::remove_const_t<decltype(U{} * U2{})>>
operator*(reference, reference<Q2, U2>)
[[nodiscard]] friend consteval Reference auto operator*(reference, reference<Q2, U2>)
{
return {};
return detail::make_reference(Q{} * Q2{}, U{} * U2{});
}

template<AssociatedUnit U2>
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<Q * get_quantity_spec(U2{}), U * U2{}>{}) operator*(reference, U2)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} * get_quantity_spec(U2{}))>,
std::remove_const_t<decltype(U{} * U2{})>>
operator*(reference, U2)
#endif
[[nodiscard]] friend consteval Reference auto operator*(reference, U2)
{
return {};
return detail::make_reference(Q{} * get_quantity_spec(U2{}), U{} * U2{});
}

template<AssociatedUnit U1>
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<get_quantity_spec(U1{}) * Q, U1{} * U>{}) operator*(U1, reference)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(get_quantity_spec(U1{}) * Q{})>,
std::remove_const_t<decltype(U1{} * U{})>>
operator*(U1, reference)
#endif
[[nodiscard]] friend consteval Reference auto operator*(U1, reference)
{
return {};
return detail::make_reference(get_quantity_spec(U1{}) * Q{}, U1{} * U{});
}

template<typename Q2, typename U2>
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} / Q2{})>,
std::remove_const_t<decltype(U{} / U2{})>>
operator/(reference, reference<Q2, U2>)
[[nodiscard]] friend consteval Reference auto operator/(reference, reference<Q2, U2>)
{
return {};
return detail::make_reference(Q{} / Q2{}, U{} / U2{});
}

template<AssociatedUnit U2>
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<Q / get_quantity_spec(U2{}), U / U2{}>{}) operator/(reference, U2)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} / get_quantity_spec(U2{}))>,
std::remove_const_t<decltype(U{} / U2{})>>
operator/(reference, U2)
#endif
[[nodiscard]] friend consteval Reference auto operator/(reference, U2)
{
return {};
return detail::make_reference(Q{} / get_quantity_spec(U2{}), U{} / U2{});
}

template<AssociatedUnit U1>
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<get_quantity_spec(U1{}) / Q, U1{} / U>{}) operator/(U1, reference)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(get_quantity_spec(U1{}) / Q{})>,
std::remove_const_t<decltype(U1{} / U{})>>
operator/(U1, reference)
#endif
[[nodiscard]] friend consteval Reference auto operator/(U1, reference)
{
return {};
return detail::make_reference(get_quantity_spec(U1{}) / Q{}, U1{} / U{});
}

[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(inverse(Q{}))>,
std::remove_const_t<decltype(inverse(U{}))>>
inverse(reference)
[[nodiscard]] friend consteval Reference auto inverse(reference)
{
return {};
return detail::make_reference(inverse(Q{}), inverse(U{}));
}

/**
Expand All @@ -149,11 +119,9 @@ struct reference {
*/
template<std::intmax_t Num, std::intmax_t Den = 1>
requires detail::non_zero<Den>
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(pow<Num, Den>(Q{}))>,
std::remove_const_t<decltype(pow<Num, Den>(U{}))>>
pow(reference)
[[nodiscard]] friend consteval Reference auto pow(reference)
{
return {};
return detail::make_reference(pow<Num, Den>(Q{}), pow<Num, Den>(U{}));
}

/**
Expand All @@ -163,12 +131,7 @@ struct reference {
*
* @return The result of computation
*/
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(sqrt(Q{}))>,
std::remove_const_t<decltype(sqrt(U{}))>>
sqrt(reference)
{
return {};
}
[[nodiscard]] friend consteval Reference auto sqrt(reference) { return detail::make_reference(sqrt(Q{}), sqrt(U{})); }

/**
* @brief Computes the cubic root of a reference
Expand All @@ -177,12 +140,7 @@ struct reference {
*
* @return The result of computation
*/
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(cbrt(Q{}))>,
std::remove_const_t<decltype(cbrt(U{}))>>
cbrt(reference)
{
return {};
}
[[nodiscard]] friend consteval Reference auto cbrt(reference) { return detail::make_reference(cbrt(Q{}), cbrt(U{})); }

template<typename Q2, typename U2>
[[nodiscard]] friend consteval bool convertible(reference, reference<Q2, U2>)
Expand Down Expand Up @@ -289,9 +247,9 @@ template<AssociatedUnit auto To, AssociatedUnit From>
}

template<Unit auto To, QuantitySpec QS, Unit U>
[[nodiscard]] consteval reference<QS, std::remove_const_t<decltype(To)>> clone_reference_with(reference<QS, U>)
[[nodiscard]] consteval Reference auto clone_reference_with(reference<QS, U>)
{
return {};
return detail::make_reference(QS{}, To);
}

} // namespace detail
Expand Down

0 comments on commit 963256a

Please sign in to comment.