summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-17 18:14:47 +0000
committerjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-17 18:14:47 +0000
commit5b6c1a80d09f250f66070dba8dd8a05d38e74db2 (patch)
tree8bff607411dc0400581dd304c911b59ff80737df /base
parent04edc039a2f882ddc551265e51447cbd49e91211 (diff)
downloadchromium_src-5b6c1a80d09f250f66070dba8dd8a05d38e74db2.zip
chromium_src-5b6c1a80d09f250f66070dba8dd8a05d38e74db2.tar.gz
chromium_src-5b6c1a80d09f250f66070dba8dd8a05d38e74db2.tar.bz2
Add a CallbackWithReturnValue as a convenient method of calling callback functions with no arguments and a return value. This is useful in calling all of the getters we have in the code, instead of creating wrapper functions that take a pointer argument.
Review URL: http://codereview.chromium.org/155630 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20975 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp1
-rw-r--r--base/task.h37
-rw-r--r--base/task_unittest.cc40
3 files changed, 78 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp
index 77cd484..0937786 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -655,6 +655,7 @@
'sys_info_unittest.cc',
'sys_string_conversions_unittest.cc',
'system_monitor_unittest.cc',
+ 'task_unittest.cc',
'thread_collision_warner_unittest.cc',
'thread_local_storage_unittest.cc',
'thread_local_unittest.cc',
diff --git a/base/task.h b/base/task.h
index 36bd43f..8b9ed07 100644
--- a/base/task.h
+++ b/base/task.h
@@ -503,6 +503,13 @@ inline CancelableTask* NewRunnableFunction(Function function,
// CallbackRunner<Tuple2<int, string> >* callback =
// NewCallback(obj, &Object::DoStuff);
// callback->RunWithParams(MakeTuple(5, string("hello")));
+//
+// There is also a 0-args version that returns a value. Example:
+// int Object::GetNextInt();
+// CallbackWithReturnValue<int>::Type* callback =
+// NewCallbackWithReturnValue(obj, &Object::GetNextInt);
+// int next_int = callback->Run();
+// delete callback;
// Base for all Callbacks that handles storage of the pointers.
template <class T, typename Method>
@@ -666,4 +673,34 @@ class UnboundMethod {
Params p_;
};
+// Return value implementation with no args.
+template <typename ReturnValue>
+struct CallbackWithReturnValue {
+ class Type {
+ public:
+ virtual ReturnValue Run() = 0;
+ };
+};
+
+template <class T, typename Method, typename ReturnValue>
+class CallbackWithReturnValueImpl
+ : public CallbackStorage<T, Method>,
+ public CallbackWithReturnValue<ReturnValue>::Type {
+ public:
+ CallbackWithReturnValueImpl(T* obj, Method meth)
+ : CallbackStorage<T, Method>(obj, meth) { }
+
+ virtual ReturnValue Run() {
+ return (this->obj_->*(this->meth_))();
+ }
+};
+
+template <class T, typename ReturnValue>
+typename CallbackWithReturnValue<ReturnValue>::Type*
+NewCallbackWithReturnValue(T* object, ReturnValue (T::*method)()) {
+ return new CallbackWithReturnValueImpl<T, ReturnValue (T::*)(), ReturnValue>(
+ object, method);
+}
+
+
#endif // BASE_TASK_H_
diff --git a/base/task_unittest.cc b/base/task_unittest.cc
new file mode 100644
index 0000000..1fa7c9d
--- /dev/null
+++ b/base/task_unittest.cc
@@ -0,0 +1,40 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/scoped_ptr.h"
+#include "base/task.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class HelperObject {
+ public:
+ HelperObject() : next_number_(0) { }
+ int GetNextNumber() { return ++next_number_; }
+ void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
+
+ private:
+ int next_number_;
+};
+
+} // namespace
+
+TEST(Task, OneArg) {
+ HelperObject obj;
+ scoped_ptr<Callback1<int*>::Type> callback(
+ NewCallback(&obj, &HelperObject::GetNextNumberArg));
+
+ int number = 0;
+ callback->Run(&number);
+ EXPECT_EQ(number, 1);
+}
+
+TEST(Task, ReturnValue) {
+ HelperObject obj;
+ scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
+ NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
+
+ EXPECT_EQ(callback->Run(), 1);
+}