]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
Context: add [Gen]LambdaContext and some related helpers
authorSamuel Just <sjust@redhat.com>
Tue, 4 Oct 2016 20:27:31 +0000 (13:27 -0700)
committerSamuel Just <sjust@redhat.com>
Thu, 17 Nov 2016 18:40:16 +0000 (10:40 -0800)
Signed-off-by: Samuel Just <sjust@redhat.com>
src/include/Context.h

index 48c34743b8fde524b465cb5b798b5f2963e9247d..e969966258c3f366413ea40795a512b0c1181168 100644 (file)
@@ -21,6 +21,7 @@
 #include <boost/function.hpp>
 #include <list>
 #include <set>
+#include <memory>
 
 #include "include/assert.h"
 #include "include/memory.h"
@@ -41,12 +42,17 @@ class GenContext {
  public:
   GenContext() {}
   virtual ~GenContext() {}       // we want a virtual destructor!!!
-  virtual void complete(T t) {
-    finish(t);
+
+  template <typename C>
+  void complete(C &&t) {
+    finish(std::forward<C>(t));
     delete this;
   }
 };
 
+template <typename T>
+using GenContextURef = std::unique_ptr<GenContext<T> >;
+
 /*
  * Context - abstract callback class
  */
@@ -76,6 +82,10 @@ public:
   ContainerContext(T &obj) : obj(obj) {}
   void finish(int r) {}
 };
+template <typename T>
+ContainerContext<T> *make_container_context(T &&t) {
+  return new ContainerContext<T>(std::forward<T>(t));
+}
 
 template <class T>
 struct Wrapper : public Context {
@@ -97,6 +107,32 @@ struct RunOnDelete {
 };
 typedef ceph::shared_ptr<RunOnDelete> RunOnDeleteRef;
 
+template <typename T>
+struct LambdaContext : public Context {
+  T t;
+  LambdaContext(T &&t) : t(std::forward<T>(t)) {}
+  void finish(int) {
+    t();
+  }
+};
+template <typename T>
+LambdaContext<T> *make_lambda_context(T &&t) {
+  return new LambdaContext<T>(std::move(t));
+}
+
+template <typename F, typename T>
+struct LambdaGenContext : GenContext<T> {
+  F f;
+  LambdaGenContext(F &&f) : f(std::forward<F>(f)) {}
+  void finish(T t) {
+    f(std::forward<T>(t));
+  }
+};
+template <typename T, typename F>
+GenContextURef<T> make_gen_lambda_context(F &&f) {
+  return GenContextURef<T>(new LambdaGenContext<F, T>(std::move(f)));
+}
+
 /*
  * finish and destroy a list of Contexts
  */