Commit 61aaac89 authored by Shayan Mohanty's avatar Shayan Mohanty Committed by Facebook Github Bot 8

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
parent 18aab924
......@@ -65,7 +65,6 @@ class FutureDAG : public std::enable_shared_from_this<FutureDAG> {
}
}
// 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<FutureDAG> {
std::vector<Node> nodes;
};
// Polymorphic functor implementation
template <typename T>
class FutureDAGFunctor {
public:
std::shared_ptr<FutureDAG> dag = FutureDAG::create();
T state;
std::vector<T> 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment