Commit 6a47f60d authored by Dominik Charousset's avatar Dominik Charousset

Add attach_stream_sink to replace make_sink

parent f78f6c9c
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Basic, non-interactive streaming example for processing integers. * * Basic, non-interactive streaming example for processing integers. *
******************************************************************************/ ******************************************************************************/
// Manual refs: lines 17-46, 48-78, 80-106, 120-124 (Streaming) // Manual refs: lines 17-46, 48-78, 80-107, 121-125 (Streaming)
#include <iostream> #include <iostream>
#include <vector> #include <vector>
...@@ -81,7 +81,8 @@ behavior int_sink(event_based_actor* self) { ...@@ -81,7 +81,8 @@ behavior int_sink(event_based_actor* self) {
return {[=](stream<int> in) { return {[=](stream<int> in) {
// Create a stream manager for implementing a stream sink. Once more, we // Create a stream manager for implementing a stream sink. Once more, we
// have to provide three functions: Initializer, Consumer, Finalizer. // have to provide three functions: Initializer, Consumer, Finalizer.
return self->make_sink( return attach_stream_sink(
self,
// Our input source. // Our input source.
in, in,
// Initializer. Here, we store all values we receive. Note that streams // Initializer. Here, we store all values we receive. Note that streams
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "caf/after.hpp" #include "caf/after.hpp"
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include "caf/attach_continuous_stream_source.hpp" #include "caf/attach_continuous_stream_source.hpp"
#include "caf/attach_stream_sink.hpp"
#include "caf/attach_stream_source.hpp" #include "caf/attach_stream_source.hpp"
#include "caf/attach_stream_stage.hpp" #include "caf/attach_stream_stage.hpp"
#include "caf/attachable.hpp" #include "caf/attachable.hpp"
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 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 "caf/default_downstream_manager.hpp"
#include "caf/detail/stream_sink_driver_impl.hpp"
#include "caf/detail/stream_sink_impl.hpp"
#include "caf/downstream_manager.hpp"
#include "caf/fwd.hpp"
#include "caf/make_sink_result.hpp"
#include "caf/policy/arg.hpp"
#include "caf/stream.hpp"
#include "caf/stream_source.hpp"
namespace caf {
/// Attaches a new stream sink to `self` by creating a default stream sink /
/// manager from given callbacks.
/// @param self Points to the hosting actor.
/// @param xs Additional constructor arguments for `Driver`.
/// @returns The new `stream_manager`, an inbound slot, and an outbound slot.
template <class Driver, class... Ts>
make_sink_result<typename Driver::input_type>
attach_stream_sink(scheduled_actor* self,
stream<typename Driver::input_type> in, Ts&&... xs) {
auto mgr = detail::make_stream_sink<Driver>(self, std::forward<Ts>(xs)...);
auto slot = mgr->add_inbound_path(in);
return {slot, std::move(mgr)};
}
/// Attaches a new stream sink to `self` by creating a default stream sink
/// manager from given callbacks.
/// @param self Points to the hosting actor.
/// @param in Stream handshake from upstream path.
/// @param init Function object for initializing the state of the sink.
/// @param fun Processing function.
/// @param fin Optional cleanup handler.
/// @returns The new `stream_manager` and the inbound slot.
template <class In, class Init, class Fun, class Finalize = unit_t,
class Trait = stream_sink_trait_t<Fun>>
make_sink_result<In> attach_stream_sink(scheduled_actor* self, stream<In> in,
Init init, Fun fun, Finalize fin = {}) {
using driver = detail::stream_sink_driver_impl<In, Fun, Finalize>;
return attach_stream_sink<driver>(self, in, std::move(init), std::move(fun),
std::move(fin));
}
} // namespace caf
...@@ -521,6 +521,7 @@ public: ...@@ -521,6 +521,7 @@ public:
std::move(done), std::move(fin)); std::move(done), std::move(fin));
} }
/// @deprecated Please use `attach_stream_sink` instead.
template <class Driver, class... Ts> template <class Driver, class... Ts>
make_sink_result<typename Driver::input_type> make_sink_result<typename Driver::input_type>
make_sink(const stream<typename Driver::input_type>& src, Ts&&... xs) { make_sink(const stream<typename Driver::input_type>& src, Ts&&... xs) {
...@@ -529,6 +530,7 @@ public: ...@@ -529,6 +530,7 @@ public:
return {slot, std::move(mgr)}; return {slot, std::move(mgr)};
} }
/// @deprecated Please use `attach_stream_sink` instead.
template <class In, class Init, class Fun, class Finalize = unit_t, template <class In, class Init, class Fun, class Finalize = unit_t,
class Trait = stream_sink_trait_t<Fun>> class Trait = stream_sink_trait_t<Fun>>
make_sink_result<In> make_sink(const stream<In>& in, Init init, Fun fun, make_sink_result<In> make_sink(const stream<In>& in, Init init, Fun fun,
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "caf/actor_system.hpp" #include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp" #include "caf/actor_system_config.hpp"
#include "caf/attach_stream_sink.hpp"
#include "caf/event_based_actor.hpp" #include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp" #include "caf/stateful_actor.hpp"
...@@ -46,8 +47,7 @@ TESTEE_STATE(file_reader) { ...@@ -46,8 +47,7 @@ TESTEE_STATE(file_reader) {
}; };
VARARGS_TESTEE(file_reader, size_t buf_size) { VARARGS_TESTEE(file_reader, size_t buf_size) {
return { return {[=](string& fname) -> output_stream<int, string> {
[=](string& fname) -> output_stream<int, string> {
CAF_CHECK_EQUAL(fname, "numbers.txt"); CAF_CHECK_EQUAL(fname, "numbers.txt");
CAF_CHECK_EQUAL(self->mailbox().empty(), true); CAF_CHECK_EQUAL(self->mailbox().empty(), true);
return attach_stream_source( return attach_stream_source(
...@@ -77,8 +77,7 @@ VARARGS_TESTEE(file_reader, size_t buf_size) { ...@@ -77,8 +77,7 @@ VARARGS_TESTEE(file_reader, size_t buf_size) {
} }
return false; return false;
}); });
} }};
};
} }
TESTEE_STATE(sum_up) { TESTEE_STATE(sum_up) {
...@@ -86,32 +85,26 @@ TESTEE_STATE(sum_up) { ...@@ -86,32 +85,26 @@ TESTEE_STATE(sum_up) {
}; };
TESTEE(sum_up) { TESTEE(sum_up) {
return { return {[=](stream<int>& in, const string& fname) {
[=](stream<int>& in, const string& fname) {
CAF_CHECK_EQUAL(fname, "numbers.txt"); CAF_CHECK_EQUAL(fname, "numbers.txt");
using int_ptr = int*; using int_ptr = int*;
return self->make_sink( return attach_stream_sink(
self,
// input stream // input stream
in, in,
// initialize state // initialize state
[=](int_ptr& x) { [=](int_ptr& x) { x = &self->state.x; },
x = &self->state.x;
},
// processing step // processing step
[](int_ptr& x , int y) { [](int_ptr& x, int y) { *x += y; },
*x += y;
},
// cleanup // cleanup
[=](int_ptr&, const error&) { [=](int_ptr&, const error&) {
CAF_MESSAGE(self->name() << " is done"); CAF_MESSAGE(self->name() << " is done");
} });
);
}, },
[=](join_atom atm, actor src) { [=](join_atom atm, actor src) {
CAF_MESSAGE(self->name() << " joins a stream"); CAF_MESSAGE(self->name() << " joins a stream");
self->send(self * src, atm); self->send(self * src, atm);
} }};
};
} }
TESTEE_STATE(stream_multiplexer) { TESTEE_STATE(stream_multiplexer) {
...@@ -125,14 +118,9 @@ TESTEE(stream_multiplexer) { ...@@ -125,14 +118,9 @@ TESTEE(stream_multiplexer) {
// nop // nop
}, },
// processing step // processing step
[](unit_t&, downstream<int>& out, int x) { [](unit_t&, downstream<int>& out, int x) { out.push(x); },
out.push(x);
},
// cleanup // cleanup
[=](unit_t&, const error&) { [=](unit_t&, const error&) { CAF_MESSAGE(self->name() << " is done"); });
CAF_MESSAGE(self->name() << " is done");
}
);
return { return {
[=](join_atom) { [=](join_atom) {
CAF_MESSAGE("received 'join' request"); CAF_MESSAGE("received 'join' request");
...@@ -152,7 +140,7 @@ TESTEE(stream_multiplexer) { ...@@ -152,7 +140,7 @@ TESTEE(stream_multiplexer) {
using fixture = test_coordinator_fixture<>; using fixture = test_coordinator_fixture<>;
} // namespace <anonymous> } // namespace
// -- unit tests --------------------------------------------------------------- // -- unit tests ---------------------------------------------------------------
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "caf/actor_system.hpp" #include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp" #include "caf/actor_system_config.hpp"
#include "caf/attach_stream_sink.hpp"
#include "caf/event_based_actor.hpp" #include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp" #include "caf/stateful_actor.hpp"
...@@ -98,7 +99,8 @@ TESTEE_STATE(sum_up) { ...@@ -98,7 +99,8 @@ TESTEE_STATE(sum_up) {
TESTEE(sum_up) { TESTEE(sum_up) {
using intptr = int*; using intptr = int*;
return {[=](stream<int32_t>& in) { return {[=](stream<int32_t>& in) {
return self->make_sink( return attach_stream_sink(
self,
// input stream // input stream
in, in,
// initialize state // initialize state
...@@ -122,7 +124,8 @@ TESTEE_STATE(collect) { ...@@ -122,7 +124,8 @@ TESTEE_STATE(collect) {
TESTEE(collect) { TESTEE(collect) {
return {[=](stream<string>& in) { return {[=](stream<string>& in) {
return self->make_sink( return attach_stream_sink(
self,
// input stream // input stream
in, in,
// initialize state // initialize state
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "caf/actor_system.hpp" #include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp" #include "caf/actor_system_config.hpp"
#include "caf/attach_stream_sink.hpp"
#include "caf/attach_stream_stage.hpp" #include "caf/attach_stream_stage.hpp"
#include "caf/event_based_actor.hpp" #include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp" #include "caf/stateful_actor.hpp"
...@@ -111,7 +112,8 @@ TESTEE_STATE(sum_up) { ...@@ -111,7 +112,8 @@ TESTEE_STATE(sum_up) {
TESTEE(sum_up) { TESTEE(sum_up) {
using intptr = int*; using intptr = int*;
return {[=](stream<int>& in) { return {[=](stream<int>& in) {
return self->make_sink( return attach_stream_sink(
self,
// input stream // input stream
in, in,
// initialize state // initialize state
...@@ -131,7 +133,8 @@ TESTEE(delayed_sum_up) { ...@@ -131,7 +133,8 @@ TESTEE(delayed_sum_up) {
return {[=](ok_atom) { return {[=](ok_atom) {
self->become([=](stream<int>& in) { self->become([=](stream<int>& in) {
self->set_default_handler(print_and_drop); self->set_default_handler(print_and_drop);
return self->make_sink( return attach_stream_sink(
self,
// input stream // input stream
in, in,
// initialize state // initialize state
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "caf/actor_system.hpp" #include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp" #include "caf/actor_system_config.hpp"
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include "caf/attach_stream_sink.hpp"
#include "caf/attach_stream_source.hpp" #include "caf/attach_stream_source.hpp"
#include "caf/broadcast_downstream_manager.hpp" #include "caf/broadcast_downstream_manager.hpp"
#include "caf/event_based_actor.hpp" #include "caf/event_based_actor.hpp"
...@@ -136,7 +137,8 @@ TESTEE_STATE(log_consumer) { ...@@ -136,7 +137,8 @@ TESTEE_STATE(log_consumer) {
TESTEE(log_consumer) { TESTEE(log_consumer) {
return {[=](stream<value_type>& in) { return {[=](stream<value_type>& in) {
return self->make_sink( return attach_stream_sink(
self,
// input stream // input stream
in, in,
// initialize state // initialize state
......
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