From 61aaac8908fc518ee1d5f8cfe2ff316fd9ccfd59 Mon Sep 17 00:00:00 2001 From: Shayan Mohanty Date: Wed, 10 Aug 2016 19:15:36 -0700 Subject: [PATCH] Polymorphic Functor implementation in Folly::FutureDAG Summary: Implements a polymorphic functor for FutureDAGs. In order for FutureDAGs to be stateful they must be wrapped by a class of some sort. This is a really common pattern which we've been using in Gossit (and further - across the RedWood stack) in order to maintain state, and we feel it's generalized enough to be useful elsewhere. `state` is an instance of the type declared in the template, and the exec* methods wrap go().get() so client-side implementations only have to touch the functor after construction in order to drive their DAGs. Reviewed By: tjkswaine Differential Revision: D3685651 fbshipit-source-id: 81169aefcff13ac8cc6cbb6bef6d90047732ad8a --- folly/experimental/FutureDAG.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/folly/experimental/FutureDAG.h b/folly/experimental/FutureDAG.h index a825e617..42391336 100644 --- a/folly/experimental/FutureDAG.h +++ b/folly/experimental/FutureDAG.h @@ -65,7 +65,6 @@ class FutureDAG : public std::enable_shared_from_this { } } - // Faster to just create a new vector with the element in it? nodes.erase(nodes.begin(), nodes.begin() + source_node); nodes.erase(nodes.begin() + 1, nodes.end()); nodes[0].hasDependents = false; @@ -197,4 +196,28 @@ class FutureDAG : public std::enable_shared_from_this { std::vector nodes; }; +// Polymorphic functor implementation +template +class FutureDAGFunctor { + public: + std::shared_ptr dag = FutureDAG::create(); + T state; + std::vector dep_states; + T result() { + return state; + }; + // execReset() runs DAG & clears all nodes except for source + void execReset() { + this->dag->go().get(); + this->dag->reset(); + }; + void exec() { + this->dag->go().get(); + }; + virtual void operator()(){}; + explicit FutureDAGFunctor(T init_val) : state(init_val) {} + FutureDAGFunctor() : state() {} + virtual ~FutureDAGFunctor(){}; +}; + } // folly -- 2.34.1