Fatal , .
, :
#include <fatal/type/map.h>
#include <fatal/type/string_lookup.h>
#include <fatal/type/sequence.h>
, .
struct foo_action {
FATAL_STR(name, "foo");
static void DOIT() { std::cout << "FOO-ACTION"; }
};
struct bar_action {
FATAL_STR(name, "bar");
static void DOIT() { std::cout << "BAR-ACTION"; }
};
struct baz_action {
FATAL_STR(name, "baz");
static void DOIT() { std::cout << "BAZ-ACTION"; }
};
:
using my_map = fatal::build_type_map<
foo_action::name, foo_action,
bar_action::name, bar_action,
baz_action::name, baz_action
>;
, , , . , -, :
using my_lookup = my_map::keys::apply<fatal::string_lookup>;
, , .
, , .
. a1 a2 . :
struct lookup_visitor {
template <typename Key>
void operator ()(fatal::type_tag<Key>, int a1, std::string const &a2) const {
using type = typename my_map::template get<Key>;
std::cout << "performing action from type '" << typeid(type).name()
<< "' (additional args from the call to exact: a1="
<< a1 << ", a2='" << a2 << "'):";
type::DOIT();
std::cout << std::endl;
}
};
, , , .
in , .
,
exact(). exact(), . , , .
56 "test":
int main() {
for (std::string in; std::cout << "lookup: ", std::cin >> in; ) {
bool const found = my_lookup::match<>::exact(
in.begin(), in.end(), lookup_visitor(),
56, "test"
);
if (!found) {
std::cout << "no match was found for string '" << in << '\''
<< std::endl;
}
}
return 0;
}
:
$ clang++ -Wall -std=c++11 -I path/to/fatal sample.cpp -o sample && ./sample
lookup: somestring
no match was found for string 'somestring'
lookup: test
no match was found for string 'test'
lookup: foo
performing action from type '10foo_action' (additional args from the call to exact: a1=56, a2='test'): FOO-ACTION
lookup: bar
performing action from type '10bar_action' (additional args from the call to exact: a1=56, a2='test'): BAR-ACTION
lookup: ^D
$
, , , my_map. .
: , .