From: Samuel Just Date: Tue, 4 Oct 2016 20:27:31 +0000 (-0700) Subject: Context: add [Gen]LambdaContext and some related helpers X-Git-Tag: v11.1.0~245^2~35 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=de3c22b0ee81613462b5f48d85d8a692c6e53053;p=ceph.git Context: add [Gen]LambdaContext and some related helpers Signed-off-by: Samuel Just --- diff --git a/src/include/Context.h b/src/include/Context.h index 48c34743b8fd..e969966258c3 100644 --- a/src/include/Context.h +++ b/src/include/Context.h @@ -21,6 +21,7 @@ #include #include #include +#include #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 + void complete(C &&t) { + finish(std::forward(t)); delete this; } }; +template +using GenContextURef = std::unique_ptr >; + /* * Context - abstract callback class */ @@ -76,6 +82,10 @@ public: ContainerContext(T &obj) : obj(obj) {} void finish(int r) {} }; +template +ContainerContext *make_container_context(T &&t) { + return new ContainerContext(std::forward(t)); +} template struct Wrapper : public Context { @@ -97,6 +107,32 @@ struct RunOnDelete { }; typedef ceph::shared_ptr RunOnDeleteRef; +template +struct LambdaContext : public Context { + T t; + LambdaContext(T &&t) : t(std::forward(t)) {} + void finish(int) { + t(); + } +}; +template +LambdaContext *make_lambda_context(T &&t) { + return new LambdaContext(std::move(t)); +} + +template +struct LambdaGenContext : GenContext { + F f; + LambdaGenContext(F &&f) : f(std::forward(f)) {} + void finish(T t) { + f(std::forward(t)); + } +}; +template +GenContextURef make_gen_lambda_context(F &&f) { + return GenContextURef(new LambdaGenContext(std::move(f))); +} + /* * finish and destroy a list of Contexts */