Commit 42035bf4 authored by Dominik Charousset's avatar Dominik Charousset

Use type flag in `channel` to carry subtype

parent eeea70b4
......@@ -276,8 +276,6 @@ class abstract_actor : public abstract_channel {
// identifies the execution unit this actor is currently executed by
execution_unit* m_host;
// stores several actor-related flags
int m_flags;
};
} // namespace caf
......
......@@ -34,6 +34,9 @@ namespace caf {
*/
class abstract_channel : public ref_counted {
public:
friend class abstract_actor;
friend class abstract_group;
virtual ~abstract_channel();
/**
......@@ -54,17 +57,34 @@ class abstract_channel : public ref_counted {
*/
bool is_remote() const;
protected:
enum channel_type_flag {
is_abstract_actor_flag = 0x100000,
is_abstract_group_flag = 0x200000
};
abstract_channel(size_t initial_ref_count = 0);
inline bool is_abstract_actor() const {
return m_flags & static_cast<int>(is_abstract_actor_flag);
}
abstract_channel(node_id nid, size_t initial_ref_count = 0);
inline bool is_abstract_group() const {
return m_flags & static_cast<int>(is_abstract_group_flag);
}
private:
protected:
/**
* Accumulates several state and type flags. Subtypes may use only the
* first 20 bits, i.e., the bitmask 0xFFF00000 is reserved for
* channel-related flags.
*/
int m_flags;
private:
// can only be called from abstract_actor and abstract_group
abstract_channel(channel_type_flag subtype, size_t initial_ref_count = 0);
abstract_channel(channel_type_flag subtype, node_id nid,
size_t initial_ref_count = 0);
// identifies the node of this channel
node_id m_node;
};
/**
......
......@@ -48,20 +48,20 @@ using guard_type = std::unique_lock<std::mutex>;
// by std::atomic<> constructor
abstract_actor::abstract_actor(actor_id aid, node_id nid, size_t initial_count)
: super(std::move(nid), initial_count),
: super(abstract_channel::is_abstract_actor_flag,
std::move(nid), initial_count),
m_id(aid),
m_exit_reason(exit_reason::not_exited),
m_host(nullptr),
m_flags(0) {
m_host(nullptr) {
// nop
}
abstract_actor::abstract_actor(size_t initial_count)
: super(detail::singletons::get_node_id(), initial_count),
: super(abstract_channel::is_abstract_actor_flag,
detail::singletons::get_node_id(), initial_count),
m_id(detail::singletons::get_actor_registry()->next_id()),
m_exit_reason(exit_reason::not_exited),
m_host(nullptr),
m_flags(0) {
m_host(nullptr) {
// nop
}
......
......@@ -24,14 +24,18 @@ namespace caf {
using detail::singletons;
abstract_channel::abstract_channel(size_t initial_ref_count)
abstract_channel::abstract_channel(channel_type_flag subtype,
size_t initial_ref_count)
: ref_counted(initial_ref_count),
m_flags(static_cast<int>(subtype)),
m_node(singletons::get_node_id()) {
// nop
}
abstract_channel::abstract_channel(node_id nid, size_t initial_ref_count)
abstract_channel::abstract_channel(channel_type_flag subtype, node_id nid,
size_t initial_ref_count)
: ref_counted(initial_ref_count),
m_flags(static_cast<int>(subtype)),
m_node(std::move(nid)) {
// nop
}
......
......@@ -56,7 +56,9 @@ const std::string& abstract_group::module::name() {
}
abstract_group::abstract_group(abstract_group::module_ptr mod, std::string id)
: m_module(mod), m_identifier(std::move(id)) {
: abstract_channel(abstract_channel::is_abstract_group_flag),
m_module(mod),
m_identifier(std::move(id)) {
// nop
}
......
......@@ -182,23 +182,19 @@ void serialize_impl(const channel& chref, serializer* sink) {
} else {
// raw pointer
auto rptr = actor_cast<abstract_channel*>(chref);
// raw actor pointer
abstract_actor_ptr aptr = dynamic_cast<abstract_actor*>(rptr);
if (aptr != nullptr) {
CAF_REQUIRE(rptr->is_abstract_actor() || rptr->is_abstract_group());
if (rptr->is_abstract_actor()) {
// raw actor pointer
abstract_actor_ptr aptr = static_cast<abstract_actor*>(rptr);
flag = 1;
sink->write_value(flag);
serialize_impl(actor_cast<actor>(aptr), sink);
} else {
// get raw group pointer and store it inside a group handle
group tmp{dynamic_cast<abstract_group*>(rptr)};
if (tmp) {
flag = 2;
sink->write_value(flag);
serialize_impl(tmp, sink);
} else {
CAF_LOGF_ERROR("ptr is neither an actor nor a group");
wr_nullptr();
}
group tmp{static_cast<abstract_group*>(rptr)};
flag = 2;
sink->write_value(flag);
serialize_impl(tmp, sink);
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment