Skip to content

Commit

Permalink
refactor: initial CPOs for representation types and their usage in re…
Browse files Browse the repository at this point in the history
…presentation concepts
  • Loading branch information
mpusz committed Nov 21, 2024
1 parent 0c09008 commit f84ed8f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
106 changes: 106 additions & 0 deletions src/core/include/mp-units/framework/customization_points.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,112 @@ constexpr bool is_vector = false;
template<typename Rep>
constexpr bool is_tensor = false;

MP_UNITS_EXPORT_END

namespace detail::inline norm_impl {

void norm() = delete; // poison pill

struct norm_t {
template<typename T>
[[nodiscard]] constexpr auto operator()(const T& vec) const
{
if constexpr (requires { vec.norm(); })
return vec.norm();
else if constexpr (requires { norm(vec); })
return norm(vec);
else if constexpr (requires { vec.magnitude(); })
return vec.magnitude();
else if constexpr (requires { magnitude(vec); })
return magnitude(vec);
// TODO Is it a good idea to enable fundamental types to represent vector quantities?
// else if constexpr (is_scalar<T>)
// return std::abs(vec);
}
};

} // namespace detail::inline norm_impl

inline namespace cpo {

MP_UNITS_EXPORT inline constexpr ::mp_units::detail::norm_impl::norm_t norm;

}

namespace detail::inline real_impl {

void real() = delete; // poison pill

struct real_t {
template<typename T>
[[nodiscard]] constexpr auto operator()(const T& clx) const
{
if constexpr (requires { clx.real(); })
return clx.real();
else if constexpr (requires { real(clx); })
return real(clx);
}
};

} // namespace detail::inline real_impl

inline namespace cpo {

MP_UNITS_EXPORT inline constexpr ::mp_units::detail::real_impl::real_t real;

}

namespace detail::inline imag_impl {

void imag() = delete; // poison pill

struct imag_t {
template<typename T>
[[nodiscard]] constexpr auto operator()(const T& clx) const
{
if constexpr (requires { clx.imag(); })
return clx.imag();
else if constexpr (requires { imag(clx); })
return imag(clx);
}
};

} // namespace detail::inline imag_impl

inline namespace cpo {

MP_UNITS_EXPORT inline constexpr ::mp_units::detail::imag_impl::imag_t imag;

}

namespace detail::inline modulus_impl {

void modulus() = delete; // poison pill

struct modulus_t {
template<typename T>
[[nodiscard]] constexpr auto operator()(const T& clx) const
{
if constexpr (requires { clx.modulus(); })
return clx.modulus();
else if constexpr (requires { modulus(clx); })
return modulus(clx);
// `std` made a precedence of using `abs` for modulo on `std::complex`
else if constexpr (requires { abs(clx); })
return abs(clx);
}
};

} // namespace detail::inline modulus_impl

inline namespace cpo {

MP_UNITS_EXPORT inline constexpr ::mp_units::detail::modulus_impl::modulus_t modulus;

}

MP_UNITS_EXPORT_BEGIN

/**
* @brief A type trait that defines zero, one, min, and max for a representation type
*
Expand Down
10 changes: 4 additions & 6 deletions src/core/include/mp-units/framework/representation_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ concept ComplexRepresentation = Complex<T> && WeaklyRegular<T> && requires(T a,
{ a - b } -> Complex;
{ a* b } -> Complex;
{ a / b } -> Complex;
{ real(a) } -> Scalar;
{ imag(a) } -> Scalar;
{ abs(a) } -> Scalar;
{ arg(a) } -> Scalar;
{ conj(a) } -> Complex;
{ ::mp_units::real(a) } -> Scalar;
{ ::mp_units::imag(a) } -> Scalar;
{ ::mp_units::modulus(a) } -> Scalar;
};

// TODO how to check for a complex(Scalar, Scalar) -> Complex?
Expand All @@ -138,8 +136,8 @@ concept VectorRepresentation = Vector<T> && WeaklyRegular<T> && requires(T a, T
{ -a } -> Vector;
{ a + b } -> Vector;
{ a - b } -> Vector;
{ ::mp_units::norm(a) } -> Scalar;
// TBD
// { norm(a) } -> Scalar;
// { zero_vector<T>() } -> Vector;
// { unit_vector(a) } -> Vector;
// { scalar_product(a, b) } -> Scalar;
Expand Down

0 comments on commit f84ed8f

Please sign in to comment.