diff options
-rw-r--r-- | include/mbgl/actor/actor.hpp | 18 | ||||
-rw-r--r-- | test/actor/actor.test.cpp | 31 |
2 files changed, 46 insertions, 3 deletions
diff --git a/include/mbgl/actor/actor.hpp b/include/mbgl/actor/actor.hpp index 00e1bb82f8..74d5a66285 100644 --- a/include/mbgl/actor/actor.hpp +++ b/include/mbgl/actor/actor.hpp @@ -7,6 +7,7 @@ #include <memory> #include <future> +#include <type_traits> namespace mbgl { @@ -47,10 +48,21 @@ 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<Object>, Args...>::value>::type...> + Actor(Scheduler& scheduler, 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<Object>, Args...>::value>::type...> Actor(Scheduler& scheduler, Args&&... args_) - : mailbox(std::make_shared<Mailbox>(scheduler)), - object(self(), std::forward<Args>(args_)...) { + : mailbox(std::make_shared<Mailbox>(scheduler)), + object(std::forward<Args>(args_)...) { } ~Actor() { diff --git a/test/actor/actor.test.cpp b/test/actor/actor.test.cpp index 2b4c83f566..39d7ff81f4 100644 --- a/test/actor/actor.test.cpp +++ b/test/actor/actor.test.cpp @@ -305,3 +305,34 @@ TEST(Actor, Ask) { ASSERT_EQ(std::future_status::ready, status); ASSERT_EQ(2, result.get()); } + +TEST(Actor, NoSelfActorRef) { + // Not all actors need a reference to self + + // Trivially constructable + struct Trivial {}; + + ThreadPool pool { 2 }; + Actor<Trivial> trivial(pool); + + + // With arguments + struct WithArguments { + std::promise<void> promise; + + WithArguments(std::promise<void> promise_) + : promise(std::move(promise_)) { + } + + void receive() { + promise.set_value(); + } + }; + + std::promise<void> promise; + auto future = promise.get_future(); + Actor<WithArguments> withArguments(pool, std::move(promise)); + + withArguments.invoke(&WithArguments::receive); + future.wait(); +} |