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") ...@@ -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/sec.hpp" "src/sec_strings.cpp")
add_enum_consistency_check("caf/pec.hpp" "src/pec_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" add_enum_consistency_check("caf/stream_priority.hpp"
"src/stream_priority_strings.cpp") "src/stream_priority_strings.cpp")
add_enum_consistency_check("caf/exit_reason.hpp" add_enum_consistency_check("caf/exit_reason.hpp"
...@@ -111,7 +109,6 @@ set(CAF_CORE_SOURCES ...@@ -111,7 +109,6 @@ set(CAF_CORE_SOURCES
src/logger.cpp src/logger.cpp
src/mailbox_element.cpp src/mailbox_element.cpp
src/make_config_option.cpp src/make_config_option.cpp
src/match_result_strings.cpp
src/memory_managed.cpp src/memory_managed.cpp
src/message.cpp src/message.cpp
src/message_builder.cpp src/message_builder.cpp
......
...@@ -99,8 +99,8 @@ public: ...@@ -99,8 +99,8 @@ public:
} }
/// Runs this handler with callback. /// Runs this handler with callback.
match_result operator()(detail::invoke_result_visitor& f, message& xs) { bool operator()(detail::invoke_result_visitor& f, message& xs) {
return impl_ ? impl_->invoke(f, xs) : match_result::no_match; return impl_ ? impl_->invoke(f, xs) : false;
} }
/// Checks whether this behavior is not empty. /// Checks whether this behavior is not empty.
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include "caf/detail/type_traits.hpp" #include "caf/detail/type_traits.hpp"
#include "caf/intrusive_ptr.hpp" #include "caf/intrusive_ptr.hpp"
#include "caf/make_counted.hpp" #include "caf/make_counted.hpp"
#include "caf/match_result.hpp"
#include "caf/message.hpp" #include "caf/message.hpp"
#include "caf/none.hpp" #include "caf/none.hpp"
#include "caf/optional.hpp" #include "caf/optional.hpp"
...@@ -62,10 +61,9 @@ public: ...@@ -62,10 +61,9 @@ public:
explicit behavior_impl(timespan tout); 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) virtual bool invoke(detail::invoke_result_visitor& f, message& xs) = 0;
= 0;
optional<message> invoke(message&); optional<message> invoke(message&);
...@@ -123,13 +121,12 @@ public: ...@@ -123,13 +121,12 @@ public:
// nop // nop
} }
virtual match_result invoke(detail::invoke_result_visitor& f, virtual bool invoke(detail::invoke_result_visitor& f, message& xs) override {
message& xs) override {
return invoke_impl(f, xs, std::make_index_sequence<sizeof...(Ts)>{}); return invoke_impl(f, xs, std::make_index_sequence<sizeof...(Ts)>{});
} }
template <size_t... Is> template <size_t... Is>
match_result invoke_impl(detail::invoke_result_visitor& f, message& msg, bool invoke_impl(detail::invoke_result_visitor& f, message& msg,
std::index_sequence<Is...>) { std::index_sequence<Is...>) {
auto dispatch = [&](auto& fun) { auto dispatch = [&](auto& fun) {
using fun_type = std::decay_t<decltype(fun)>; using fun_type = std::decay_t<decltype(fun)>;
...@@ -149,8 +146,7 @@ public: ...@@ -149,8 +146,7 @@ public:
} }
return false; return false;
}; };
bool dispatched = (dispatch(std::get<Is>(cases_)) || ...); return (dispatch(std::get<Is>(cases_)) || ...);
return dispatched ? match_result::match : match_result::no_match;
} }
void handle_timeout() override { 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: ...@@ -91,8 +91,8 @@ public:
} }
/// Runs this handler with callback. /// Runs this handler with callback.
match_result operator()(detail::invoke_result_visitor& f, message& xs) { bool operator()(detail::invoke_result_visitor& f, message& xs) {
return impl_ ? impl_->invoke(f, xs) : match_result::no_match; return impl_ ? impl_->invoke(f, xs) : false;
} }
/// Returns a new handler that concatenates this handler /// Returns a new handler that concatenates this handler
......
...@@ -190,21 +190,17 @@ blocking_actor::mailbox_visitor::operator()(mailbox_element& x) { ...@@ -190,21 +190,17 @@ blocking_actor::mailbox_visitor::operator()(mailbox_element& x) {
[&] { self->current_element_ = prev_element; }); [&] { self->current_element_ = prev_element; });
// Dispatch on x. // Dispatch on x.
detail::default_invoke_result_visitor<blocking_actor> visitor{self}; detail::default_invoke_result_visitor<blocking_actor> visitor{self};
switch (bhvr.nested(visitor, x.content())) { if (bhvr.nested(visitor, x.content()))
default:
return check_if_done(); return check_if_done();
case match_result::no_match: { // Blocking actors can have fallback // Blocking actors can have fallback handlers for catch-all rules.
// handlers for catch-all rules.
auto sres = bhvr.fallback(self->current_element_->payload); auto sres = bhvr.fallback(self->current_element_->payload);
auto f = detail::make_overload( auto f = detail::make_overload(
[&](skip_t&) { [&](skip_t&) {
// Response handlers must get re-invoked with an error when // Response handlers must get re-invoked with an error when
// receiving an unexpected message. // receiving an unexpected message.
if (mid.is_response()) { if (mid.is_response()) {
auto err = make_error(sec::unexpected_response, auto err = make_error(sec::unexpected_response, std::move(x.payload));
std::move(x.payload)); mailbox_element tmp{std::move(x.sender), x.mid, std::move(x.stages),
mailbox_element tmp{std::move(x.sender), x.mid,
std::move(x.stages),
make_message(std::move(err))}; make_message(std::move(err))};
self->current_element_ = &tmp; self->current_element_ = &tmp;
bhvr.nested(tmp.content()); bhvr.nested(tmp.content());
...@@ -217,8 +213,6 @@ blocking_actor::mailbox_visitor::operator()(mailbox_element& x) { ...@@ -217,8 +213,6 @@ blocking_actor::mailbox_visitor::operator()(mailbox_element& x) {
return check_if_done(); return check_if_done();
}); });
return visit(f, sres); return visit(f, sres);
}
}
}; };
// Post-process the returned value from the function body. // Post-process the returned value from the function body.
auto result = body(); auto result = body();
......
...@@ -28,9 +28,8 @@ namespace { ...@@ -28,9 +28,8 @@ namespace {
class combinator final : public behavior_impl { class combinator final : public behavior_impl {
public: public:
match_result invoke(detail::invoke_result_visitor& f, message& xs) override { bool invoke(detail::invoke_result_visitor& f, message& xs) override {
auto x = first->invoke(f, xs); return first->invoke(f, xs) || second->invoke(f, xs);
return x == match_result::no_match ? second->invoke(f, xs) : x;
} }
void handle_timeout() override { void handle_timeout() override {
...@@ -76,27 +75,20 @@ behavior_impl::behavior_impl(timespan tout) : timeout_(tout) { ...@@ -76,27 +75,20 @@ behavior_impl::behavior_impl(timespan tout) : timeout_(tout) {
// nop // nop
} }
match_result behavior_impl::invoke_empty(detail::invoke_result_visitor& f) { bool behavior_impl::invoke_empty(detail::invoke_result_visitor& f) {
message xs; message xs;
return invoke(f, xs); return invoke(f, xs);
} }
optional<message> behavior_impl::invoke(message& xs) { optional<message> behavior_impl::invoke(message& xs) {
maybe_message_visitor f; maybe_message_visitor f;
// the following const-cast is safe, because invoke() is aware of if (invoke(f, xs))
// 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); return std::move(f.value);
return none;
} }
match_result behavior_impl::invoke(detail::invoke_result_visitor& f, bool behavior_impl::invoke(detail::invoke_result_visitor& f, message& xs) {
message& xs) {
if (!xs.empty())
return invoke(f, xs); return invoke(f, xs);
return invoke_empty(f);
} }
void behavior_impl::handle_timeout() { 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) { ...@@ -87,7 +87,7 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
unsetf(has_timeout_flag); unsetf(has_timeout_flag);
if (!bhvr_stack_.empty()) { if (!bhvr_stack_.empty()) {
auto& bhvr = bhvr_stack_.back(); auto& bhvr = bhvr_stack_.back();
if (bhvr(visitor, x.content()) == match_result::match) if (bhvr(visitor, x.content()))
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
auto sres = call_handler(default_handler_, this, x.payload); auto sres = call_handler(default_handler_, this, x.payload);
......
...@@ -696,7 +696,7 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -696,7 +696,7 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
unsetf(has_timeout_flag); unsetf(has_timeout_flag);
if (!bhvr_stack_.empty()) { if (!bhvr_stack_.empty()) {
auto& bhvr = bhvr_stack_.back(); auto& bhvr = bhvr_stack_.back();
if (bhvr(visitor, x.content()) == match_result::match) if (bhvr(visitor, x.content()))
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
auto sres = call_handler(default_handler_, this, x.payload); auto sres = call_handler(default_handler_, this, x.payload);
...@@ -1108,35 +1108,20 @@ scheduled_actor::handle_open_stream_msg(mailbox_element& x) { ...@@ -1108,35 +1108,20 @@ scheduled_actor::handle_open_stream_msg(mailbox_element& x) {
auto rp = make_response_promise(); auto rp = make_response_promise();
rp.deliver(sec::stream_init_failed); rp.deliver(sec::stream_init_failed);
}; };
// Utility for invoking the default handler. // Invoke behavior and dispatch on the result.
auto fallback = [&] { auto& bs = bhvr_stack();
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); auto sres = call_handler(default_handler_, this, x.payload);
if (holds_alternative<skip_t>(sres)) { if (holds_alternative<skip_t>(sres)) {
CAF_LOG_DEBUG("default handler skipped open_stream_msg:" << osm.msg); CAF_LOG_DEBUG("default handler skipped open_stream_msg:" << osm.msg);
return invoke_message_result::skipped; return invoke_message_result::skipped;
} else { } else {
CAF_LOG_DEBUG( CAF_LOG_DEBUG("default handler was called for open_stream_msg:" << osm.msg);
"default handler was called for open_stream_msg:" << osm.msg);
fail(sec::stream_init_failed, "dropped open_stream_msg (no match)"); fail(sec::stream_init_failed, "dropped open_stream_msg (no match)");
return invoke_message_result::dropped; 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
}
} }
actor_clock::time_point actor_clock::time_point
......
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