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
366c771a
Commit
366c771a
authored
May 02, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve calculator example
parent
6430464e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
19 deletions
+47
-19
examples/message_passing/calculator.cpp
examples/message_passing/calculator.cpp
+47
-19
No files found.
examples/message_passing/calculator.cpp
View file @
366c771a
...
@@ -10,20 +10,26 @@
...
@@ -10,20 +10,26 @@
#include "caf/all.hpp"
#include "caf/all.hpp"
using
std
::
cout
;
using
std
::
cout
;
using
std
::
cerr
;
using
std
::
endl
;
using
std
::
endl
;
using
namespace
caf
;
using
namespace
caf
;
using
plus_atom
=
atom_constant
<
atom
(
"plus"
)
>
;
using
plus_atom
=
atom_constant
<
atom
(
"plus"
)
>
;
using
minus_atom
=
atom_constant
<
atom
(
"minus"
)
>
;
using
minus_atom
=
atom_constant
<
atom
(
"minus"
)
>
;
using
result_atom
=
atom_constant
<
atom
(
"result"
)
>
;
using
calculator_actor
=
typed_actor
<
replies_to
<
plus_atom
,
int
,
int
>::
with
<
result_atom
,
int
>
,
replies_to
<
minus_atom
,
int
,
int
>::
with
<
result_atom
,
int
>>
;
// implementation using the blocking API
// implementation using the blocking API
void
blocking_calculator
(
blocking_actor
*
self
)
{
void
blocking_calculator
(
blocking_actor
*
self
)
{
self
->
receive_loop
(
self
->
receive_loop
(
[](
plus_atom
,
int
a
,
int
b
)
{
[](
plus_atom
,
int
a
,
int
b
)
{
return
a
+
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
a
+
b
)
;
},
},
[](
minus_atom
,
int
a
,
int
b
)
{
[](
minus_atom
,
int
a
,
int
b
)
{
return
a
-
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
a
-
b
)
;
},
},
others
>>
[
=
]
{
others
>>
[
=
]
{
cout
<<
"unexpected: "
<<
to_string
(
self
->
current_message
())
<<
endl
;
cout
<<
"unexpected: "
<<
to_string
(
self
->
current_message
())
<<
endl
;
...
@@ -31,22 +37,36 @@ void blocking_calculator(blocking_actor* self) {
...
@@ -31,22 +37,36 @@ void blocking_calculator(blocking_actor* self) {
);
);
}
}
//
execute this behavior until actor terminates
//
implementation using the event-based API
behavior
calculator
(
event_based_actor
*
self
)
{
behavior
calculator
(
event_based_actor
*
self
)
{
return
behavior
{
return
behavior
{
[](
plus_atom
,
int
a
,
int
b
)
{
[](
plus_atom
,
int
a
,
int
b
)
{
return
a
+
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
a
+
b
)
;
},
},
[](
minus_atom
,
int
a
,
int
b
)
{
[](
minus_atom
,
int
a
,
int
b
)
{
return
a
-
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
a
-
b
)
;
},
},
others
>>
[
=
]
{
others
>>
[
=
]
{
c
out
<<
"unexpected: "
<<
to_string
(
self
->
current_message
())
<<
endl
;
c
err
<<
"unexpected: "
<<
to_string
(
self
->
current_message
())
<<
endl
;
}
}
};
};
}
}
void
tester
(
event_based_actor
*
self
,
const
actor
&
testee
)
{
// implementation using the statically typed API
calculator_actor
::
behavior_type
typed_calculator
()
{
return
{
[](
plus_atom
,
int
a
,
int
b
)
{
return
std
::
make_tuple
(
result_atom
::
value
,
a
+
b
);
},
[](
minus_atom
,
int
a
,
int
b
)
{
return
std
::
make_tuple
(
result_atom
::
value
,
a
-
b
);
}
};
}
// tests a calculator instance
template
<
class
Handle
>
void
tester
(
event_based_actor
*
self
,
const
Handle
&
testee
,
int
x
,
int
y
)
{
self
->
link_to
(
testee
);
self
->
link_to
(
testee
);
// will be invoked if we receive an unexpected response message
// will be invoked if we receive an unexpected response message
self
->
on_sync_failure
([
=
]
{
self
->
on_sync_failure
([
=
]
{
...
@@ -54,13 +74,13 @@ void tester(event_based_actor* self, const actor& testee) {
...
@@ -54,13 +74,13 @@ void tester(event_based_actor* self, const actor& testee) {
self
->
quit
(
exit_reason
::
user_shutdown
);
self
->
quit
(
exit_reason
::
user_shutdown
);
});
});
// first test: 2 + 1 = 3
// first test: 2 + 1 = 3
self
->
sync_send
(
testee
,
plus_atom
::
value
,
2
,
1
).
then
(
self
->
sync_send
(
testee
,
plus_atom
::
value
,
x
,
y
).
then
(
on
(
3
)
>>
[
=
]
{
[
=
](
result_atom
,
int
res1
)
{
// second test: 2 - 1 = 1
aout
(
self
)
<<
x
<<
" + "
<<
y
<<
" = "
<<
res1
<<
endl
;
self
->
sync_send
(
testee
,
minus_atom
::
value
,
2
,
1
).
then
(
self
->
sync_send
(
testee
,
minus_atom
::
value
,
x
,
y
).
then
(
on
(
1
)
>>
[
=
]
{
[
=
](
result_atom
,
int
res2
)
{
// both tests succeeded
// both tests succeeded
aout
(
self
)
<<
"AUT (actor under test) seems to be ok"
<<
endl
;
aout
(
self
)
<<
x
<<
" - "
<<
y
<<
" = "
<<
res2
<<
endl
;
self
->
quit
(
exit_reason
::
user_shutdown
);
self
->
quit
(
exit_reason
::
user_shutdown
);
}
}
);
);
...
@@ -68,12 +88,20 @@ void tester(event_based_actor* self, const actor& testee) {
...
@@ -68,12 +88,20 @@ void tester(event_based_actor* self, const actor& testee) {
);
);
}
}
void
test_calculators
()
{
scoped_actor
self
;
aout
(
self
)
<<
"blocking actor:"
<<
endl
;
self
->
spawn
(
tester
<
actor
>
,
spawn
<
blocking_api
>
(
blocking_calculator
),
1
,
2
);
self
->
await_all_other_actors_done
();
aout
(
self
)
<<
"event-based actor:"
<<
endl
;
self
->
spawn
(
tester
<
actor
>
,
spawn
(
calculator
),
3
,
4
);
self
->
await_all_other_actors_done
();
aout
(
self
)
<<
"typed actor:"
<<
endl
;
self
->
spawn
(
tester
<
calculator_actor
>
,
spawn_typed
(
typed_calculator
),
5
,
6
);
self
->
await_all_other_actors_done
();
}
int
main
()
{
int
main
()
{
cout
<<
"test blocking actor"
<<
endl
;
test_calculators
();
spawn
(
tester
,
spawn
<
blocking_api
>
(
blocking_calculator
));
await_all_actors_done
();
cout
<<
"test event-based actor"
<<
endl
;
spawn
(
tester
,
spawn
(
calculator
));
await_all_actors_done
();
shutdown
();
shutdown
();
}
}
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