Polymorphic Functor implementation in Folly::FutureDAG
authorShayan Mohanty <shayanjm@fb.com>
Thu, 11 Aug 2016 02:15:36 +0000 (19:15 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Thu, 11 Aug 2016 02:23:27 +0000 (19:23 -0700)
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

index a825e617aa10f444b7a2c00a7728c79b997bbf75..42391336eccfef95a189df69b25cb206c8236c59 100644 (file)
@@ -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