Unverified Commit 463ebbfe authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #637

Add templated put/get registry methods
parents f2473e03 ef80cb74
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/actor.hpp" #include "caf/actor.hpp"
#include "caf/actor_cast.hpp"
#include "caf/abstract_actor.hpp" #include "caf/abstract_actor.hpp"
#include "caf/actor_control_block.hpp" #include "caf/actor_control_block.hpp"
...@@ -49,10 +50,16 @@ public: ...@@ -49,10 +50,16 @@ public:
~actor_registry(); ~actor_registry();
/// Returns the local actor associated to `key`. /// Returns the local actor associated to `key`.
strong_actor_ptr get(actor_id key) const; template<class T = strong_actor_ptr>
T get(actor_id key) const {
return actor_cast<T>(get_impl(key));
}
/// Associates a local actor with its ID. /// Associates a local actor with its ID.
void put(actor_id key, strong_actor_ptr val); template<class T>
void put(actor_id key, const T& val) {
put_impl(key, actor_cast<strong_actor_ptr>(val));
}
/// Removes an actor from this registry, /// Removes an actor from this registry,
/// leaving `reason` for future reference. /// leaving `reason` for future reference.
...@@ -72,10 +79,17 @@ public: ...@@ -72,10 +79,17 @@ public:
void await_running_count_equal(size_t expected) const; void await_running_count_equal(size_t expected) const;
/// Returns the actor associated with `key` or `invalid_actor`. /// Returns the actor associated with `key` or `invalid_actor`.
strong_actor_ptr get(atom_value key) const; template<class T = strong_actor_ptr>
T get(atom_value key) const {
return actor_cast<T>(get_impl(key));
}
/// Associates given actor to `key`. /// Associates given actor to `key`.
void put(atom_value key, strong_actor_ptr value); template<class T>
void put(atom_value key, const T& value) {
// using reference here and before to allow putting a scoped_actor without calling .ptr()
put_impl(key, actor_cast<strong_actor_ptr>(value));
}
/// Removes a name mapping. /// Removes a name mapping.
void erase(atom_value key); void erase(atom_value key);
...@@ -91,6 +105,18 @@ private: ...@@ -91,6 +105,18 @@ private:
// Stops this component. // Stops this component.
void stop(); void stop();
/// Returns the local actor associated to `key`.
strong_actor_ptr get_impl(actor_id key) const;
/// Associates a local actor with its ID.
void put_impl(actor_id key, strong_actor_ptr val);
/// Returns the actor associated with `key` or `invalid_actor`.
strong_actor_ptr get_impl(atom_value key) const;
/// Associates given actor to `key`.
void put_impl(atom_value key, strong_actor_ptr value);
using entries = std::unordered_map<actor_id, strong_actor_ptr>; using entries = std::unordered_map<actor_id, strong_actor_ptr>;
actor_registry(actor_system& sys); actor_registry(actor_system& sys);
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include "caf/sec.hpp" #include "caf/sec.hpp"
#include "caf/locks.hpp" #include "caf/locks.hpp"
#include "caf/logger.hpp" #include "caf/logger.hpp"
#include "caf/actor_cast.hpp"
#include "caf/attachable.hpp" #include "caf/attachable.hpp"
#include "caf/exit_reason.hpp" #include "caf/exit_reason.hpp"
#include "caf/actor_system.hpp" #include "caf/actor_system.hpp"
...@@ -56,7 +55,7 @@ actor_registry::actor_registry(actor_system& sys) : running_(0), system_(sys) { ...@@ -56,7 +55,7 @@ actor_registry::actor_registry(actor_system& sys) : running_(0), system_(sys) {
// nop // nop
} }
strong_actor_ptr actor_registry::get(actor_id key) const { strong_actor_ptr actor_registry::get_impl(actor_id key) const {
shared_guard guard(instances_mtx_); shared_guard guard(instances_mtx_);
auto i = entries_.find(key); auto i = entries_.find(key);
if (i != entries_.end()) if (i != entries_.end())
...@@ -65,7 +64,7 @@ strong_actor_ptr actor_registry::get(actor_id key) const { ...@@ -65,7 +64,7 @@ strong_actor_ptr actor_registry::get(actor_id key) const {
return nullptr; return nullptr;
} }
void actor_registry::put(actor_id key, strong_actor_ptr val) { void actor_registry::put_impl(actor_id key, strong_actor_ptr val) {
CAF_LOG_TRACE(CAF_ARG(key)); CAF_LOG_TRACE(CAF_ARG(key));
if (!val) if (!val)
return; return;
...@@ -119,7 +118,7 @@ void actor_registry::await_running_count_equal(size_t expected) const { ...@@ -119,7 +118,7 @@ void actor_registry::await_running_count_equal(size_t expected) const {
} }
} }
strong_actor_ptr actor_registry::get(atom_value key) const { strong_actor_ptr actor_registry::get_impl(atom_value key) const {
shared_guard guard{named_entries_mtx_}; shared_guard guard{named_entries_mtx_};
auto i = named_entries_.find(key); auto i = named_entries_.find(key);
if (i == named_entries_.end()) if (i == named_entries_.end())
...@@ -127,10 +126,10 @@ strong_actor_ptr actor_registry::get(atom_value key) const { ...@@ -127,10 +126,10 @@ strong_actor_ptr actor_registry::get(atom_value key) const {
return i->second; return i->second;
} }
void actor_registry::put(atom_value key, strong_actor_ptr value) { void actor_registry::put_impl(atom_value key, strong_actor_ptr value) {
if (value) if (value)
value->get()->attach_functor([=] { value->get()->attach_functor([=] {
system_.registry().put(key, nullptr); system_.registry().put_impl(key, nullptr);
}); });
exclusive_guard guard{named_entries_mtx_}; exclusive_guard guard{named_entries_mtx_};
named_entries_.emplace(key, std::move(value)); named_entries_.emplace(key, std::move(value));
......
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