Commit 8d753c01 authored by Dominik Charousset's avatar Dominik Charousset

Update manual: {others() => others}

parent 9d7204e5
...@@ -60,7 +60,7 @@ receive_for(i, 10) ( ...@@ -60,7 +60,7 @@ receive_for(i, 10) (
size_t received = 0; size_t received = 0;
do { do {
receive ( receive (
others() >> [&]() { others >> [&]() {
++received; ++received;
} }
); );
...@@ -69,7 +69,7 @@ do { ...@@ -69,7 +69,7 @@ do {
\begin{lstlisting} \begin{lstlisting}
size_t received = 0; size_t received = 0;
do_receive ( do_receive (
others() >> [&]() { others >> [&]() {
++received; ++received;
} }
).until([&] { return received >= 10; }); ).until([&] { return received >= 10; });
......
...@@ -27,14 +27,14 @@ In our first example, \lstinline^bhvr1^ models a pattern accepting messages that ...@@ -27,14 +27,14 @@ In our first example, \lstinline^bhvr1^ models a pattern accepting messages that
Any other message is not matched and will remain in the mailbox until it is consumed eventually. Any other message is not matched and will remain in the mailbox until it is consumed eventually.
This caching mechanism allows actors to ignore messages until a state change replaces its message handler. This caching mechanism allows actors to ignore messages until a state change replaces its message handler.
However, this can lead to a memory leak if an actor receives messages it handles in no state. However, this can lead to a memory leak if an actor receives messages it handles in no state.
To allow actors to specify a default message handlers for otherwise unmatched messages, \lib provides the function \lstinline^others()^. To allow actors to specify a default message handlers for otherwise unmatched messages, \lib provides \lstinline^others^.
\begin{lstlisting} \begin{lstlisting}
behavior bhvr2{ behavior bhvr2{
[](int i) { /*...*/ }, [](int i) { /*...*/ },
[](int i, float f) { /*...*/ }, [](int i, float f) { /*...*/ },
[](int a, int b, int c) { /*...*/ }, [](int a, int b, int c) { /*...*/ },
others() >> [] { /*...*/ } others >> [] { /*...*/ }
}; };
\end{lstlisting} \end{lstlisting}
...@@ -88,15 +88,13 @@ Atom constants define a static member \lstinline^value^ that can be used on the ...@@ -88,15 +88,13 @@ Atom constants define a static member \lstinline^value^ that can be used on the
Please note that the static \lstinline^value^ member does \emph{not} have the type \lstinline^atom_value^, unlike \lstinline^std::integral_constant^ for example. Please note that the static \lstinline^value^ member does \emph{not} have the type \lstinline^atom_value^, unlike \lstinline^std::integral_constant^ for example.
\clearpage \clearpage
\subsection{Match Expressions} \subsection{Advanced Match Cases}
Match expressions are an advanced feature of \lib and allow you to match on values and to extract data while matching. Match cases are an advanced feature of \lib and allow you to match on values and to transform data while matching.
Using lambda expressions and atom constants---cf. \ref{Sec::PatternMatching::Atoms}---suffices for most use cases. A match case begins with a call to the function \lstinline^on^, which returns an intermediate object providing \lstinline^operator>>^.
A match expression begins with a call to the function \lstinline^on^, which returns an intermediate object providing \lstinline^operator>>^.
The function \lstinline^others()^ is an alias for \lstinline^on<anything>()^.
The right-hand side of the operator denotes a callback, usually a lambda expression, that should be invoked if a tuple matches the types given to \lstinline^on^, The right-hand side of the operator denotes a callback, usually a lambda expression, that should be invoked if a tuple matches the types given to \lstinline^on^,
When using the basic syntax, \lib generates the match expression automatically. When using the basic syntax, \lib generates the match case automatically.
A verbose version of the \lstinline^bhvr1^ from \ref{Sec::PatternMatching::Basics} is shown below. A verbose version of the \lstinline^bhvr1^ from \ref{Sec::PatternMatching::Basics} is shown below.
\begin{lstlisting} \begin{lstlisting}
...@@ -107,6 +105,7 @@ behavior verbose_bhvr1{ ...@@ -107,6 +105,7 @@ behavior verbose_bhvr1{
}; };
\end{lstlisting} \end{lstlisting}
It is worth mentioning that passing the lambdas directly is more efficient, since it allows \lib to select a special-purpose implementation.
The function \lstinline^on^ can be used in two ways. The function \lstinline^on^ can be used in two ways.
Either with template parameters only or with function parameters only. Either with template parameters only or with function parameters only.
The latter version deduces all types from its arguments and matches for both type and value. The latter version deduces all types from its arguments and matches for both type and value.
...@@ -143,7 +142,7 @@ on(arg_match) >> [](int a, int b) { /*...*/ } ...@@ -143,7 +142,7 @@ on(arg_match) >> [](int a, int b) { /*...*/ }
\end{lstlisting} \end{lstlisting}
Note that \lstinline^arg_match^ must be passed as last parameter. Note that \lstinline^arg_match^ must be passed as last parameter.
If all types should be deduced from the callback signature, \lstinline^on_arg_match^ can be used, which is an alias for \lstinline^on(arg_match)^. If all types should be deduced from the callback signature, \lstinline^on_arg_match^ can be used, which is a faster alternative for \lstinline^on(arg_match)^.
However, \lstinline^on_arg_match^ is used implicitly whenever a callback is used without preceding match expression. However, \lstinline^on_arg_match^ is used implicitly whenever a callback is used without preceding match expression.
\clearpage \clearpage
...@@ -152,7 +151,7 @@ However, \lstinline^on_arg_match^ is used implicitly whenever a callback is used ...@@ -152,7 +151,7 @@ However, \lstinline^on_arg_match^ is used implicitly whenever a callback is used
The type \lstinline^anything^ can be used as wildcard to match any number of any types. The type \lstinline^anything^ can be used as wildcard to match any number of any types.
%The \lstinline^constexpr^ value \lstinline^any_vals^ can be used as function argument if \lstinline^on^ is used without template paremeters. %The \lstinline^constexpr^ value \lstinline^any_vals^ can be used as function argument if \lstinline^on^ is used without template paremeters.
A pattern created by \lstinline^on<anything>()^ or its alias \lstinline^others()^ is useful to define a default case. A pattern created by \lstinline^on<anything>()^ or its alias \lstinline^others^ is useful to define a default case.
For patterns defined without template parameters, the \lstinline^constexpr^ value \lstinline^any_vals^ can be used as function argument. For patterns defined without template parameters, the \lstinline^constexpr^ value \lstinline^any_vals^ can be used as function argument.
The constant \lstinline^any_vals^ is of type \lstinline^anything^ and is nothing but syntactic sugar for defining patterns. The constant \lstinline^any_vals^ is of type \lstinline^anything^ and is nothing but syntactic sugar for defining patterns.
...@@ -164,18 +163,18 @@ on(any_vals, arg_match) >> [](int i) { ...@@ -164,18 +163,18 @@ on(any_vals, arg_match) >> [](int i) {
// tuple with int as last element // tuple with int as last element
// "on(any_vals, arg_match)" is equal to "on(anything{}, arg_match)" // "on(any_vals, arg_match)" is equal to "on(anything{}, arg_match)"
}, },
others() >> [] { others >> [] {
// everything else (default handler) // everything else (default handler)
// "others()" is equal to "on<anything>()" and "on(any_vals)" // "others" is equal to "on<anything>()" and "on(any_vals)"
} }
\end{lstlisting} \end{lstlisting}
\subsection{Projections and Extractors} \subsection{Projections}
Projections perform type conversions or extract data from a given input. Projections perform type conversions or extract data from a given input.
If a callback expects an integer but the received message contains a string, a projection can be used to perform a type conversion on-the-fly. If a callback expects an integer but the received message contains a string, a projection can be used to perform a type conversion on-the-fly.
This conversion should be free of side-effects and, in particular, shall not throw exceptions, because a failed projection is not an error. This conversion must not have side-effects and must not throw exceptions.
A pattern simply does not match if a projection failed. A failed projection is not an error, it simply indicates that a pattern is not matched.
Let us have a look at a simple example. Let us have a look at a simple example.
\begin{lstlisting} \begin{lstlisting}
......
...@@ -14,7 +14,7 @@ A class-based actor is a subtype of \lstinline^event_based_actor^ and must imple ...@@ -14,7 +14,7 @@ A class-based actor is a subtype of \lstinline^event_based_actor^ and must imple
class printer : public event_based_actor { class printer : public event_based_actor {
behavior make_behavior() override { behavior make_behavior() override {
return { return {
others() >> [] { others >> [] {
cout << to_string(last_dequeued()) << endl; cout << to_string(last_dequeued()) << endl;
} }
}; };
...@@ -28,7 +28,7 @@ This base class simply returns \lstinline^init_state^ (defined in the subclass) ...@@ -28,7 +28,7 @@ This base class simply returns \lstinline^init_state^ (defined in the subclass)
\begin{lstlisting} \begin{lstlisting}
struct printer : sb_actor<printer> { struct printer : sb_actor<printer> {
behavior init_state { behavior init_state {
others() >> [] { others >> [] {
cout << to_string(last_dequeued()) << endl; cout << to_string(last_dequeued()) << endl;
} }
}; };
...@@ -143,7 +143,7 @@ behavior eager_actor(event_based_actor* self) { ...@@ -143,7 +143,7 @@ behavior eager_actor(event_based_actor* self) {
return { return {
[](int i) { /* ... */ }, [](int i) { /* ... */ },
[](float i) { /* ... */ }, [](float i) { /* ... */ },
others() >> [] { /* ... */ }, others >> [] { /* ... */ },
after(std::chrono::seconds(10)) >> [] { after(std::chrono::seconds(10)) >> [] {
aout(self) << "received nothing within 10 seconds..." << endl; aout(self) << "received nothing within 10 seconds..." << endl;
// ... // ...
...@@ -166,7 +166,7 @@ Thus, you should not depend on a certain clock resolution. ...@@ -166,7 +166,7 @@ Thus, you should not depend on a certain clock resolution.
Unmatched messages are skipped automatically by \lib's runtime system. Unmatched messages are skipped automatically by \lib's runtime system.
This is true for \textit{all} actor implementations. This is true for \textit{all} actor implementations.
To allow actors to skip messages manually, \lstinline^skip_message^ can be used. To allow actors to skip messages manually, \lstinline^skip_message^ can be used.
This is in particular useful whenever an actor switches between behaviors, but wants to use a default rule created by \lstinline^others()^ to catch messages that are not handled by any of its behaviors. This is in particular useful whenever an actor switches between behaviors, but wants to use a default rule created by \lstinline^others^ to catch messages that are not handled by any of its behaviors.
The following example illustrates a simple server actor that dispatches requests to workers. The following example illustrates a simple server actor that dispatches requests to workers.
After receiving an \lstinline^'idle'^ message, it awaits a request that is then forwarded to the idle worker. After receiving an \lstinline^'idle'^ message, it awaits a request that is then forwarded to the idle worker.
...@@ -193,13 +193,13 @@ behavior server(event_based_actor* self) { ...@@ -193,13 +193,13 @@ behavior server(event_based_actor* self) {
[=](idle_atom) { [=](idle_atom) {
return skip_message(); return skip_message();
}, },
others() >> die others >> die
); );
}, },
[=](request_atom) { [=](request_atom) {
return skip_message(); return skip_message();
}, },
others() >> die others >> die
}; };
} }
\end{lstlisting} \end{lstlisting}
No preview for this file type
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