summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-07-21 08:26:40 (GMT)
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-07-24 16:30:02 (GMT)
commit81e9ed7fdd65ca4816ac6a76139d6c18d5182a21 (patch)
tree17fbcc599c43394afd24c73a83bd0f8a7fc4cce9 /include
parenta9ac314a5d28bec2a26cd6889364f81f811d4cd4 (diff)
downloadqtlocation-mapboxgl-81e9ed7fdd65ca4816ac6a76139d6c18d5182a21.tar.gz
[core] add ask pattern to actor ref
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/actor/actor_ref.hpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/include/mbgl/actor/actor_ref.hpp b/include/mbgl/actor/actor_ref.hpp
index aeb5bb4..958ee37 100644
--- a/include/mbgl/actor/actor_ref.hpp
+++ b/include/mbgl/actor/actor_ref.hpp
@@ -35,6 +35,27 @@ public:
}
}
+ 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;