diff options
author | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2017-06-21 17:29:05 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2017-06-26 16:32:21 +0300 |
commit | 54c3ccc37d4534f699437f9c1854d8f5a857069e (patch) | |
tree | e033ecbe2c2978a547d0d097818764063a4ac1b8 /include/mbgl/actor/actor_ref.hpp | |
parent | 19e872b15c1fb9852b53ffb7e2850d2a2a7763da (diff) | |
download | qtlocation-mapboxgl-54c3ccc37d4534f699437f9c1854d8f5a857069e.tar.gz |
[core] Make the mbgl/actor headers public
They will be needed by the DefaultFileSource, something that we
also export as public.
Diffstat (limited to 'include/mbgl/actor/actor_ref.hpp')
-rw-r--r-- | include/mbgl/actor/actor_ref.hpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/include/mbgl/actor/actor_ref.hpp b/include/mbgl/actor/actor_ref.hpp new file mode 100644 index 0000000000..9d858d823f --- /dev/null +++ b/include/mbgl/actor/actor_ref.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include <mbgl/actor/mailbox.hpp> +#include <mbgl/actor/message.hpp> + +#include <memory> + +namespace mbgl { + +/* + An `ActorRef<O>` is a *non*-owning, weak reference to an actor of type `O`. You can send it + messages just like an `Actor<O>`. It's a value object: safe to copy and pass between actors + via messages. + + An `ActorRef<O>` does not extend the lifetime of the corresponding `Actor<O>`. That's determined + entirely by whichever object owns the `Actor<O>` -- the actor's "supervisor". + + It's safe for a `Ref` to outlive its `Actor` -- the reference is "weak", and does not extend + the lifetime of the owning Actor, and sending a message to a `Ref` whose `Actor` has died is + a no-op. (In the future, a dead-letters queue or log may be implemented.) +*/ + +template <class Object> +class ActorRef { +public: + ActorRef(Object& object_, std::weak_ptr<Mailbox> weakMailbox_) + : object(object_), + weakMailbox(std::move(weakMailbox_)) { + } + + template <typename Fn, class... Args> + void invoke(Fn fn, Args&&... args) { + if (auto mailbox = weakMailbox.lock()) { + mailbox->push(actor::makeMessage(object, fn, std::forward<Args>(args)...)); + } + } + +private: + Object& object; + std::weak_ptr<Mailbox> weakMailbox; +}; + +} // namespace mbgl |