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
370707c7
Commit
370707c7
authored
May 02, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use atom constants in typed calculator example
parent
366c771a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
32 deletions
+20
-32
examples/message_passing/typed_calculator.cpp
examples/message_passing/typed_calculator.cpp
+20
-32
No files found.
examples/message_passing/typed_calculator.cpp
View file @
370707c7
...
@@ -12,24 +12,21 @@ using namespace caf;
...
@@ -12,24 +12,21 @@ using namespace caf;
namespace
{
namespace
{
struct
shutdown_request
{
}
;
using
plus_atom
=
atom_constant
<
atom
(
"plus"
)
>
;
struct
plus_request
{
int
a
;
int
b
;
}
;
using
minus_atom
=
atom_constant
<
atom
(
"minus"
)
>
;
struct
minus_request
{
int
a
;
int
b
;
}
;
using
result_atom
=
atom_constant
<
atom
(
"result"
)
>
;
using
calculator_type
=
typed_actor
<
replies_to
<
plus_request
>::
with
<
int
>
,
using
calculator_type
=
replies_to
<
minus_request
>::
with
<
int
>
,
typed_actor
<
replies_to
<
plus_atom
,
int
,
int
>::
with
<
result_atom
,
int
>
,
replies_to
<
shutdown_request
>::
with
<
void
>>
;
replies_to
<
minus_atom
,
int
,
int
>::
with
<
result_atom
,
int
>>
;
calculator_type
::
behavior_type
typed_calculator
(
calculator_type
::
pointer
self
)
{
calculator_type
::
behavior_type
typed_calculator
(
calculator_type
::
pointer
)
{
return
{
return
{
[](
const
plus_request
&
pr
)
{
[](
plus_atom
,
int
x
,
int
y
)
{
return
pr
.
a
+
pr
.
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
x
+
y
)
;
},
},
[](
const
minus_request
&
pr
)
{
[](
minus_atom
,
int
x
,
int
y
)
{
return
pr
.
a
-
pr
.
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
x
-
y
);
},
[
=
](
const
shutdown_request
&
)
{
self
->
quit
();
}
}
};
};
}
}
...
@@ -38,14 +35,11 @@ class typed_calculator_class : public calculator_type::base {
...
@@ -38,14 +35,11 @@ class typed_calculator_class : public calculator_type::base {
protected:
protected:
behavior_type
make_behavior
()
override
{
behavior_type
make_behavior
()
override
{
return
{
return
{
[](
const
plus_request
&
pr
)
{
[](
plus_atom
,
int
x
,
int
y
)
{
return
pr
.
a
+
pr
.
b
;
return
std
::
make_tuple
(
result_atom
::
value
,
x
+
y
);
},
[](
const
minus_request
&
pr
)
{
return
pr
.
a
-
pr
.
b
;
},
},
[
=
](
const
shutdown_request
&
)
{
[
](
minus_atom
,
int
x
,
int
y
)
{
quit
(
);
return
std
::
make_tuple
(
result_atom
::
value
,
x
-
y
);
}
}
};
};
}
}
...
@@ -59,17 +53,17 @@ void tester(event_based_actor* self, const calculator_type& testee) {
...
@@ -59,17 +53,17 @@ void tester(event_based_actor* self, const calculator_type& 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_
request
{
2
,
1
}
).
then
(
self
->
sync_send
(
testee
,
plus_
atom
::
value
,
2
,
1
).
then
(
[
=
](
int
r1
)
{
[
=
](
result_atom
,
int
r1
)
{
// second test: 2 - 1 = 1
// second test: 2 - 1 = 1
self
->
sync_send
(
testee
,
minus_
request
{
2
,
1
}
).
then
(
self
->
sync_send
(
testee
,
minus_
atom
::
value
,
2
,
1
).
then
(
[
=
](
int
r2
)
{
[
=
](
result_atom
,
int
r2
)
{
// both tests succeeded
// both tests succeeded
if
(
r1
==
3
&&
r2
==
1
)
{
if
(
r1
==
3
&&
r2
==
1
)
{
aout
(
self
)
<<
"AUT (actor under test) seems to be ok"
aout
(
self
)
<<
"AUT (actor under test) seems to be ok"
<<
endl
;
<<
endl
;
}
}
self
->
send
(
testee
,
shutdown_request
{}
);
self
->
send
_exit
(
testee
,
exit_reason
::
user_shutdown
);
}
}
);
);
}
}
...
@@ -79,11 +73,6 @@ void tester(event_based_actor* self, const calculator_type& testee) {
...
@@ -79,11 +73,6 @@ void tester(event_based_actor* self, const calculator_type& testee) {
}
// namespace <anonymous>
}
// namespace <anonymous>
int
main
()
{
int
main
()
{
// announce custom message types
announce
<
shutdown_request
>
(
"shutdown_request"
);
announce
<
plus_request
>
(
"plus_request"
,
&
plus_request
::
a
,
&
plus_request
::
b
);
announce
<
minus_request
>
(
"minus_request"
,
&
minus_request
::
a
,
&
minus_request
::
b
);
// test function-based impl
// test function-based impl
spawn
(
tester
,
spawn_typed
(
typed_calculator
));
spawn
(
tester
,
spawn_typed
(
typed_calculator
));
await_all_actors_done
();
await_all_actors_done
();
...
@@ -92,5 +81,4 @@ int main() {
...
@@ -92,5 +81,4 @@ int main() {
await_all_actors_done
();
await_all_actors_done
();
// done
// done
shutdown
();
shutdown
();
return
0
;
}
}
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