Any++
Loading...
Searching...
No Matches
Examples for C++26
Introduction to Internal and External Polymorphism:
Using cref:
Using val:
Using shared and weak:
Using any pure:
any function:
any view:
trait_as:
downcast:
crosscast:
Case Study: factory and crosscast:
No Visitors! Open Dispatch:
using namespace anyxx;
template <typename Self>
struct node : anyxx::deleteable<Self> {
using dispatch_table[[= v_table_data]] = anyxx::dispatch_table;
};
static_assert(is_open_dispatch_v_table<any<node>::v_table_t>);
struct Plus {
any<node> left_, right_;
};
struct Times {
any<node> left_, right_;
};
struct Integer {
int i;
};
// =============================================================================
// add behavior to existing classes, without changing them
//-----------------------------------------------------------------------------
// open dispatch evaluate, returns int, dispatched via an any<node>;
dispatch<int(virtual_<any<node>>)> evaluate;
auto __ = evaluate.define<Plus>([](auto const& expr) {
return evaluate(expr.left_) + evaluate(expr.right_);
});
auto __ = evaluate.define<Times>([](auto const& expr) {
return evaluate(expr.left_) * evaluate(expr.right_);
});
auto __ = evaluate.define<Integer>([](auto const& expr) { return expr.i; });
//
//-----------------------------------------------------------------------------
// render as Forth
dispatch<std::string(virtual_<any<node>>)> as_forth;
auto __ = as_forth.define<Plus>([](auto const& expr) {
return as_forth(expr.left_) + " " + as_forth(expr.right_) + " +";
});
auto __ = as_forth.define<Times>([](auto const& expr) {
return as_forth(expr.left_) + " " + as_forth(expr.right_) + " *";
});
auto __ = as_forth.define<Integer>(
[](auto const& expr) { return std::to_string(expr.i); });
//
Open Multi Dispatch for Spaceship Operator
using namespace anyxx;
template <typename Self>
struct thing : copyable<Self> {
std::string registry() const;
using dispatch_table[[= anyxx::v_table_data]] = anyxx::dispatch_table;
};
dispatch<any<thing>(virtual_<any<thing>>, virtual_<any<thing>>)> collide;
any<thing> operator<=>(any<thing> const& l, any<thing> const& r) {
return collide(l, r);
}
any<thing> run(std::vector<any<thing>> things) {
return *std::ranges::fold_left_first(
things, [](any<thing> const& winner, any<thing> const& next) {
std::print("{} <=> {} -> ", winner.registry(), next.registry());
auto result = winner <=> next;
std::println("{}!", result.registry());
return result;
});
}
Open Variant Dispatch: vany
Vany Multi Dispatch: Fancy Expression Tree
template <typename Self>
struct evaluable : copyable<Self> {
std::string to_string() const;
using dispatch_table[[= v_table_data]] = anyxx::dispatch_table;
};
template <typename V>
requires std::formattable<V, char>
struct evaluable<V> {
static std::string to_string(V const& self) {
return std::format("{}", self);
}
};
using value = vany<evaluable, bool, int, double>;
using bool_int_double = vany_type_trait<value>::concrete_variant;
static_assert(std::is_same_v<bool_int_double, std::variant<bool, int, double>>);
template <typename Self>
struct node : anyxx::deleteable<Self> {
value evaluate() const;
using dispatch_table[[= v_table_data]] = anyxx::dispatch_table;
};
VANY_DISPACH_DECLARE(
, evaluate_plus,
(value(virtual_<any<evaluable>>, virtual_<any<evaluable>>)),
([]<no_any L, no_any R>(L&& l, R&& r) -> value
{ return std::forward<L>(l) + std::forward<R>(r);
}));
struct plus {
any<node> left_, right_;
value evaluate() const {
return evaluate_plus(left_.evaluate(), right_.evaluate());
}
};