Commit 23e09f28 authored by Dominik Charousset's avatar Dominik Charousset

Rename `message::{filter => extract}`

parent 85a2898c
...@@ -30,7 +30,7 @@ using namespace caf; ...@@ -30,7 +30,7 @@ using namespace caf;
int main(int argc, char** argv) { int main(int argc, char** argv) {
string name; string name;
string group_id; string group_id;
auto res = message_builder(argv + 1, argv + argc).filter_cli({ auto res = message_builder(argv + 1, argv + argc).extract_opts({
{"name,n", "set chat name", name}, {"name,n", "set chat name", name},
{"group,g", "join chat group", group_id} {"group,g", "join chat group", group_id}
}); });
......
...@@ -184,7 +184,7 @@ void client_repl(const string& host, uint16_t port) { ...@@ -184,7 +184,7 @@ void client_repl(const string& host, uint16_t port) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
uint16_t port = 0; uint16_t port = 0;
string host = "localhost"; string host = "localhost";
auto res = message_builder(argv + 1, argv + argc).filter_cli({ auto res = message_builder(argv + 1, argv + argc).extract_opts({
{"port,p", "set port (either to publish at or to connect to)", port}, {"port,p", "set port (either to publish at or to connect to)", port},
{"host,H", "set host (client mode only, default: localhost)", host}, {"host,H", "set host (client mode only, default: localhost)", host},
{"server,s", "run in server mode"}, {"server,s", "run in server mode"},
......
...@@ -68,7 +68,7 @@ void client(event_based_actor* self, const string& name) { ...@@ -68,7 +68,7 @@ void client(event_based_actor* self, const string& name) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
string name; string name;
string group_id; string group_id;
auto res = message_builder(argv + 1, argv + argc).filter_cli({ auto res = message_builder(argv + 1, argv + argc).extract_opts({
{"name,n", "set name", name}, {"name,n", "set name", name},
{"group,g", "join group", group_id} {"group,g", "join group", group_id}
}); });
......
...@@ -169,8 +169,8 @@ class message { ...@@ -169,8 +169,8 @@ class message {
* *
* ~~~ * ~~~
* auto msg = make_message(1, 2.f, 3.f, 4); * auto msg = make_message(1, 2.f, 3.f, 4);
* // filter float and integer pairs * // extract float and integer pairs
* auto msg2 = msg.filter({ * auto msg2 = msg.extract({
* [](float, float) { }, * [](float, float) { },
* [](int, int) { } * [](int, int) { }
* }); * });
...@@ -187,10 +187,10 @@ class message { ...@@ -187,10 +187,10 @@ class message {
* - Slice 7: `(4)`, no match * - Slice 7: `(4)`, no match
* *
* Slice 7 is `(4)`, i.e., does not contain the first element, because the * Slice 7 is `(4)`, i.e., does not contain the first element, because the
* match on slice 6 occurred at index position 1. The function `filter` * match on slice 6 occurred at index position 1. The function `extract`
* iterates a message only once, from left to right. * iterates a message only once, from left to right.
*/ */
message filter(message_handler handler) const; message extract(message_handler handler) const;
/** /**
* Stores the name of a command line option ("<long name>[,<short name>]") * Stores the name of a command line option ("<long name>[,<short name>]")
...@@ -246,14 +246,14 @@ class message { ...@@ -246,14 +246,14 @@ class message {
struct cli_res; struct cli_res;
/** /**
* A simplistic interface for using `filter` to parse command line options. * A simplistic interface for using `extract` to parse command line options.
* Usage example: * Usage example:
* *
* ~~~ * ~~~
* int main(int argc, char** argv) { * int main(int argc, char** argv) {
* uint16_t port; * uint16_t port;
* string host = "localhost"; * string host = "localhost";
* auto res = message_builder(argv + 1, argv + argc).filter_cli({ * auto res = message_builder(argv + 1, argv + argc).extract_opts({
* {"port,p", "set port", port}, * {"port,p", "set port", port},
* {"host,H", "set host (default: localhost)", host}, * {"host,H", "set host (default: localhost)", host},
* {"verbose,v", "enable verbose mode"} * {"verbose,v", "enable verbose mode"}
...@@ -264,7 +264,7 @@ class message { ...@@ -264,7 +264,7 @@ class message {
* return 0; * return 0;
* } * }
* if (!res.remainder.empty()) { * if (!res.remainder.empty()) {
* // ... filter did not consume all CLI arguments ... * // ... extract did not consume all CLI arguments ...
* } * }
* if (res.opts.count("verbose") > 0) { * if (res.opts.count("verbose") > 0) {
* // ... enable verbose mode ... * // ... enable verbose mode ...
...@@ -273,7 +273,7 @@ class message { ...@@ -273,7 +273,7 @@ class message {
* } * }
* ~~~ * ~~~
*/ */
cli_res filter_cli(std::vector<cli_arg> args) const; cli_res extract_opts(std::vector<cli_arg> args) const;
/** /**
* Queries whether the element at position `p` is of type `T`. * Queries whether the element at position `p` is of type `T`.
...@@ -373,13 +373,13 @@ class message { ...@@ -373,13 +373,13 @@ class message {
return match_element<T>(P) && match_elements_impl(next_p, next_list); return match_element<T>(P) && match_elements_impl(next_p, next_list);
} }
message filter_impl(size_t start, message_handler handler) const; message extract_impl(size_t start, message_handler handler) const;
data_ptr m_vals; data_ptr m_vals;
}; };
/** /**
* Stores the result of `message::filter_cli`. * Stores the result of `message::extract_opts`.
*/ */
struct message::cli_res { struct message::cli_res {
/** /**
......
...@@ -93,17 +93,17 @@ class message_builder { ...@@ -93,17 +93,17 @@ class message_builder {
message move_to_message(); message move_to_message();
/** /**
* @copydoc message::filter * @copydoc message::extract
*/ */
inline message filter(message_handler f) const { inline message extract(message_handler f) const {
return to_message().filter(f); return to_message().extract(f);
} }
/** /**
* @copydoc message::filter_cli * @copydoc message::extract_opts
*/ */
inline message::cli_res filter_cli(std::vector<message::cli_arg> args) const { inline message::cli_res extract_opts(std::vector<message::cli_arg> args) const {
return to_message().filter_cli(std::move(args)); return to_message().extract_opts(std::move(args));
} }
/** /**
......
...@@ -116,7 +116,7 @@ optional<message> message::apply(message_handler handler) { ...@@ -116,7 +116,7 @@ optional<message> message::apply(message_handler handler) {
return handler(*this); return handler(*this);
} }
message message::filter_impl(size_t start, message_handler handler) const { message message::extract_impl(size_t start, message_handler handler) const {
auto s = size(); auto s = size();
for (size_t i = start; i < s; ++i) { for (size_t i = start; i < s; ++i) {
for (size_t n = (s - i) ; n > 0; --n) { for (size_t n = (s - i) ; n > 0; --n) {
...@@ -132,18 +132,18 @@ message message::filter_impl(size_t start, message_handler handler) const { ...@@ -132,18 +132,18 @@ message message::filter_impl(size_t start, message_handler handler) const {
} }
message next{detail::decorated_tuple::create(m_vals, message next{detail::decorated_tuple::create(m_vals,
std::move(mapping))}; std::move(mapping))};
return next.filter_impl(i, handler); return next.extract_impl(i, handler);
} }
} }
} }
return *this; return *this;
} }
message message::filter(message_handler handler) const { message message::extract(message_handler handler) const {
return filter_impl(0, handler); return extract_impl(0, handler);
} }
message::cli_res message::filter_cli(std::vector<cli_arg> cliargs) const { message::cli_res message::extract_opts(std::vector<cli_arg> cliargs) const {
std::set<std::string> opts; std::set<std::string> opts;
cli_arg dummy{"help,h", ""}; cli_arg dummy{"help,h", ""};
std::map<std::string, cli_arg*> shorts; std::map<std::string, cli_arg*> shorts;
...@@ -171,7 +171,7 @@ message::cli_res message::filter_cli(std::vector<cli_arg> cliargs) const { ...@@ -171,7 +171,7 @@ message::cli_res message::filter_cli(std::vector<cli_arg> cliargs) const {
opts.insert(ptr->name.substr(0, separator)); opts.insert(ptr->name.substr(0, separator));
} }
}; };
auto res = filter({ auto res = extract({
[&](const std::string& arg) -> optional<skip_message_t> { [&](const std::string& arg) -> optional<skip_message_t> {
if (arg.empty() || arg.front() != '-') { if (arg.empty() || arg.front() != '-') {
return skip_message(); return skip_message();
......
...@@ -45,14 +45,14 @@ void test_slice() { ...@@ -45,14 +45,14 @@ void test_slice() {
CAF_CHECK_EQUAL(to_string(m2), to_string(make_message(3, 4))); CAF_CHECK_EQUAL(to_string(m2), to_string(make_message(3, 4)));
} }
void test_filter1(int lhs1, int lhs2, int lhs3, int rhs1, int rhs2, int val) { void test_extract1(int lhs1, int lhs2, int lhs3, int rhs1, int rhs2, int val) {
auto m1 = make_message(lhs1, lhs2, lhs3); auto m1 = make_message(lhs1, lhs2, lhs3);
auto m2 = make_message(rhs1, rhs2); auto m2 = make_message(rhs1, rhs2);
auto m3 = m1.filter(on(val) >> [] {}); auto m3 = m1.extract(on(val) >> [] {});
CAF_CHECK_EQUAL(to_string(m2), to_string(m3)); CAF_CHECK_EQUAL(to_string(m2), to_string(m3));
} }
void test_filter2() { void test_extract2() {
auto m1 = make_message(1.0, 2.0, 3.0); auto m1 = make_message(1.0, 2.0, 3.0);
auto m2 = make_message(1, 2, 1.0, 2.0, 3.0); auto m2 = make_message(1, 2, 1.0, 2.0, 3.0);
auto m3 = make_message(1.0, 1, 2, 2.0, 3.0); auto m3 = make_message(1.0, 1, 2, 2.0, 3.0);
...@@ -64,19 +64,19 @@ void test_filter2() { ...@@ -64,19 +64,19 @@ void test_filter2() {
[](int, int) { }, [](int, int) { },
[](float, float) { } [](float, float) { }
}; };
CAF_CHECK_EQUAL(to_string(m2.filter(f)), to_string(m1)); CAF_CHECK_EQUAL(to_string(m2.extract(f)), to_string(m1));
CAF_CHECK_EQUAL(to_string(m3.filter(f)), to_string(m1)); CAF_CHECK_EQUAL(to_string(m3.extract(f)), to_string(m1));
CAF_CHECK_EQUAL(to_string(m4.filter(f)), to_string(m1)); CAF_CHECK_EQUAL(to_string(m4.extract(f)), to_string(m1));
CAF_CHECK_EQUAL(to_string(m5.filter(f)), to_string(m1)); CAF_CHECK_EQUAL(to_string(m5.extract(f)), to_string(m1));
CAF_CHECK_EQUAL(to_string(m6.filter(f)), to_string(m1)); CAF_CHECK_EQUAL(to_string(m6.extract(f)), to_string(m1));
CAF_CHECK_EQUAL(to_string(m7.filter(f)), to_string(m1)); CAF_CHECK_EQUAL(to_string(m7.extract(f)), to_string(m1));
} }
void test_filter3() { void test_extract3() {
auto m1 = make_message(1); auto m1 = make_message(1);
CAF_CHECK_EQUAL(to_string(m1.filter([](int) {})), to_string(message{})); CAF_CHECK_EQUAL(to_string(m1.extract([](int) {})), to_string(message{}));
auto m2 = make_message(1.0, 2, 3, 4.0); auto m2 = make_message(1.0, 2, 3, 4.0);
auto m3 = m2.filter({ auto m3 = m2.extract({
[](int, int) { }, [](int, int) { },
[](double, double) { } [](double, double) { }
}); });
...@@ -84,10 +84,10 @@ void test_filter3() { ...@@ -84,10 +84,10 @@ void test_filter3() {
CAF_CHECK_EQUAL(to_string(m3), to_string(make_message(1.0, 4.0))); CAF_CHECK_EQUAL(to_string(m3), to_string(make_message(1.0, 4.0)));
} }
void test_filter_cli() { void test_extract_opts() {
auto f = [](std::vector<std::string> args) { auto f = [](std::vector<std::string> args) {
std::string filename; std::string filename;
auto res = message_builder(args.begin(), args.end()).filter_cli({ auto res = message_builder(args.begin(), args.end()).extract_opts({
{"version,v", "print version"}, {"version,v", "print version"},
{"file,f", "set output file", filename}, {"file,f", "set output file", filename},
{"whatever", "do whatever"} {"whatever", "do whatever"}
...@@ -109,12 +109,12 @@ int main() { ...@@ -109,12 +109,12 @@ int main() {
CAF_TEST(message); CAF_TEST(message);
test_drop(); test_drop();
test_slice(); test_slice();
test_filter1(1, 2, 3, 2, 3, 1); test_extract1(1, 2, 3, 2, 3, 1);
test_filter1(1, 2, 3, 1, 3, 2); test_extract1(1, 2, 3, 1, 3, 2);
test_filter1(1, 2, 3, 1, 2, 3); test_extract1(1, 2, 3, 1, 2, 3);
test_filter2(); test_extract2();
test_filter3(); test_extract3();
test_filter_cli(); test_extract_opts();
test_type_token(); test_type_token();
return CAF_TEST_RESULT(); return CAF_TEST_RESULT();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment