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
8d96abe9
Commit
8d96abe9
authored
Jul 09, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ufownl-topic/delegate' into develop
parents
05e60667
c88f290b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
215 additions
and
39 deletions
+215
-39
libcaf_core/caf/delegated.hpp
libcaf_core/caf/delegated.hpp
+33
-0
libcaf_core/caf/detail/ctm.hpp
libcaf_core/caf/detail/ctm.hpp
+7
-16
libcaf_core/caf/detail/typed_actor_util.hpp
libcaf_core/caf/detail/typed_actor_util.hpp
+11
-2
libcaf_core/caf/local_actor.hpp
libcaf_core/caf/local_actor.hpp
+62
-1
libcaf_core/test/typed_spawn.cpp
libcaf_core/test/typed_spawn.cpp
+102
-20
No files found.
libcaf_core/caf/delegated.hpp
0 → 100644
View file @
8d96abe9
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2015 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_DELEGATED_HPP
#define CAF_DELEGATED_HPP
namespace
caf
{
/// Helper class to indicate that a request has been forwarded.
template
<
class
...
Ts
>
struct
delegated
{
// nop
};
}
// namespace caf
#endif // CAF_DELEGATED_HPP
libcaf_core/caf/detail/ctm.hpp
View file @
8d96abe9
...
...
@@ -20,6 +20,7 @@
#ifndef CAF_DETAIL_CTM_HPP
#define CAF_DETAIL_CTM_HPP
#include "caf/delegated.hpp"
#include "caf/replies_to.hpp"
#include "caf/typed_response_promise.hpp"
...
...
@@ -42,19 +43,6 @@ struct ctm_cmp<typed_mpi<In, L, R1>,
||
std
::
is_same
<
R2
,
empty_type_list
>::
value
;
};
/*
template <class In, class Out>
struct ctm_cmp<typed_mpi<In, Out, empty_type_list>,
typed_mpi<In, Out, empty_type_list>>
: std::true_type { };
template <class In, class L, class R>
struct ctm_cmp<typed_mpi<In, L, R>,
typed_mpi<In, L, R>>
: std::true_type { };
*/
template
<
class
In
,
class
Out
>
struct
ctm_cmp
<
typed_mpi
<
In
,
Out
,
empty_type_list
>
,
typed_mpi
<
In
,
type_list
<
typed_continue_helper
<
Out
>>
,
empty_type_list
>>
...
...
@@ -75,12 +63,15 @@ struct ctm_cmp<typed_mpi<In, L, R>,
typed_mpi
<
In
,
type_list
<
typed_response_promise
<
either_or_t
<
L
,
R
>>>
,
empty_type_list
>>
:
std
::
true_type
{
};
/*
template
<
class
In
,
class
...
Ts
>
struct
ctm_cmp
<
typed_mpi
<
In
,
type_list
<
Ts
...
>
,
empty_type_list
>
,
typed_mpi
<
In
,
type_list
<
delegated
<
Ts
...
>>
,
empty_type_list
>>
:
std
::
true_type
{
};
template
<
class
In
,
class
L
,
class
R
>
struct
ctm_cmp
<
typed_mpi
<
In
,
L
,
R
>
,
typed_mpi<In,
L
, empty_type_list>>
typed_mpi
<
In
,
type_list
<
delegated
<
either_or_t
<
L
,
R
>>>
,
empty_type_list
>>
:
std
::
true_type
{
};
*/
template
<
class
In
,
class
L
,
class
R
>
struct
ctm_cmp
<
typed_mpi
<
In
,
L
,
R
>
,
...
...
libcaf_core/caf/detail/typed_actor_util.hpp
View file @
8d96abe9
...
...
@@ -22,6 +22,7 @@
#include <tuple>
#include "caf/delegated.hpp"
#include "caf/replies_to.hpp"
#include "caf/system_messages.hpp"
...
...
@@ -207,8 +208,16 @@ struct deduce_output_type {
>
::
value
;
static_assert
(
input_pos
!=
-
1
,
"typed actor does not support given input"
);
using
signature
=
typename
tl_at
<
Signatures
,
input_pos
>::
type
;
using
type
=
detail
::
type_pair
<
typename
signature
::
output_opt1_types
,
typename
signature
::
output_opt2_types
>
;
using
opt1
=
typename
signature
::
output_opt1_types
;
using
opt2
=
typename
signature
::
output_opt2_types
;
using
type
=
detail
::
type_pair
<
opt1
,
opt2
>
;
// generates the appropriate `delegated<...>` type from given signatures
using
delegated_type
=
typename
std
::
conditional
<
std
::
is_same
<
opt2
,
detail
::
empty_type_list
>::
value
,
typename
detail
::
tl_apply
<
opt1
,
delegated
>::
type
,
delegated
<
either_or_t
<
opt1
,
opt2
>>
>::
type
;
};
template
<
class
...
Ts
>
...
...
libcaf_core/caf/local_actor.hpp
View file @
8d96abe9
...
...
@@ -34,6 +34,7 @@
#include "caf/channel.hpp"
#include "caf/duration.hpp"
#include "caf/behavior.hpp"
#include "caf/delegated.hpp"
#include "caf/spawn_fwd.hpp"
#include "caf/resumable.hpp"
#include "caf/actor_cast.hpp"
...
...
@@ -459,6 +460,66 @@ public:
void
forward_message
(
const
actor
&
dest
,
message_priority
mp
);
template
<
class
...
Ts
>
void
delegate
(
message_priority
mp
,
const
actor
&
dest
,
Ts
&&
...
xs
)
{
static_assert
(
sizeof
...(
Ts
)
>
0
,
"no message to send"
);
if
(
!
dest
)
return
;
auto
mid
=
current_element_
->
mid
;
current_element_
->
mid
=
mp
==
message_priority
::
high
?
mid
.
with_high_priority
()
:
mid
.
with_normal_priority
();
current_element_
->
msg
=
make_message
(
std
::
forward
<
Ts
>
(
xs
)...);
dest
->
enqueue
(
std
::
move
(
current_element_
),
host
());
}
template
<
class
...
Ts
>
void
delegate
(
const
actor
&
dest
,
Ts
&&
...
xs
)
{
delegate
(
message_priority
::
normal
,
dest
,
std
::
forward
<
Ts
>
(
xs
)...);
}
template
<
class
...
Sigs
,
class
...
Ts
>
typename
detail
::
deduce_output_type
<
detail
::
type_list
<
Sigs
...
>
,
detail
::
type_list
<
typename
detail
::
implicit_conversions
<
typename
std
::
decay
<
Ts
>::
type
>::
type
...
>
>::
delegated_type
delegate
(
message_priority
mp
,
const
typed_actor
<
Sigs
...
>&
dest
,
Ts
&&
...
xs
)
{
static_assert
(
sizeof
...(
Ts
)
>
0
,
"no message to send"
);
using
token
=
detail
::
type_list
<
typename
detail
::
implicit_conversions
<
typename
std
::
decay
<
Ts
>::
type
>::
type
...
>
;
token
tk
;
check_typed_input
(
dest
,
tk
);
if
(
!
dest
)
return
{};
auto
mid
=
current_element_
->
mid
;
current_element_
->
mid
=
mp
==
message_priority
::
high
?
mid
.
with_high_priority
()
:
mid
.
with_normal_priority
();
current_element_
->
msg
=
make_message
(
std
::
forward
<
Ts
>
(
xs
)...);
dest
->
enqueue
(
std
::
move
(
current_element_
),
host
());
return
{};
}
template
<
class
...
Sigs
,
class
...
Ts
>
typename
detail
::
deduce_output_type
<
detail
::
type_list
<
Sigs
...
>
,
detail
::
type_list
<
typename
detail
::
implicit_conversions
<
typename
std
::
decay
<
Ts
>::
type
>::
type
...
>
>::
delegated_type
delegate
(
const
typed_actor
<
Sigs
...
>&
dest
,
Ts
&&
...
xs
)
{
return
delegate
(
message_priority
::
normal
,
dest
,
std
::
forward
<
Ts
>
(
xs
)...);
}
inline
uint32_t
planned_exit_reason
()
const
{
return
planned_exit_reason_
;
}
...
...
@@ -594,7 +655,7 @@ private:
host
());
}
void
send_impl
(
message_id
m
p
,
abstract_channel
*
dest
,
message
what
)
const
;
void
send_impl
(
message_id
m
id
,
abstract_channel
*
dest
,
message
what
)
const
;
void
delayed_send_impl
(
message_id
mid
,
const
channel
&
whom
,
const
duration
&
rtime
,
message
data
);
...
...
libcaf_core/test/typed_spawn.cpp
View file @
8d96abe9
...
...
@@ -49,7 +49,7 @@ static_assert(! std::is_convertible<dummy1, dummy2>::value,
"handle is assignable to broader definition"
);
/******************************************************************************
*
simple request/response test
*
*
simple request/response test
*
******************************************************************************/
struct
my_request
{
...
...
@@ -138,7 +138,7 @@ void test_typed_spawn(server_type ts) {
}
/******************************************************************************
*
test skipping of messages intentionally + using become()
*
*
test skipping of messages intentionally + using become()
*
******************************************************************************/
struct
get_state_msg
{};
...
...
@@ -184,35 +184,75 @@ public:
};
/******************************************************************************
*
simple 'forwarding' chain
*
*
simple 'forwarding' chain
*
******************************************************************************/
using
string_actor
=
typed_actor
<
replies_to
<
string
>::
with
<
string
>>
;
void
simple_relay
(
string_actor
::
pointer
self
,
string_actor
master
,
bool
leaf
)
{
string_actor
next
=
leaf
?
spawn_typed
(
simple_relay
,
master
,
false
)
:
master
;
string_actor
::
behavior_type
string_reverter
()
{
return
{
[](
string
&
str
)
{
std
::
reverse
(
str
.
begin
(),
str
.
end
());
return
std
::
move
(
str
);
}
};
}
// uses `return sync_send(...).then(...)`
string_actor
::
behavior_type
string_relay
(
string_actor
::
pointer
self
,
string_actor
master
,
bool
leaf
)
{
auto
next
=
leaf
?
spawn_typed
(
string_relay
,
master
,
false
)
:
master
;
self
->
link_to
(
next
);
self
->
become
(
return
{
[
=
](
const
string
&
str
)
{
return
self
->
sync_send
(
next
,
str
).
then
(
[](
const
string
&
answer
)
->
string
{
return
answer
;
[](
string
&
answer
)
->
string
{
return
std
::
move
(
answer
)
;
}
);
});
}
};
}
// uses `return delegate(...)`
string_actor
::
behavior_type
string_delegator
(
string_actor
::
pointer
self
,
string_actor
master
,
bool
leaf
)
{
auto
next
=
leaf
?
spawn_typed
(
string_delegator
,
master
,
false
)
:
master
;
self
->
link_to
(
next
);
return
{
[
=
](
string
&
str
)
->
delegated
<
string
>
{
return
self
->
delegate
(
next
,
std
::
move
(
str
));
}
};
}
string_actor
::
behavior_type
simple_string_reverter
()
{
using
maybe_string_actor
=
typed_actor
<
replies_to
<
string
>
::
with_either
<
ok_atom
,
string
>
::
or_else
<
error_atom
>>
;
maybe_string_actor
::
behavior_type
maybe_string_reverter
()
{
return
{
[](
const
string
&
str
)
{
return
string
{
str
.
rbegin
(),
str
.
rend
()};
[](
string
&
str
)
->
either
<
ok_atom
,
string
>::
or_else
<
error_atom
>
{
if
(
str
.
empty
())
return
{
error_atom
::
value
};
std
::
reverse
(
str
.
begin
(),
str
.
end
());
return
{
ok_atom
::
value
,
std
::
move
(
str
)};
}
};
}
maybe_string_actor
::
behavior_type
maybe_string_delegator
(
maybe_string_actor
::
pointer
self
,
maybe_string_actor
x
)
{
self
->
link_to
(
x
);
return
{
[
=
](
string
&
s
)
->
delegated
<
either
<
ok_atom
,
string
>::
or_else
<
error_atom
>>
{
return
self
->
delegate
(
x
,
std
::
move
(
s
));
}
};
}
/******************************************************************************
*
sending typed actor handles
*
*
sending typed actor handles
*
******************************************************************************/
using
int_actor
=
typed_actor
<
replies_to
<
int
>::
with
<
int
>>
;
...
...
@@ -341,18 +381,60 @@ CAF_TEST(test_event_testee) {
CAF_CHECK_EQUAL
(
result
,
"wait4int"
);
}
CAF_TEST
(
test_simple_string_reverter
)
{
CAF_TEST
(
reverter_relay_chain
)
{
// run test series with string reverter
scoped_actor
self
;
// actor-under-test
auto
aut
=
self
->
spawn_typed
<
monitored
>
(
s
imple
_relay
,
spawn_typed
(
s
imple_s
tring_reverter
),
auto
aut
=
self
->
spawn_typed
<
monitored
>
(
s
tring
_relay
,
spawn_typed
(
string_reverter
),
true
);
set
<
string
>
iface
{
"caf::replies_to<@str>::with<@str>"
};
CAF_CHECK
(
aut
->
message_types
()
==
iface
);
self
->
sync_send
(
aut
,
"Hello World!"
).
await
([](
const
string
&
answer
)
{
CAF_CHECK_EQUAL
(
answer
,
"!dlroW olleH"
);
});
self
->
sync_send
(
aut
,
"Hello World!"
).
await
(
[](
const
string
&
answer
)
{
CAF_CHECK_EQUAL
(
answer
,
"!dlroW olleH"
);
}
);
anon_send_exit
(
aut
,
exit_reason
::
user_shutdown
);
}
CAF_TEST
(
string_delegator_chain
)
{
// run test series with string reverter
scoped_actor
self
;
// actor-under-test
auto
aut
=
self
->
spawn_typed
<
monitored
>
(
string_delegator
,
spawn_typed
(
string_reverter
),
true
);
set
<
string
>
iface
{
"caf::replies_to<@str>::with<@str>"
};
CAF_CHECK
(
aut
->
message_types
()
==
iface
);
self
->
sync_send
(
aut
,
"Hello World!"
).
await
(
[](
const
string
&
answer
)
{
CAF_CHECK_EQUAL
(
answer
,
"!dlroW olleH"
);
}
);
anon_send_exit
(
aut
,
exit_reason
::
user_shutdown
);
}
CAF_TEST
(
maybe_string_delegator_chain
)
{
scoped_actor
self
;
auto
aut
=
spawn_typed
(
maybe_string_delegator
,
spawn_typed
(
maybe_string_reverter
));
self
->
sync_send
(
aut
,
""
).
await
(
[](
ok_atom
,
const
string
&
)
{
throw
std
::
logic_error
(
"unexpected result!"
);
},
[](
error_atom
)
{
// nop
}
);
self
->
sync_send
(
aut
,
"abcd"
).
await
(
[](
ok_atom
,
const
string
&
str
)
{
CAF_CHECK_EQUAL
(
str
,
"dcba"
);
},
[](
error_atom
)
{
throw
std
::
logic_error
(
"unexpected error_atom!"
);
}
);
anon_send_exit
(
aut
,
exit_reason
::
user_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