From 68f470fdda4e31d7704fba3e41bb2f899db39541 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Tue, 8 Aug 2017 10:42:44 +0300 Subject: [core] make actor self reference optional - again - uses a different method of constructor selection that also works on Apple clang < 8.2. --- include/mbgl/actor/actor.hpp | 9 +++++++-- test/actor/actor.test.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/mbgl/actor/actor.hpp b/include/mbgl/actor/actor.hpp index 6610421eb5..a0df19208e 100644 --- a/include/mbgl/actor/actor.hpp +++ b/include/mbgl/actor/actor.hpp @@ -50,13 +50,18 @@ class Actor : public util::noncopyable { public: // Enabled for Objects with a constructor taking ActorRef as the first parameter - template , Args...>::value>::type...> + template , Args...>::value>::type * = nullptr> Actor(Scheduler& scheduler, Args&&... args_) : mailbox(std::make_shared(scheduler)), object(self(), std::forward(args_)...) { } + // Enabled for plain Objects + template, Args...>::value>::type * = nullptr> + Actor(Scheduler& scheduler, Args&& ... args_) + : mailbox(std::make_shared(scheduler)), object(std::forward(args_)...) { + } + ~Actor() { mailbox->close(); } diff --git a/test/actor/actor.test.cpp b/test/actor/actor.test.cpp index 2b4c83f566..4c7fc3666d 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(pool); + + + // With arguments + struct WithArguments { + std::promise promise; + + WithArguments(std::promise promise_) + : promise(std::move(promise_)) { + } + + void receive() { + promise.set_value(); + } + }; + + std::promise promise; + auto future = promise.get_future(); + Actor withArguments(pool, std::move(promise)); + + withArguments.invoke(&WithArguments::receive); + future.wait(); +} -- cgit v1.2.1