Skip to content

Commit

Permalink
constant doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jfalcou committed Sep 16, 2023
1 parent 77915c1 commit bbc60af
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
12 changes: 8 additions & 4 deletions include/eve/traits/overload/default_behaviors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <eve/concept/value.hpp>
#include <eve/detail/skeleton.hpp>
#include <eve/detail/function/conditional.hpp>

//======================================================================================================================
// Overload's helpers
Expand Down Expand Up @@ -73,7 +74,9 @@ namespace eve
//!
//! Constants functions in EVE are built using a very common pattern. Inheriting from eve::constant simplifies the
//! implementation of such eve::callable by just requiring your eve::callable type to implement a static `value`
//! member function that provides the constant value.
//! member function that provides the constant value using two parameters:
//! * an eve::options pack containing potential decorators passed to the constant.
//! * an eve::as instance to specify the output type.
//!
//! Constant functions in EVE also supports masking, which is directly implemented in eve::constant.
//!
Expand All @@ -92,10 +95,11 @@ namespace eve
{
template<typename T, typename O>
static constexpr auto call(auto, options<O> const& opts, eve::as<T> const& tgt)
requires( requires{ Tag::value(tgt); } )
requires( requires{ Tag::value(opts, tgt); } )
{
auto v = Tag::value(tgt);
if constexpr(std::same_as<ignore_none_, rbr::result::fetch_t<option::condition,O>>) return v;
// we pass opts to value so each constant can handle their own option support
auto v = Tag::value(opts,tgt);
if constexpr(option_type_is<option::condition, O, ignore_none_>) return v;
else return v & detail::expand_mask(opts[option::condition], tgt).mask();
}
};
Expand Down
4 changes: 4 additions & 0 deletions include/eve/traits/overload/supports.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,8 @@ namespace eve
/// Default settings of eve::conditional is eve::ignore_none
EVE_FORCEINLINE static constexpr auto defaults() noexcept { return options{option::condition = ignore_none}; }
};

/// Checks if the type associated to a given Keyword in a Option pack is equal to Type
template<auto Keyword, typename Opts, typename Type>
inline constexpr bool option_type_is = std::same_as<Type, rbr::result::fetch_t<Keyword,Opts>>;
}
35 changes: 35 additions & 0 deletions test/doc/traits/callable_constant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <type_traits>
#include <eve/traits/overload.hpp>
#include <eve/wide.hpp>

namespace eve
{
struct pi_t : constant<pi_t>
{
template<eve::floating_value T> auto call(as<T>) const -> T;

// A constant must provide a value() static member with two parameters:
// * an eve::options pack containing potential decorators passed to the constant
// * an eve::as instance to specify the output type
template<typename T> static constexpr auto value(auto, eve::as<T>) { return T{3.14159216}; }

EVE_CALLABLE_OBJECT(pi_t, pi_);
} inline constexpr Pi;
};

int main()
{
std::cout << eve::Pi(eve::as(1.0)) << "\n";
std::cout << eve::Pi(eve::as<eve::wide<float>>{}) << "\n";
std::cout << eve::Pi[eve::keep_between(1,3)](eve::as<eve::wide<float>>{}) << "\n\n";


std::cout << "Pi(int) does not compile: "
<< std::boolalpha << !std::is_invocable_v<eve::pi_t, float>
<< "\n";

std::cout << "Pi(wide<unsigned char>) does not compile: "
<< std::boolalpha << !std::is_invocable_v<eve::pi_t, eve::wide<unsigned char>>
<< "\n";
}

0 comments on commit bbc60af

Please sign in to comment.