Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed as_value to be a new format callable #2058

Merged
merged 7 commits into from
Feb 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 44 additions & 43 deletions include/eve/module/core/constant/as_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,54 @@

namespace eve
{
//================================================================================================
//! @addtogroup core_conversions
//! @{
//! @var as_value
//! @brief converts eve constant or just a value to a type.
//!
//! **Defined in Header**
//!
//! @code
//! #include <eve/module/core.hpp>
//! @endcode
//!
//! @groupheader{Callable Signatures}
//!
//! @code
//! namespace eve
//! {
//! template< eve::value From, eve::value T >
//! T as_value(From x, eve::as<T> t) noexcept;
//! }
//! @endcode
//!
//! **Parameters**
//!
//! * `x`: either something convertible to T or an of eve constant
//! * `t` : [Type wrapper](@ref eve::as) instance embedding the type of the constant.
//!
//! **Return value**
//!
//! The call `eve::as_value(as<T>())` returns a value of type T which is
//! the conversion of `x` to type Target or the the eve constant of type Target.
//!
//! @groupheader{Example}
//! @godbolt{doc/core/constant/as_value.cpp}
//================================================================================================
EVE_MAKE_CALLABLE(as_value_, as_value);

namespace detail
template<typename Options>
struct as_value_t : constant_callable<as_value_t, Options>
SadiinsoSnowfall marked this conversation as resolved.
Show resolved Hide resolved
{
template<typename From, value T>
EVE_FORCEINLINE constexpr auto as_value_(EVE_SUPPORTS(cpu_), From from, as<T> const& t) noexcept
EVE_FORCEINLINE constexpr T operator()(From from, as<T> t) const noexcept
{
if constexpr( requires { typename From::constant_callable_tag; } ) return from(t);//if constexpr( instance_of<From,functor> ) return from(t);
else if constexpr( scalar_value<T> ) return static_cast<T>(from);
else return T {from};
if constexpr (requires { typename From::constant_callable_tag; }) return from(t);
else if constexpr (scalar_value<T>) return static_cast<T>(from);
else return T{from};
}
}
};

//================================================================================================
//! @addtogroup core_conversions
//! @{
//! @var as_value
//! @brief converts eve constant or just a value to a type.
//!
//! **Defined in Header**
//!
//! @code
//! #include <eve/module/core.hpp>
//! @endcode
//!
//! @groupheader{Callable Signatures}
//!
//! @code
//! namespace eve
//! {
//! template< eve::value From, eve::value T >
//! T as_value(From x, eve::as<T> t) noexcept;
//! }
//! @endcode
//!
//! **Parameters**
//!
//! * `x`: either something convertible to T or an of eve constant
//! * `t` : [Type wrapper](@ref eve::as) instance embedding the type of the constant.
//!
//! **Return value**
//!
//! The call `eve::as_value(as<T>())` returns a value of type T which is
//! the conversion of `x` to type Target or the the eve constant of type Target.
//!
//! @groupheader{Example}
//! @godbolt{doc/core/constant/as_value.cpp}
//================================================================================================
inline constexpr auto as_value = functor<as_value_t>;
//================================================================================================
//! @}
//================================================================================================
Expand Down