Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(618)

Unified Diff: base/callback_helpers.h

Issue 6109007: Unified callback system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: Address Will's comments. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/callback.h.pump ('k') | base/callback_old.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/callback_helpers.h
diff --git a/base/callback_helpers.h b/base/callback_helpers.h
new file mode 100644
index 0000000000000000000000000000000000000000..86b0df1eefeba6fe6d63036d9360f4f1551965de
--- /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_
« no previous file with comments | « base/callback.h.pump ('k') | base/callback_old.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698