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
1e23f322
Commit
1e23f322
authored
Jul 27, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow actor to define a custom name
parent
528285ba
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
97 additions
and
0 deletions
+97
-0
libcaf_core/caf/detail/type_traits.hpp
libcaf_core/caf/detail/type_traits.hpp
+25
-0
libcaf_core/caf/experimental/stateful_actor.hpp
libcaf_core/caf/experimental/stateful_actor.hpp
+26
-0
libcaf_core/caf/local_actor.hpp
libcaf_core/caf/local_actor.hpp
+5
-0
libcaf_core/src/local_actor.cpp
libcaf_core/src/local_actor.cpp
+4
-0
libcaf_core/test/stateful_actor.cpp
libcaf_core/test/stateful_actor.cpp
+37
-0
No files found.
libcaf_core/caf/detail/type_traits.hpp
View file @
1e23f322
...
...
@@ -462,6 +462,31 @@ struct is_optional<optional<T>> : std::true_type {
// no members
};
// Checks whether T has a member variable named `name`.
template
<
class
T
>
class
has_name
{
private:
// a simple struct with a member called `name`
struct
fallback
{
int
name
;
};
// creates an ambiguity for any `T` with the requested member
struct
derived
:
T
,
fallback
{
// no members
};
// picked for any U without requested member since `U::name` is not ambigious
template
<
class
U
>
static
char
fun
(
U
*
,
decltype
(
U
::
name
)
*
=
nullptr
);
// picked for any U with requested member since `U::name` is ambigious
static
int
fun
(
void
*
);
public:
static
constexpr
bool
value
=
sizeof
(
fun
(
static_cast
<
derived
*>
(
nullptr
)))
>
1
;
};
}
// namespace detail
}
// namespace caf
...
...
libcaf_core/caf/experimental/stateful_actor.hpp
View file @
1e23f322
...
...
@@ -24,6 +24,7 @@
#include <type_traits>
#include "caf/fwd.hpp"
#include "caf/detail/type_traits.hpp"
namespace
caf
{
namespace
experimental
{
...
...
@@ -50,6 +51,10 @@ public:
state_
.
~
State
();
}
const
char
*
name
()
const
override
final
{
return
get_name
(
state_
);
}
/// A reference to the actor's state.
State
&
state
;
...
...
@@ -75,6 +80,27 @@ private:
new
(
&
state_
)
State
();
}
static
const
char
*
unbox_str
(
const
char
*
str
)
{
return
str
;
}
template
<
class
U
>
static
const
char
*
unbox_str
(
const
U
&
str
)
{
return
str
.
c_str
();
}
template
<
class
U
>
typename
std
::
enable_if
<
detail
::
has_name
<
U
>::
value
,
const
char
*>::
type
get_name
(
const
U
&
st
)
const
{
return
unbox_str
(
st
.
name
);
}
template
<
class
U
>
typename
std
::
enable_if
<!
detail
::
has_name
<
U
>::
value
,
const
char
*>::
type
get_name
(
const
U
&
)
const
{
return
Base
::
name
();
}
union
{
State
state_
;
};
};
...
...
libcaf_core/caf/local_actor.hpp
View file @
1e23f322
...
...
@@ -387,6 +387,11 @@ public:
attach
(
attachable_ptr
{
new
functor_attachable
(
std
::
move
(
f
))});
}
/// Returns an implementation-dependent name for logging purposes, which
/// is only valid as long as the actor is running. The default
/// implementation simply returns "actor".
virtual
const
char
*
name
()
const
;
/****************************************************************************
* deprecated member functions *
****************************************************************************/
...
...
libcaf_core/src/local_actor.cpp
View file @
1e23f322
...
...
@@ -820,6 +820,10 @@ response_promise local_actor::make_response_promise() {
return
result
;
}
const
char
*
local_actor
::
name
()
const
{
return
"actor"
;
}
behavior
&
local_actor
::
get_behavior
()
{
return
pending_responses_
.
empty
()
?
bhvr_stack_
.
back
()
:
pending_responses_
.
front
().
second
;
...
...
libcaf_core/test/stateful_actor.cpp
View file @
1e23f322
...
...
@@ -98,6 +98,22 @@ void test_adder(ActorUnderTest aut) {
anon_send_exit
(
aut
,
exit_reason
::
kill
);
}
template
<
class
State
>
void
test_name
(
const
char
*
expected
)
{
auto
aut
=
spawn
([](
stateful_actor
<
State
>*
self
)
->
behavior
{
return
[
=
](
get_atom
)
{
self
->
quit
();
return
self
->
name
();
};
});
scoped_actor
self
;
self
->
sync_send
(
aut
,
get_atom
::
value
).
await
(
[
&
](
const
string
&
str
)
{
CAF_CHECK_EQUAL
(
str
,
expected
);
}
);
}
}
// namespace <anonymous>
CAF_TEST_FIXTURE_SCOPE
(
dynamic_stateful_actor_tests
,
fixture
)
...
...
@@ -118,4 +134,25 @@ CAF_TEST(typed_stateful_actor_class) {
test_adder
(
spawn_typed
<
typed_adder_class
>
());
}
CAF_TEST
(
no_name
)
{
struct
state
{
// empty
};
test_name
<
state
>
(
"actor"
);
}
CAF_TEST
(
char_name
)
{
struct
state
{
const
char
*
name
=
"testee"
;
};
test_name
<
state
>
(
"testee"
);
}
CAF_TEST
(
string_name
)
{
struct
state
{
string
name
=
"testee2"
;
};
test_name
<
state
>
(
"testee2"
);
}
CAF_TEST_FIXTURE_SCOPE_END
()
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