summaryrefslogtreecommitdiffstats
path: root/base/callback_helpers.h
diff options
context:
space:
mode:
authorajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-15 01:27:38 +0000
committerajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-15 01:27:38 +0000
commitb38d3578e53d0a7f441c6858334a2d9f08e5c024 (patch)
tree917a1d893c40fd10408e172b1a66cba692a0fedc /base/callback_helpers.h
parentc41fe6697e38057138cbe33332d9882e0d7d9b4b (diff)
downloadchromium_src-b38d3578e53d0a7f441c6858334a2d9f08e5c024.zip
chromium_src-b38d3578e53d0a7f441c6858334a2d9f08e5c024.tar.gz
chromium_src-b38d3578e53d0a7f441c6858334a2d9f08e5c024.tar.bz2
Unified callback system based on tr1::function/tr1::bind and Google's internal callback code.
This callback system allows for creation of functors for normal functions, methods, and const methods. It is a superset of the functionality of NewRunnableMethod, NewRunnableFunction, NewCallback, and CreateFunctor. We support partial binding of function arguments, and also specification of refcounting semantics by wrapping a target object in a wrapper object. BUG=35223 TEST=none Review URL: http://codereview.chromium.org/6109007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74904 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/callback_helpers.h')
-rw-r--r--base/callback_helpers.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/base/callback_helpers.h b/base/callback_helpers.h
new file mode 100644
index 0000000..86b0df1
--- /dev/null
+++ b/base/callback_helpers.h
@@ -0,0 +1,55 @@
+// Copyright (c) 2011 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.
+
+// This file contains utility functions and classes that help the
+// implementation, and management of the Callback objects.
+
+#ifndef BASE_CALLBACK_HELPERS_H_
+#define BASE_CALLBACK_HELPERS_H_
+#pragma once
+
+#include "base/ref_counted.h"
+
+namespace base {
+namespace internal {
+
+// InvokerStorageBase is used to provide an opaque handle that the Callback
+// class can use to represent a function object with bound arguments. It
+// behaves as an existential type that is used by a corresponding
+// DoInvoke function to perform the function execution. This allows
+// us to shield the Callback class from the types of the bound argument via
+// "type erasure."
+class InvokerStorageBase : public RefCountedThreadSafe<InvokerStorageBase> {
+ protected:
+ friend class RefCountedThreadSafe<InvokerStorageBase>;
+ virtual ~InvokerStorageBase() {}
+};
+
+// This structure exists purely to pass the returned |invoker_storage_| from
+// Bind() to Callback while avoiding an extra AddRef/Release() pair.
+//
+// To do this, the constructor of Callback<> must take a const-ref. The
+// reference must be to a const object otherwise the compiler will emit a
+// warning about taking a reference to a temporary.
+//
+// Unfortunately, this means that the internal |invoker_storage_| field must
+// be made mutable.
+template <typename T>
+struct InvokerStorageHolder {
+ explicit InvokerStorageHolder(T* invoker_storage)
+ : invoker_storage_(invoker_storage) {
+ }
+
+ mutable scoped_refptr<InvokerStorageBase> invoker_storage_;
+};
+
+template <typename T>
+InvokerStorageHolder<T> MakeInvokerStorageHolder(T* o) {
+ return InvokerStorageHolder<T>(o);
+}
+
+} // namespace internal
+} // namespace base
+
+#endif // BASE_CALLBACK_HELPERS_H_