Commit 9fdea6a4 authored by Dominik Charousset's avatar Dominik Charousset

Remove match_result

parent be8c3f34
......@@ -6,8 +6,6 @@ file(GLOB_RECURSE CAF_CORE_HEADERS "caf/*.hpp")
add_enum_consistency_check("caf/sec.hpp" "src/sec_strings.cpp")
add_enum_consistency_check("caf/pec.hpp" "src/pec_strings.cpp")
add_enum_consistency_check("caf/match_result.hpp"
"src/match_result_strings.cpp")
add_enum_consistency_check("caf/stream_priority.hpp"
"src/stream_priority_strings.cpp")
add_enum_consistency_check("caf/exit_reason.hpp"
......@@ -111,7 +109,6 @@ set(CAF_CORE_SOURCES
src/logger.cpp
src/mailbox_element.cpp
src/make_config_option.cpp
src/match_result_strings.cpp
src/memory_managed.cpp
src/message.cpp
src/message_builder.cpp
......
......@@ -99,8 +99,8 @@ public:
}
/// Runs this handler with callback.
match_result operator()(detail::invoke_result_visitor& f, message& xs) {
return impl_ ? impl_->invoke(f, xs) : match_result::no_match;
bool operator()(detail::invoke_result_visitor& f, message& xs) {
return impl_ ? impl_->invoke(f, xs) : false;
}
/// Checks whether this behavior is not empty.
......
......@@ -31,7 +31,6 @@
#include "caf/detail/type_traits.hpp"
#include "caf/intrusive_ptr.hpp"
#include "caf/make_counted.hpp"
#include "caf/match_result.hpp"
#include "caf/message.hpp"
#include "caf/none.hpp"
#include "caf/optional.hpp"
......@@ -62,10 +61,9 @@ public:
explicit behavior_impl(timespan tout);
match_result invoke_empty(detail::invoke_result_visitor& f);
bool invoke_empty(detail::invoke_result_visitor& f);
virtual match_result invoke(detail::invoke_result_visitor& f, message& xs)
= 0;
virtual bool invoke(detail::invoke_result_visitor& f, message& xs) = 0;
optional<message> invoke(message&);
......@@ -123,14 +121,13 @@ public:
// nop
}
virtual match_result invoke(detail::invoke_result_visitor& f,
message& xs) override {
virtual bool invoke(detail::invoke_result_visitor& f, message& xs) override {
return invoke_impl(f, xs, std::make_index_sequence<sizeof...(Ts)>{});
}
template <size_t... Is>
match_result invoke_impl(detail::invoke_result_visitor& f, message& msg,
std::index_sequence<Is...>) {
bool invoke_impl(detail::invoke_result_visitor& f, message& msg,
std::index_sequence<Is...>) {
auto dispatch = [&](auto& fun) {
using fun_type = std::decay_t<decltype(fun)>;
using trait = get_callable_trait_t<fun_type>;
......@@ -149,8 +146,7 @@ public:
}
return false;
};
bool dispatched = (dispatch(std::get<Is>(cases_)) || ...);
return dispatched ? match_result::match : match_result::no_match;
return (dispatch(std::get<Is>(cases_)) || ...);
}
void handle_timeout() override {
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <string>
#include "caf/detail/core_export.hpp"
namespace caf {
/// Denotes the invoke result of a ::behavior or ::message_handler.
enum class match_result {
match,
no_match,
};
/// @relates match_result
CAF_CORE_EXPORT std::string to_string(match_result);
} // namespace caf
......@@ -91,8 +91,8 @@ public:
}
/// Runs this handler with callback.
match_result operator()(detail::invoke_result_visitor& f, message& xs) {
return impl_ ? impl_->invoke(f, xs) : match_result::no_match;
bool operator()(detail::invoke_result_visitor& f, message& xs) {
return impl_ ? impl_->invoke(f, xs) : false;
}
/// Returns a new handler that concatenates this handler
......
......@@ -190,35 +190,29 @@ blocking_actor::mailbox_visitor::operator()(mailbox_element& x) {
[&] { self->current_element_ = prev_element; });
// Dispatch on x.
detail::default_invoke_result_visitor<blocking_actor> visitor{self};
switch (bhvr.nested(visitor, x.content())) {
default:
if (bhvr.nested(visitor, x.content()))
return check_if_done();
// Blocking actors can have fallback handlers for catch-all rules.
auto sres = bhvr.fallback(self->current_element_->payload);
auto f = detail::make_overload(
[&](skip_t&) {
// Response handlers must get re-invoked with an error when
// receiving an unexpected message.
if (mid.is_response()) {
auto err = make_error(sec::unexpected_response, std::move(x.payload));
mailbox_element tmp{std::move(x.sender), x.mid, std::move(x.stages),
make_message(std::move(err))};
self->current_element_ = &tmp;
bhvr.nested(tmp.content());
return check_if_done();
}
return intrusive::task_result::skip;
},
[&](auto& res) {
visitor(res);
return check_if_done();
case match_result::no_match: { // Blocking actors can have fallback
// handlers for catch-all rules.
auto sres = bhvr.fallback(self->current_element_->payload);
auto f = detail::make_overload(
[&](skip_t&) {
// Response handlers must get re-invoked with an error when
// receiving an unexpected message.
if (mid.is_response()) {
auto err = make_error(sec::unexpected_response,
std::move(x.payload));
mailbox_element tmp{std::move(x.sender), x.mid,
std::move(x.stages),
make_message(std::move(err))};
self->current_element_ = &tmp;
bhvr.nested(tmp.content());
return check_if_done();
}
return intrusive::task_result::skip;
},
[&](auto& res) {
visitor(res);
return check_if_done();
});
return visit(f, sres);
}
}
});
return visit(f, sres);
};
// Post-process the returned value from the function body.
auto result = body();
......
......@@ -28,9 +28,8 @@ namespace {
class combinator final : public behavior_impl {
public:
match_result invoke(detail::invoke_result_visitor& f, message& xs) override {
auto x = first->invoke(f, xs);
return x == match_result::no_match ? second->invoke(f, xs) : x;
bool invoke(detail::invoke_result_visitor& f, message& xs) override {
return first->invoke(f, xs) || second->invoke(f, xs);
}
void handle_timeout() override {
......@@ -76,27 +75,20 @@ behavior_impl::behavior_impl(timespan tout) : timeout_(tout) {
// nop
}
match_result behavior_impl::invoke_empty(detail::invoke_result_visitor& f) {
bool behavior_impl::invoke_empty(detail::invoke_result_visitor& f) {
message xs;
return invoke(f, xs);
}
optional<message> behavior_impl::invoke(message& xs) {
maybe_message_visitor f;
// the following const-cast is safe, because invoke() is aware of
// copy-on-write and does not modify x if it's shared
if (!xs.empty())
invoke(f, xs);
else
invoke_empty(f);
return std::move(f.value);
if (invoke(f, xs))
return std::move(f.value);
return none;
}
match_result behavior_impl::invoke(detail::invoke_result_visitor& f,
message& xs) {
if (!xs.empty())
return invoke(f, xs);
return invoke_empty(f);
bool behavior_impl::invoke(detail::invoke_result_visitor& f, message& xs) {
return invoke(f, xs);
}
void behavior_impl::handle_timeout() {
......
// clang-format off
// DO NOT EDIT: this file is auto-generated by caf-generate-enum-strings.
// Run the target update-enum-strings if this file is out of sync.
#include "caf/match_result.hpp"
#include <string>
namespace caf {
std::string to_string(match_result x) {
switch(x) {
default:
return "???";
case match_result::match:
return "match";
case match_result::no_match:
return "no_match";
};
}
} // namespace caf
......@@ -87,7 +87,7 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
unsetf(has_timeout_flag);
if (!bhvr_stack_.empty()) {
auto& bhvr = bhvr_stack_.back();
if (bhvr(visitor, x.content()) == match_result::match)
if (bhvr(visitor, x.content()))
return invoke_message_result::consumed;
}
auto sres = call_handler(default_handler_, this, x.payload);
......
......@@ -696,7 +696,7 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
unsetf(has_timeout_flag);
if (!bhvr_stack_.empty()) {
auto& bhvr = bhvr_stack_.back();
if (bhvr(visitor, x.content()) == match_result::match)
if (bhvr(visitor, x.content()))
return invoke_message_result::consumed;
}
auto sres = call_handler(default_handler_, this, x.payload);
......@@ -1108,34 +1108,19 @@ scheduled_actor::handle_open_stream_msg(mailbox_element& x) {
auto rp = make_response_promise();
rp.deliver(sec::stream_init_failed);
};
// Utility for invoking the default handler.
auto fallback = [&] {
auto sres = call_handler(default_handler_, this, x.payload);
if (holds_alternative<skip_t>(sres)) {
CAF_LOG_DEBUG("default handler skipped open_stream_msg:" << osm.msg);
return invoke_message_result::skipped;
} else {
CAF_LOG_DEBUG(
"default handler was called for open_stream_msg:" << osm.msg);
fail(sec::stream_init_failed, "dropped open_stream_msg (no match)");
return invoke_message_result::dropped;
}
};
// Invoke behavior and dispatch on the result.
auto& bs = bhvr_stack();
if (bs.empty())
return fallback();
auto res = (bs.back())(f, osm.msg);
switch (res) {
case match_result::no_match:
CAF_LOG_DEBUG("no match in behavior, fall back to default handler");
return fallback();
case match_result::match: {
return invoke_message_result::consumed;
}
default:
CAF_LOG_DEBUG("behavior skipped open_stream_msg:" << osm.msg);
return invoke_message_result::skipped; // nop
if (!bs.empty() && bs.back()(f, osm.msg))
return invoke_message_result::consumed;
CAF_LOG_DEBUG("no match in behavior, fall back to default handler");
auto sres = call_handler(default_handler_, this, x.payload);
if (holds_alternative<skip_t>(sres)) {
CAF_LOG_DEBUG("default handler skipped open_stream_msg:" << osm.msg);
return invoke_message_result::skipped;
} else {
CAF_LOG_DEBUG("default handler was called for open_stream_msg:" << osm.msg);
fail(sec::stream_init_failed, "dropped open_stream_msg (no match)");
return invoke_message_result::dropped;
}
}
......
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