summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2017-09-26 23:40:55 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2017-09-26 23:40:55 +0200
commit3a2021cf359ba23c01dacd70f7c635c7243905c9 (patch)
treeadf9b023955f05ca68b8734ff653dccf60c93040 /vespalib
parent911cc20b5253e8c14d4ab534cb46fd40470db9f0 (diff)
Add test for makeLambdaTask
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/executor/executor_test.cpp33
-rw-r--r--vespalib/src/vespa/vespalib/util/lambdatask.h29
2 files changed, 40 insertions, 22 deletions
diff --git a/vespalib/src/tests/executor/executor_test.cpp b/vespalib/src/tests/executor/executor_test.cpp
index c508417e1c2..9015391beaa 100644
--- a/vespalib/src/tests/executor/executor_test.cpp
+++ b/vespalib/src/tests/executor/executor_test.cpp
@@ -2,30 +2,13 @@
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/closuretask.h>
+#include <vespa/vespalib/util/lambdatask.h>
using namespace vespalib;
-namespace {
-
-class Test : public vespalib::TestApp {
- void requireThatClosuresCanBeWrappedInATask();
-
-public:
- int Main() override;
-};
-
-int
-Test::Main()
-{
- TEST_INIT("executor_test");
-
- TEST_DO(requireThatClosuresCanBeWrappedInATask());
-
- TEST_DONE();
-}
-
void setBool(bool *b) { *b = true; }
-void Test::requireThatClosuresCanBeWrappedInATask() {
+
+TEST("require that closures can be wrapped as tasks") {
bool called = false;
Executor::Task::UP task = makeTask(makeClosure(setBool, &called));
EXPECT_TRUE(!called);
@@ -33,6 +16,12 @@ void Test::requireThatClosuresCanBeWrappedInATask() {
EXPECT_TRUE(called);
}
-} // namespace
+TEST("require that lambdas can be wrapped as tasks") {
+ bool called = false;
+ Executor::Task::UP task = makeLambdaTask([&called]() { called = true; });
+ EXPECT_TRUE(!called);
+ task->run();
+ EXPECT_TRUE(called);
+}
-TEST_APPHOOK(Test);
+TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/util/lambdatask.h b/vespalib/src/vespa/vespalib/util/lambdatask.h
new file mode 100644
index 00000000000..35543407aaa
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/lambdatask.h
@@ -0,0 +1,29 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#pragma once
+
+#include "executor.h"
+
+namespace vespalib {
+
+template <class FunctionType>
+class LambdaTask : public vespalib::Executor::Task {
+ FunctionType _func;
+
+public:
+ LambdaTask(const FunctionType &func) : _func(func) {}
+ LambdaTask(FunctionType &&func) : _func(std::move(func)) {}
+ LambdaTask(const LambdaTask &) = delete;
+ LambdaTask & operator = (const LambdaTask &) = delete;
+ ~LambdaTask() {}
+ void run() override { _func(); }
+};
+
+template <class FunctionType>
+vespalib::Executor::Task::UP
+makeLambdaTask(FunctionType &&function)
+{
+ return std::make_unique<LambdaTask<std::decay_t<FunctionType>>>
+ (std::forward<FunctionType>(function));
+}
+
+}