summaryrefslogtreecommitdiff
path: root/include/mbgl/actor/actor_ref.hpp
blob: 958ee3777c3370dd57d34bd457918400a0121929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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)...));
        }
    }

    template <typename Fn, class... Args>
    auto ask(Fn fn, Args&&... args) {
        // Result type is deduced from the function's return type
        using ResultType = typename std::result_of<decltype(fn)(Object, Args...)>::type;

        std::promise<ResultType> promise;
        auto future = promise.get_future();

        if (auto mailbox = weakMailbox.lock()) {
            mailbox->push(
                    actor::makeMessage(
                            std::move(promise), *object, fn, std::forward<Args>(args)...
                    )
            );
        } else {
            promise.set_exception(std::make_exception_ptr(std::runtime_error("Actor has gone away")));
        }

        return future;
    }

private:
    Object* object;
    std::weak_ptr<Mailbox> weakMailbox;
};

} // namespace mbgl