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

Unified Diff: base/callback_internal.h

Issue 8774032: Add Pass(), which implements move semantics, to scoped_ptr, scoped_array, and scoped_ptr_malloc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comments, make Passed() support temporaries, fix unbound argument forwarding, add more tests. Created 9 years 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
Index: base/callback_internal.h
diff --git a/base/callback_internal.h b/base/callback_internal.h
index 81c87c0aa749759d9dd987aa16fb0fd3c4e9bdef..0e3475fcf6a4526d4c0cb4fbc977442671b41e73 100644
--- a/base/callback_internal.h
+++ b/base/callback_internal.h
@@ -12,6 +12,7 @@
#include <stddef.h>
#include "base/base_export.h"
+#include "base/memory/scoped_ptr.h"
#include "base/memory/ref_counted.h"
namespace base {
@@ -130,6 +131,64 @@ struct CallbackParamTraits<T[]> {
typedef const T* StorageType;
};
+// Parameter traits for moveable-but-not-copyable scopers.
willchan no longer on Chromium 2011/12/07 16:24:34 s/moveable/movable/ everywhere in this file
awong 2011/12/08 21:04:10 Done.
+//
+// Callback<>/Bind() understands moveable-but-not-copyable semanatics where
+// the type cannot be copied but can still have its state destructively
+// transfered (aka. moved) to another instance of the same type by calling a
willchan no longer on Chromium 2011/12/07 16:24:34 transferred
awong 2011/12/08 21:04:10 Done.
+// helper function. When used with Bind(), this signfies transferrance of the
willchan no longer on Chromium 2011/12/07 16:24:34 signifies transferral
awong 2011/12/08 21:04:10 Done.
+// object's state to the target function.
+//
+// For these types, the ForwardType must not be a const reference, or a
+// reference. A const reference is inappropriate, and would break const
+// correctness, because we are implementing a destructive move. A non-const
+// reference cannot be used with temporaries which means the result of a
+// function or a cast would not be usable with Callback<> or Bind().
+//
+// TODO(ajwong): We might be able to use SFINAE to search for the existence of
+// a Pass() function in the type and avoid the whitelist in CallbackParamTraits
+// and CallbackForward.
+template <typename T>
+struct CallbackParamTraits<scoped_ptr<T> > {
+ typedef scoped_ptr<T> ForwardType;
willchan no longer on Chromium 2011/12/07 16:24:34 Can we get away with forward declaring these scope
awong 2011/12/08 21:04:10 Tried it, but ran into an issue with scoped_ptr_ma
+ typedef scoped_ptr<T> StorageType;
+};
+
+template <typename T>
+struct CallbackParamTraits<scoped_array<T> > {
+ typedef scoped_array<T> ForwardType;
+ typedef scoped_array<T> StorageType;
+};
+
+template <typename T>
+struct CallbackParamTraits<scoped_ptr_malloc<T> > {
+ typedef scoped_ptr_malloc<T> ForwardType;
+ typedef scoped_ptr_malloc<T> StorageType;
+};
+
+// CallbackForward() is a very limited simulation of C++11's std::forward()
+// used by the Callback/Bind system for a set of movable-but-not-copyable
+// types. It is needed because forwarding a moveable-but-not-copyable
+// argument to another function requires us to invoke the proper move
+// operator to create a rvalue version of the type. The supported types are
+// whitelisted below as overloads of the CallbackForward() function. The
+// default template compiles out to be a no-op.
+//
+// In C++11, std::forward would replace all uses of this function. However, it
+// is impossible to implement a general std::foward with C++11 due to a lack of
willchan no longer on Chromium 2011/12/07 19:25:24 s/foward/forward/
awong 2011/12/08 21:04:10 Done.
+// rvalue references.
+template <typename T>
+T& CallbackForward(T& t) { return t; }
+
+template <typename T>
+scoped_ptr<T> CallbackForward(scoped_ptr<T>& p) { return p.Pass(); }
+
+template <typename T>
+scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); }
+
+template <typename T>
+scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); }
+
} // namespace internal
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698