diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-09-18 13:39:47 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-09-22 23:33:56 +0300 |
commit | 8b9794b1a7276d8ea2957d5aed7169fc0b40e25d (patch) | |
tree | e42da349f705a15dca3521e5535b558a79866659 | |
parent | c17be060fd910dbcaf7334356743421ac6936e7e (diff) | |
download | qtlocation-mapboxgl-8b9794b1a7276d8ea2957d5aed7169fc0b40e25d.tar.gz |
[core] Actor/ActorRef - ask calls to void methods
- allows to wait for execution of void methods as well as non-void methods
-rw-r--r-- | include/mbgl/actor/message.hpp | 26 | ||||
-rw-r--r-- | test/actor/actor.test.cpp | 22 | ||||
-rw-r--r-- | test/actor/actor_ref.test.cpp | 22 |
3 files changed, 70 insertions, 0 deletions
diff --git a/include/mbgl/actor/message.hpp b/include/mbgl/actor/message.hpp index 406de425d4..0a20993352 100644 --- a/include/mbgl/actor/message.hpp +++ b/include/mbgl/actor/message.hpp @@ -64,6 +64,32 @@ public: std::promise<ResultType> promise; }; +template <class Object, class MemberFn, class ArgsTuple> +class AskMessageImpl<void, Object, MemberFn, ArgsTuple> : public Message { +public: + AskMessageImpl(std::promise<void> promise_, Object& object_, MemberFn memberFn_, ArgsTuple argsTuple_) + : object(object_), + memberFn(memberFn_), + argsTuple(std::move(argsTuple_)), + promise(std::move(promise_)) { + } + + void operator()() override { + ask(std::make_index_sequence<std::tuple_size<ArgsTuple>::value>()); + promise.set_value(); + } + + template <std::size_t... I> + void ask(std::index_sequence<I...>) { + (object.*memberFn)(std::move(std::get<I>(argsTuple))...); + } + + Object& object; + MemberFn memberFn; + ArgsTuple argsTuple; + std::promise<void> promise; +}; + namespace actor { template <class Object, class MemberFn, class... Args> diff --git a/test/actor/actor.test.cpp b/test/actor/actor.test.cpp index 4c7fc3666d..967dc152d9 100644 --- a/test/actor/actor.test.cpp +++ b/test/actor/actor.test.cpp @@ -306,6 +306,28 @@ TEST(Actor, Ask) { ASSERT_EQ(2, result.get()); } +TEST(Actor, AskVoid) { + // Ask waits for void methods + + struct Test { + bool& executed; + + Test(bool& executed_) : executed(executed_) { + } + + void doIt() { + executed = true; + } + }; + + ThreadPool pool { 1 }; + bool executed = false; + Actor<Test> actor(pool, executed); + + actor.ask(&Test::doIt).get(); + EXPECT_TRUE(executed); +} + TEST(Actor, NoSelfActorRef) { // Not all actors need a reference to self diff --git a/test/actor/actor_ref.test.cpp b/test/actor/actor_ref.test.cpp index 52b0de295b..20aa1c35c1 100644 --- a/test/actor/actor_ref.test.cpp +++ b/test/actor/actor_ref.test.cpp @@ -62,6 +62,28 @@ TEST(ActorRef, Ask) { EXPECT_EQ(30, ref.ask(&Test::echo, 30).get()); } +TEST(ActorRef, AskVoid) { + // Ask waits for void methods + + struct Test { + bool& executed; + + Test(bool& executed_) : executed(executed_) { + } + + void doIt() { + executed = true; + } + }; + + ThreadPool pool { 1 }; + bool executed = false; + Actor<Test> actor(pool, executed); + ActorRef<Test> ref = actor.self(); + + ref.ask(&Test::doIt).get(); + EXPECT_TRUE(executed); +} TEST(ActorRef, AskOnDestroyedActor) { // Tests behavior when calling ask() after the |