Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
90fcde67
Commit
90fcde67
authored
Jan 05, 2022
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expand flow API examples
parent
36a7a16f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
8 deletions
+98
-8
examples/CMakeLists.txt
examples/CMakeLists.txt
+2
-0
examples/flow/from-callable.cpp
examples/flow/from-callable.cpp
+39
-0
examples/flow/observe-on.cpp
examples/flow/observe-on.cpp
+50
-0
examples/flow/spsc-buffer-resource.cpp
examples/flow/spsc-buffer-resource.cpp
+7
-8
No files found.
examples/CMakeLists.txt
View file @
90fcde67
...
...
@@ -33,6 +33,8 @@ add_core_example(message_passing request)
add_core_example
(
message_passing typed_calculator
)
# flow API
add_core_example
(
flow from-callable
)
add_core_example
(
flow observe-on
)
add_core_example
(
flow spsc-buffer-resource
)
# dynamic behavior changes using 'become'
...
...
examples/flow/from-callable.cpp
0 → 100644
View file @
90fcde67
// Non-interactive example to showcase `from_callable`.
#include "caf/actor_system.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/scheduled_actor/flow.hpp"
#include <iostream>
namespace
{
struct
config
:
caf
::
actor_system_config
{
config
()
{
opt_group
{
custom_options_
,
"global"
}
//
.
add
(
n
,
"num-values,n"
,
"number of values produced by the source"
);
}
size_t
n
=
10
;
};
// --(rst-from-callable-begin)--
void
caf_main
(
caf
::
actor_system
&
sys
,
const
config
&
cfg
)
{
sys
.
spawn
([
n
=
cfg
.
n
](
caf
::
event_based_actor
*
self
)
{
self
// Get an observable factory.
->
make_observable
()
// Produce an integer sequence starting at 1, i.e., 1, 2, 3, ...
.
from_callable
([
i
=
0
]()
mutable
{
return
++
i
;
})
// Only take the requested number of items from the infinite sequence.
.
take
(
n
)
// Print each integer.
.
for_each
([](
int
x
)
{
std
::
cout
<<
x
<<
'\n'
;
});
});
}
// --(rst-from-callable-end)--
}
// namespace
CAF_MAIN
()
examples/flow/observe-on.cpp
0 → 100644
View file @
90fcde67
// Non-interactive example to showcase `observe_on`.
#include "caf/actor_system.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/scheduled_actor/flow.hpp"
#include <iostream>
namespace
{
struct
config
:
caf
::
actor_system_config
{
config
()
{
opt_group
{
custom_options_
,
"global"
}
//
.
add
(
n
,
"num-values,n"
,
"number of values produced by the source"
);
}
size_t
n
=
10
;
};
// --(rst-from-callable-begin)--
void
caf_main
(
caf
::
actor_system
&
sys
,
const
config
&
cfg
)
{
// Create two actors without actually running them yet.
using
actor_t
=
caf
::
event_based_actor
;
auto
[
src
,
launch_src
]
=
sys
.
spawn_inactive
<
actor_t
>
();
auto
[
snk
,
launch_snk
]
=
sys
.
spawn_inactive
<
actor_t
>
();
// Define our data flow: generate data on `src` and print it on `snk`.
src
// Get an observable factory.
->
make_observable
()
// Produce an integer sequence starting at 1, i.e., 1, 2, 3, ...
.
from_callable
([
i
=
0
]()
mutable
{
return
++
i
;
})
// Only take the requested number of items from the infinite sequence.
.
take
(
cfg
.
n
)
// Switch to `snk` for further processing.
.
observe_on
(
snk
)
// Print each integer.
.
for_each
([](
int
x
)
{
std
::
cout
<<
x
<<
'\n'
;
});
// Allow the actors to run. After this point, we may no longer dereference
// the `src` and `snk` pointers! Calling these manually is optional. When
// removing these two lines, CAF automatically launches the actors at scope
// exit.
launch_src
();
launch_snk
();
}
// --(rst-from-callable-end)--
}
// namespace
CAF_MAIN
()
examples/flow/spsc-buffer-resource.cpp
View file @
90fcde67
// Non-interactive example to illustrate how to connect flows over an
SPSC
// (Single Producer Single Consumer) buffer.
// Non-interactive example to illustrate how to connect flows over an
//
asynchronous SPSC
(Single Producer Single Consumer) buffer.
#include "caf/actor_system.hpp"
#include "caf/async/spsc_buffer.hpp"
...
...
@@ -13,8 +13,8 @@ namespace {
// --(rst-source-begin)--
// Simple source for generating a stream of integers from 1 to n.
void
int_
source
(
caf
::
event_based_actor
*
self
,
caf
::
async
::
producer_resource
<
int
>
out
,
size_t
n
)
{
void
source
(
caf
::
event_based_actor
*
self
,
caf
::
async
::
producer_resource
<
int
>
out
,
size_t
n
)
{
self
// Get an observable factory.
->
make_observable
()
...
...
@@ -29,8 +29,7 @@ void int_source(caf::event_based_actor* self,
// --(rst-sink-begin)--
// Simple sink for consuming a stream of integers, printing it to stdout.
void
int_sink
(
caf
::
event_based_actor
*
self
,
caf
::
async
::
consumer_resource
<
int
>
in
)
{
void
sink
(
caf
::
event_based_actor
*
self
,
caf
::
async
::
consumer_resource
<
int
>
in
)
{
self
// Get an observable factory.
->
make_observable
()
...
...
@@ -53,8 +52,8 @@ struct config : caf::actor_system_config {
// --(rst-main-begin)--
void
caf_main
(
caf
::
actor_system
&
sys
,
const
config
&
cfg
)
{
auto
[
snk_res
,
src_res
]
=
caf
::
async
::
make_spsc_buffer_resource
<
int
>
();
sys
.
spawn
(
int_
sink
,
std
::
move
(
snk_res
));
sys
.
spawn
(
int_
source
,
std
::
move
(
src_res
),
cfg
.
n
);
sys
.
spawn
(
sink
,
std
::
move
(
snk_res
));
sys
.
spawn
(
source
,
std
::
move
(
src_res
),
cfg
.
n
);
}
// --(rst-main-end)--
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment