From de3c22b0ee81613462b5f48d85d8a692c6e53053 Mon Sep 17 00:00:00 2001 From: Samuel Just Date: Tue, 4 Oct 2016 13:27:31 -0700 Subject: [PATCH] Context: add [Gen]LambdaContext and some related helpers Signed-off-by: Samuel Just --- src/include/Context.h | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/include/Context.h b/src/include/Context.h index 48c34743b8f..e969966258c 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 */ -- 2.39.5