#include <catch2/catch_test_macros.hpp>
#include <string>
namespace example_2d {
(
ANY_FN_DEF(
public,
bool, is_equal, (anyxx::self
const&),
const,
[&x](T const& y) { return x == y; })))
template <typename Proxy = anyxx::val, typename Base = anyxx::base_trait>
using any_has_equal =
anyxx::any<Proxy, has_equal<Base>>;
}
TEST_CASE("example 2da any_has_equal") {
using namespace example_2d;
using namespace anyxx;
using namespace std::string_literals;
{
any_has_equal<> a{std::in_place, "a"s};
any_has_equal<> b{std::in_place, "b"s};
CHECK(a.is_equal(a));
CHECK(!b.is_equal(a));
}
{
CHECK(a.is_equal(a));
CHECK(!b.is_equal(a));
}
}
namespace example_2d {
TRAIT_(has_plus, has_equal<anyxx::base_trait>,
(
ANY_FN_DEF(
public, anyxx::self, plus, (anyxx::self
const&),
const,
([&x](T const& y) {
return get_proxy_value(trait_as<has_plus>(x) +
trait_as<has_plus>(y));
})),
ANY_OP_DEF(
public, anyxx::self, +, plus_op, (anyxx::self
const&),
const,
([&x](T const& y) { return x + y; }))))
template <typename Box = anyxx::val>
using any_has_plus = anyxx::any<Box, has_plus>;
}
TEST_CASE("example 2db any_has_plus dynamic") {
using namespace example_2d;
using namespace anyxx;
using namespace std::string_literals;
{
any_has_plus<> a{std::in_place, "a"s};
any_has_plus<> b{std::in_place, "b"s};
CHECK((a + a).is_equal(any_has_plus<>{"aa"s}));
CHECK((a + b).is_equal(any_has_plus<>{"ab"s}));
CHECK((a.plus(a)).is_equal(any_has_plus<>{"aa"s}));
CHECK((a.plus(b)).is_equal(any_has_plus<>{"ab"s}));
}
}
TEST_CASE("example 2db any_has_plus static") {
using namespace example_2d;
using namespace anyxx;
using namespace std::string_literals;
{
auto a = trait_as<has_plus>("a"s);
auto b = trait_as<has_plus>("b"s);
auto aa = trait_as<has_plus>("aa"s);
auto bb = trait_as<has_plus>("ab"s);
CHECK((a + a).is_equal(aa));
CHECK((a + b).is_equal(bb));
CHECK((a.plus(a)).is_equal(aa));
CHECK((a.plus(b)).is_equal(bb));
}
}
C++ header only library for external polymorphism.
auto trait_as(T &&v)
Definition anyxx.hpp:3143
The core class template to control dispatch for external polymorphism.
Definition anyxx.hpp:2806
#define ANY_OP_DEF(access, ret, op, name, params, const_,...)
TRAIT operator with default behavior.
Definition anyxx.hpp:1066
#define TRAIT_TEMPLATE_(t, n, base, base_template_types, l)
TRAIT template with a base TRAIT.(.
Definition anyxx.hpp:813
#define TRAIT_(n, BASE, l)
TRAIT derived from base.
Definition anyxx.hpp:762
#define ANY_FN_DEF(access, ret, name, params, const_,...)
TRAIT function with default behavior.
Definition anyxx.hpp:998