Commit 3c873350 authored by Dominik Charousset's avatar Dominik Charousset

Use type tags instead of typeid for attachables

parent f6762dfc
...@@ -100,11 +100,6 @@ class abstract_actor : public abstract_channel { ...@@ -100,11 +100,6 @@ class abstract_actor : public abstract_channel {
*/ */
size_t detach(const attachable::token& what); size_t detach(const attachable::token& what);
template <class T>
size_t detach(const T& what) {
return detach(attachable::token{typeid(T), &what});
}
enum linking_operation { enum linking_operation {
establish_link_op, establish_link_op,
establish_backlink_op, establish_backlink_op,
......
...@@ -59,6 +59,7 @@ class abstract_group : public abstract_channel { ...@@ -59,6 +59,7 @@ class abstract_group : public abstract_channel {
struct subscription_token { struct subscription_token {
intrusive_ptr<abstract_group> group; intrusive_ptr<abstract_group> group;
static constexpr size_t token_type = attachable::token::subscription;
}; };
class subscription_predicate { class subscription_predicate {
......
...@@ -41,25 +41,40 @@ class attachable { ...@@ -41,25 +41,40 @@ class attachable {
attachable& operator=(const attachable&) = delete; attachable& operator=(const attachable&) = delete;
/** /**
* Represents a pointer to a value with its RTTI. * Represents a pointer to a value with its subtype as type ID number.
*/ */
struct token { struct token {
/**
* Identifies a non-matchable subtype.
*/
static constexpr size_t anonymous = 0;
/**
* Identifies `abstract_group::subscription`.
*/
static constexpr size_t subscription = 1;
/**
* Identifies `default_attachable::observe_token`.
*/
static constexpr size_t observer = 2;
template <class T> template <class T>
token(const T& tk) : subtype(typeid(T)), ptr(&tk) { token(const T& tk) : subtype(T::token_type), ptr(&tk) {
// nop // nop
} }
/** /**
* Denotes the type of ptr. * Denotes the type of ptr.
*/ */
const std::type_info& subtype; size_t subtype;
/** /**
* Any value, used to identify attachable instances. * Any value, used to identify attachable instances.
*/ */
const void* ptr; const void* ptr;
token(const std::type_info& subtype, const void* ptr); token(size_t subtype, const void* ptr);
}; };
virtual ~attachable(); virtual ~attachable();
...@@ -89,7 +104,7 @@ class attachable { ...@@ -89,7 +104,7 @@ class attachable {
*/ */
template <class T> template <class T>
bool matches(const T& what) { bool matches(const T& what) {
return matches(token{typeid(T), &what}); return matches(token{T::token_type, &what});
} }
std::unique_ptr<attachable> next; std::unique_ptr<attachable> next;
......
...@@ -35,6 +35,7 @@ class default_attachable : public attachable { ...@@ -35,6 +35,7 @@ class default_attachable : public attachable {
struct observe_token { struct observe_token {
actor_addr observer; actor_addr observer;
observe_type type; observe_type type;
static constexpr size_t token_type = attachable::token::observer;
}; };
void actor_exited(abstract_actor* self, uint32_t reason) override; void actor_exited(abstract_actor* self, uint32_t reason) override;
......
...@@ -39,6 +39,7 @@ struct functor_attachable : attachable { ...@@ -39,6 +39,7 @@ struct functor_attachable : attachable {
void actor_exited(abstract_actor* self, uint32_t reason) override { void actor_exited(abstract_actor* self, uint32_t reason) override {
m_functor(self, reason); m_functor(self, reason);
} }
static constexpr size_t token_type = attachable::token::anonymous;
}; };
template <class F> template <class F>
......
...@@ -37,7 +37,7 @@ void abstract_group::subscription::actor_exited(abstract_actor* ptr, uint32_t) { ...@@ -37,7 +37,7 @@ void abstract_group::subscription::actor_exited(abstract_actor* ptr, uint32_t) {
} }
bool abstract_group::subscription::matches(const token& what) { bool abstract_group::subscription::matches(const token& what) {
if (what.subtype != typeid(subscription_token)) { if (what.subtype != attachable::token::subscription) {
return false; return false;
} }
auto& ot = *reinterpret_cast<const subscription_token*>(what.ptr); auto& ot = *reinterpret_cast<const subscription_token*>(what.ptr);
......
...@@ -25,8 +25,8 @@ attachable::~attachable() { ...@@ -25,8 +25,8 @@ attachable::~attachable() {
// nop // nop
} }
attachable::token::token(const std::type_info& tinfo, const void* vptr) attachable::token::token(size_t typenr, const void* vptr)
: subtype(tinfo), ptr(vptr) { : subtype(typenr), ptr(vptr) {
// nop // nop
} }
......
...@@ -44,7 +44,7 @@ void default_attachable::actor_exited(abstract_actor* self, uint32_t reason) { ...@@ -44,7 +44,7 @@ void default_attachable::actor_exited(abstract_actor* self, uint32_t reason) {
} }
bool default_attachable::matches(const token& what) { bool default_attachable::matches(const token& what) {
if (what.subtype != typeid(observe_token)) { if (what.subtype != attachable::token::observer) {
return false; return false;
} }
auto& ot = *reinterpret_cast<const observe_token*>(what.ptr); auto& ot = *reinterpret_cast<const observe_token*>(what.ptr);
......
...@@ -57,7 +57,7 @@ void local_actor::demonitor(const actor_addr& whom) { ...@@ -57,7 +57,7 @@ void local_actor::demonitor(const actor_addr& whom) {
} }
auto ptr = actor_cast<abstract_actor_ptr>(whom); auto ptr = actor_cast<abstract_actor_ptr>(whom);
default_attachable::observe_token tk{address(), default_attachable::monitor}; default_attachable::observe_token tk{address(), default_attachable::monitor};
ptr->detach({typeid(default_attachable::observe_token), &tk}); ptr->detach(tk);
} }
void local_actor::join(const group& what) { void local_actor::join(const group& what) {
......
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