diff options
author | Langston Smith <langston.smith@mapbox.com> | 2018-01-04 11:15:50 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-04 11:15:50 -0800 |
commit | 2ea955d2751ba6459f99a0695e53505c0a11702b (patch) | |
tree | f54450918b634a2eea1bd2c4ebc671bf1bb06106 /include/mbgl/actor/actor.hpp | |
parent | f2ec6ae326bad79fea2b06a21151a2835522572a (diff) | |
parent | c62b0af24fc76b4bb2eb34100611dd3ee9ee5536 (diff) | |
download | qtlocation-mapboxgl-upstream/ls-android-readme-tweaks.tar.gz |
Merge branch 'master' into ls-android-readme-tweaksupstream/ls-android-readme-tweaks
Diffstat (limited to 'include/mbgl/actor/actor.hpp')
-rw-r--r-- | include/mbgl/actor/actor.hpp | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/include/mbgl/actor/actor.hpp b/include/mbgl/actor/actor.hpp index 810114c513..a0df19208e 100644 --- a/include/mbgl/actor/actor.hpp +++ b/include/mbgl/actor/actor.hpp @@ -6,6 +6,8 @@ #include <mbgl/util/noncopyable.hpp> #include <memory> +#include <future> +#include <type_traits> namespace mbgl { @@ -46,10 +48,18 @@ namespace mbgl { template <class Object> class Actor : public util::noncopyable { public: - template <class... Args> + + // Enabled for Objects with a constructor taking ActorRef<Object> as the first parameter + template <typename U = Object, class... Args, typename std::enable_if<std::is_constructible<U, ActorRef<U>, Args...>::value>::type * = nullptr> Actor(Scheduler& scheduler, Args&&... args_) - : mailbox(std::make_shared<Mailbox>(scheduler)), - object(self(), std::forward<Args>(args_)...) { + : mailbox(std::make_shared<Mailbox>(scheduler)), + object(self(), std::forward<Args>(args_)...) { + } + + // Enabled for plain Objects + template<typename U = Object, class... Args, typename std::enable_if<!std::is_constructible<U, ActorRef<U>, Args...>::value>::type * = nullptr> + Actor(Scheduler& scheduler, Args&& ... args_) + : mailbox(std::make_shared<Mailbox>(scheduler)), object(std::forward<Args>(args_)...) { } ~Actor() { @@ -61,6 +71,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); } |