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
ac7dc7c8
Commit
ac7dc7c8
authored
Jun 17, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Deprecate `sb_actor`, close #265
parent
1403a0db
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
79 deletions
+57
-79
libcaf_core/caf/sb_actor.hpp
libcaf_core/caf/sb_actor.hpp
+4
-6
libcaf_core/test/dynamic_spawn.cpp
libcaf_core/test/dynamic_spawn.cpp
+36
-38
manual/ReceivingMessages.tex
manual/ReceivingMessages.tex
+17
-35
No files found.
libcaf_core/caf/sb_actor.hpp
View file @
ac7dc7c8
...
@@ -23,21 +23,18 @@
...
@@ -23,21 +23,18 @@
#include <utility>
#include <utility>
#include <type_traits>
#include <type_traits>
#include "caf/config.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/event_based_actor.hpp"
namespace
caf
{
namespace
caf
{
/// A base class for state-based actors using the
// <backward_compatibility version="0.13">
/// Curiously Recurring Template Pattern
/// to initialize the derived actor with its `init_state` member.
template
<
class
Derived
,
class
Base
=
event_based_actor
>
template
<
class
Derived
,
class
Base
=
event_based_actor
>
class
sb_actor
:
public
Base
{
class
sb_actor
:
public
Base
{
public:
public:
static_assert
(
std
::
is_base_of
<
event_based_actor
,
Base
>::
value
,
static_assert
(
std
::
is_base_of
<
event_based_actor
,
Base
>::
value
,
"Base must be event_based_actor or a derived type"
);
"Base must be event_based_actor or a derived type"
);
/// Overrides {@link event_based_actor::make_behavior()} and sets
/// the initial actor behavior to `Derived::init_state.
behavior
make_behavior
()
override
{
behavior
make_behavior
()
override
{
return
static_cast
<
Derived
*>
(
this
)
->
init_state
;
return
static_cast
<
Derived
*>
(
this
)
->
init_state
;
}
}
...
@@ -49,7 +46,8 @@ protected:
...
@@ -49,7 +46,8 @@ protected:
sb_actor
(
Ts
&&
...
xs
)
:
Base
(
std
::
forward
<
Ts
>
(
xs
)...)
{
sb_actor
(
Ts
&&
...
xs
)
:
Base
(
std
::
forward
<
Ts
>
(
xs
)...)
{
// nop
// nop
}
}
};
}
CAF_DEPRECATED
;
// </backward_compatibility>
}
// namespace caf
}
// namespace caf
...
...
libcaf_core/test/dynamic_spawn.cpp
View file @
ac7dc7c8
...
@@ -42,7 +42,6 @@ using c_atom = atom_constant<atom("c")>;
...
@@ -42,7 +42,6 @@ using c_atom = atom_constant<atom("c")>;
using
abc_atom
=
atom_constant
<
atom
(
"abc"
)
>
;
using
abc_atom
=
atom_constant
<
atom
(
"abc"
)
>
;
using
name_atom
=
atom_constant
<
atom
(
"name"
)
>
;
using
name_atom
=
atom_constant
<
atom
(
"name"
)
>
;
void
inc_actor_instances
()
{
void
inc_actor_instances
()
{
long
v1
=
++
s_actor_instances
;
long
v1
=
++
s_actor_instances
;
long
v2
=
s_max_actor_instances
.
load
();
long
v2
=
s_max_actor_instances
.
load
();
...
@@ -55,50 +54,49 @@ void dec_actor_instances() {
...
@@ -55,50 +54,49 @@ void dec_actor_instances() {
--
s_actor_instances
;
--
s_actor_instances
;
}
}
class
event_testee
:
public
sb_actor
<
event_testee
>
{
class
event_testee
:
public
event_based_actor
{
public:
public:
event_testee
();
event_testee
()
{
~
event_testee
();
inc_actor_instances
();
wait4string
.
assign
(
[
=
](
const
std
::
string
&
)
{
become
(
wait4int
);
},
[
=
](
get_atom
)
{
return
"wait4string"
;
}
);
wait4float
.
assign
(
[
=
](
float
)
{
become
(
wait4string
);
},
[
=
](
get_atom
)
{
return
"wait4float"
;
}
);
wait4int
.
assign
(
[
=
](
int
)
{
become
(
wait4float
);
},
[
=
](
get_atom
)
{
return
"wait4int"
;
}
);
}
~
event_testee
()
{
dec_actor_instances
();
}
behavior
make_behavior
()
override
{
return
wait4int
;
}
behavior
wait4string
;
behavior
wait4string
;
behavior
wait4float
;
behavior
wait4float
;
behavior
wait4int
;
behavior
wait4int
;
behavior
&
init_state
=
wait4int
;
};
};
event_testee
::
event_testee
()
{
inc_actor_instances
();
wait4string
.
assign
(
[
=
](
const
std
::
string
&
)
{
become
(
wait4int
);
},
[
=
](
get_atom
)
{
return
"wait4string"
;
}
);
wait4float
.
assign
(
[
=
](
float
)
{
become
(
wait4string
);
},
[
=
](
get_atom
)
{
return
"wait4float"
;
}
);
wait4int
.
assign
(
[
=
](
int
)
{
become
(
wait4float
);
},
[
=
](
get_atom
)
{
return
"wait4int"
;
}
);
}
event_testee
::~
event_testee
()
{
dec_actor_instances
();
}
// quits after 5 timeouts
// quits after 5 timeouts
actor
spawn_event_testee2
(
actor
parent
)
{
actor
spawn_event_testee2
(
actor
parent
)
{
struct
impl
:
event_based_actor
{
struct
impl
:
event_based_actor
{
...
...
manual/ReceivingMessages.tex
View file @
ac7dc7c8
...
@@ -22,62 +22,41 @@ class printer : public event_based_actor {
...
@@ -22,62 +22,41 @@ class printer : public event_based_actor {
}
;
}
;
\end{lstlisting}
\end{lstlisting}
Another way to implement class-based actors is provided by the class
\lstinline
^
sb
_
actor
^
(``State-Based Actor'').
This base class simply returns
\lstinline
^
init
_
state
^
(defined in the subclass) from its implementation for
\lstinline
^
make
_
behavior
^
.
\begin{lstlisting}
struct printer : sb
_
actor<printer>
{
behavior init
_
state
{
others >> []
{
cout << to
_
string(last
_
dequeued()) << endl;
}
}
;
}
;
\end{lstlisting}
Note that
\lstinline
^
sb
_
actor
^
uses the Curiously Recurring Template Pattern. Thus, the derived class must be given as template parameter.
This technique allows
\lstinline
^
sb
_
actor
^
to access the
\lstinline
^
init
_
state
^
member of a derived class.
The following example illustrates a more advanced state-based actor that implements a stack with a fixed maximum number of elements.
\clearpage
\clearpage
\begin{lstlisting}
\begin{lstlisting}
using pop
_
atom = atom
_
constant<atom("pop")>;
using pop
_
atom = atom
_
constant<atom("pop")>;
using push
_
atom = atom
_
constant<atom("push")>;
using push
_
atom = atom
_
constant<atom("push")>;
class fixed
_
stack : public
sb
_
actor<fixed
_
stack>
{
class fixed
_
stack : public
event
_
based
_
actor
{
public:
public:
fixed
_
stack(size
_
t max) : max
_
size(max)
{
fixed
_
stack(size
_
t max) : max
_
size(max)
{
full.assign(
full
_
.assign(
[=](push
_
atom, int)
{
[=](push
_
atom, int)
{
/
* discard */
/
/ discard
}
,
}
,
[=](pop
_
atom) -> message
{
[=](pop
_
atom) -> message
{
auto result = data.back();
auto result = data.back();
data.pop
_
back();
data.pop
_
back();
become(filled);
become(filled
_
);
return make
_
message(ok
_
atom::value, result);
return make
_
message(ok
_
atom::value, result);
}
}
);
);
filled.assign(
filled
_
.assign(
[=](push
_
atom, int what)
{
[=](push
_
atom, int what)
{
data.push
_
back(what);
data.push
_
back(what);
if (data.size() == max
_
size)
{
if (data.size() == max
_
size) become(full
_
);
become(full);
}
}
,
}
,
[=](pop
_
atom) -> message
{
[=](pop
_
atom) -> message
{
auto result = data.back();
auto result = data.back();
data.pop
_
back();
data.pop
_
back();
if (data.empty())
{
if (data.empty()) become(empty
_
);
become(empty);
}
return make
_
message(ok
_
atom::value, result);
return make
_
message(ok
_
atom::value, result);
}
}
);
);
empty.assign(
empty
_
.assign(
[=](push
_
atom, int what)
{
[=](push
_
atom, int what)
{
data.push
_
back(what);
data.push
_
back(what);
become(filled);
become(filled
_
);
}
,
}
,
[=](pop
_
atom)
{
[=](pop
_
atom)
{
return error
_
atom::value;
return error
_
atom::value;
...
@@ -85,12 +64,15 @@ class fixed_stack : public sb_actor<fixed_stack> {
...
@@ -85,12 +64,15 @@ class fixed_stack : public sb_actor<fixed_stack> {
);
);
}
}
behavior make
_
behavior() override
{
return empty
_
;
}
size
_
t max
_
size;
size
_
t max
_
size;
std::vector<int> data;
std::vector<int> data;
behavior full;
behavior full
_
;
behavior filled;
behavior filled
_
;
behavior empty;
behavior empty
_
;
behavior
&
init
_
state = empty;
}
;
}
;
\end{lstlisting}
\end{lstlisting}
...
...
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