diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-07-20 19:17:49 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-07-24 19:30:02 +0300 |
commit | a9ac314a5d28bec2a26cd6889364f81f811d4cd4 (patch) | |
tree | b1622c574ee1bc659cd11ab24f3ab16098326eca /include/mbgl/actor/actor.hpp | |
parent | 3364992be94364a6e8b302124db53aeed5d99c15 (diff) | |
download | qtlocation-mapboxgl-a9ac314a5d28bec2a26cd6889364f81f811d4cd4.tar.gz |
[core] implement ask pattern in actor
Diffstat (limited to 'include/mbgl/actor/actor.hpp')
-rw-r--r-- | include/mbgl/actor/actor.hpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/include/mbgl/actor/actor.hpp b/include/mbgl/actor/actor.hpp index 810114c513..00e1bb82f8 100644 --- a/include/mbgl/actor/actor.hpp +++ b/include/mbgl/actor/actor.hpp @@ -6,6 +6,7 @@ #include <mbgl/util/noncopyable.hpp> #include <memory> +#include <future> namespace mbgl { @@ -61,6 +62,17 @@ public: 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(); + mailbox->push(actor::makeMessage(std::move(promise), object, fn, std::forward<Args>(args)...)); + return future; + } + ActorRef<std::decay_t<Object>> self() { return ActorRef<std::decay_t<Object>>(object, mailbox); } |