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
b95d3c8b
Commit
b95d3c8b
authored
Feb 20, 2014
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added example for aout and typed actors
parent
daef499b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
0 deletions
+126
-0
examples/aout.cpp
examples/aout.cpp
+32
-0
examples/message_passing/typed_calculator.cpp
examples/message_passing/typed_calculator.cpp
+94
-0
No files found.
examples/aout.cpp
0 → 100644
View file @
b95d3c8b
/******************************************************************************\
* This example illustrates how to use aout. *
\******************************************************************************/
#include <chrono>
#include <cstdlib>
#include <iostream>
#include "cppa/cppa.hpp"
using
namespace
cppa
;
using
std
::
endl
;
int
main
()
{
std
::
srand
(
std
::
time
(
0
));
for
(
int
i
=
1
;
i
<=
50
;
++
i
)
{
spawn
<
blocking_api
>
([
i
](
blocking_actor
*
self
)
{
aout
(
self
)
<<
"Hi there! This is actor nr. "
<<
i
<<
"!"
<<
endl
;
std
::
chrono
::
milliseconds
tout
{
std
::
rand
()
%
1000
};
self
->
delayed_send
(
self
,
tout
,
atom
(
"done"
));
self
->
receive
(
others
()
>>
[
i
,
self
]
{
aout
(
self
)
<<
"Actor nr. "
<<
i
<<
" says goodbye!"
<<
endl
;
});
});
}
// wait until all other actors we've spawned are done
await_all_actors_done
();
// done
shutdown
();
return
0
;
}
examples/message_passing/typed_calculator.cpp
0 → 100644
View file @
b95d3c8b
/******************************************************************************\
* This example is a very basic, non-interactive math service implemented *
* using typed actors. *
\******************************************************************************/
#include <cassert>
#include <iostream>
#include "cppa/cppa.hpp"
using
std
::
endl
;
using
namespace
cppa
;
struct
shutdown_request
{
};
struct
plus_request
{
int
a
;
int
b
;
};
struct
minus_request
{
int
a
;
int
b
;
};
typedef
typed_actor
<
replies_to
<
plus_request
>::
with
<
int
>
,
replies_to
<
minus_request
>::
with
<
int
>
,
replies_to
<
shutdown_request
>::
with
<
void
>
>
calculator_type
;
calculator_type
::
behavior_type
typed_calculator
(
calculator_type
::
pointer
self
)
{
return
{
[](
const
plus_request
&
pr
)
{
return
pr
.
a
+
pr
.
b
;
},
[](
const
minus_request
&
pr
)
{
return
pr
.
a
-
pr
.
b
;
},
[
=
](
const
shutdown_request
&
)
{
self
->
quit
();
}
};
}
class
typed_calculator_class
:
public
calculator_type
::
base
{
protected:
behavior_type
make_behavior
()
override
{
return
{
[](
const
plus_request
&
pr
)
{
return
pr
.
a
+
pr
.
b
;
},
[](
const
minus_request
&
pr
)
{
return
pr
.
a
-
pr
.
b
;
},
[
=
](
const
shutdown_request
&
)
{
quit
();
}
};
}
};
void
tester
(
event_based_actor
*
self
,
const
calculator_type
&
testee
)
{
self
->
link_to
(
testee
);
// will be invoked if we receive an unexpected response message
self
->
on_sync_failure
([
=
]
{
aout
(
self
)
<<
"AUT (actor under test) failed"
<<
endl
;
self
->
quit
(
exit_reason
::
user_shutdown
);
});
// first test: 2 + 1 = 3
self
->
sync_send
(
testee
,
plus_request
{
2
,
1
}).
then
(
[
=
](
int
r1
)
{
assert
(
r1
==
3
);
// second test: 2 - 1 = 1
self
->
sync_send
(
testee
,
minus_request
{
2
,
1
}).
then
(
[
=
](
int
r2
)
{
assert
(
r2
==
1
);
// both tests succeeded
aout
(
self
)
<<
"AUT (actor under test) seems to be ok"
<<
endl
;
self
->
send
(
testee
,
shutdown_request
{});
}
);
}
);
}
int
main
()
{
// announce custom message types
announce
<
shutdown_request
>
();
announce
<
plus_request
>
(
&
plus_request
::
a
,
&
plus_request
::
b
);
announce
<
minus_request
>
(
&
minus_request
::
a
,
&
minus_request
::
b
);
// test function-based impl
spawn
(
tester
,
spawn_typed
(
typed_calculator
));
await_all_actors_done
();
// test class-based impl
spawn
(
tester
,
spawn_typed
<
typed_calculator_class
>
());
await_all_actors_done
();
// done
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