Any++
Loading...
Searching...
No Matches
_2o_trait_simple.cpp

Simple trait usage

#include <catch2/catch_test_macros.hpp>
#include <string>
namespace simple {
TRAIT(trait1, (ANY_FN(std::string, fn1, (), const)))
std::string cat(std::vector<anyxx::any<anyxx::val, trait1>> const& items) {
std::string result;
for (auto const& item : items) {
result += item.fn1();
}
return result;
}
struct model1 {
std::string s;
[[nodiscard]] std::string fn1() const { return "model1"; }
};
struct model2 {
int x;
};
} // namespace simple
ANY_MODEL_MAP((simple::model2), simple::trait1) {
static std::string fn1(simple::model2 const& m) {
return std::to_string(m.x);
};
};
TEST_CASE("any via concept simple") {
using namespace anyxx;
using namespace simple;
any<val, trait1> test1{model1{"model1"}};
CHECK(test1.fn1() == "model1");
any<val, trait1> test2{model2{42}};
CHECK(test2.fn1() == "42");
CHECK(cat({test1, test2}) == "model142");
}
C++ header only library for external polymorphism.
The core class template to control dispatch for external polymorphism.
Definition anyxx.hpp:2806
#define ANY_FN(ret, name, params, const_)
TRAIT function whose default behavior is to call an equally named member function of the model.
Definition anyxx.hpp:1019
#define ANY_MODEL_MAP(model_, trait_)
ANY_MODEL_MAP macro.
Definition anyxx.hpp:1192
#define TRAIT(n, fns)
Macro to define the functional behavior for an any.
Definition anyxx.hpp:774