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

Unified Diff: third_party/WebKit/Source/wtf/Functional.h

Issue 1919723002: Introduce WTF::unretained() and WTF::crossThreadUnretained() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Kuroneko_4b_ACTA_WeakPtr
Patch Set: Reflect comment Created 4 years, 6 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 | « no previous file | third_party/WebKit/Source/wtf/FunctionalTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/Functional.h
diff --git a/third_party/WebKit/Source/wtf/Functional.h b/third_party/WebKit/Source/wtf/Functional.h
index 694963ae2bbf6c31b8e9394669225d86d7af404f..ccef48998e28c137736e6d4859ed5d0b6b597a07 100644
--- a/third_party/WebKit/Source/wtf/Functional.h
+++ b/third_party/WebKit/Source/wtf/Functional.h
@@ -98,6 +98,11 @@ namespace WTF {
// Obviously, if you create a functor this way, you shouldn't call the functor twice or more; after the second call,
// the passed argument may be invalid.
+enum FunctionThreadAffinity {
+ CrossThreadAffinity,
+ SameThreadAffinity
+};
+
template <typename T>
class PassedWrapper final {
public:
@@ -118,6 +123,30 @@ PassedWrapper<T> passed(T&& value)
return PassedWrapper<T>(std::move(value));
}
+template <typename T, FunctionThreadAffinity threadAffinity>
+class UnretainedWrapper final {
+public:
+ explicit UnretainedWrapper(T* ptr) : m_ptr(ptr) { }
+ T* value() const { return m_ptr; }
+
+private:
+ T* m_ptr;
+};
+
+template <typename T>
+UnretainedWrapper<T, SameThreadAffinity> unretained(T* value)
+{
+ static_assert(!WTF::IsGarbageCollectedType<T>::value, "unretained() + GCed type is forbidden");
+ return UnretainedWrapper<T, SameThreadAffinity>(value);
+}
+
+template <typename T>
+UnretainedWrapper<T, CrossThreadAffinity> crossThreadUnretained(T* value)
+{
+ static_assert(!WTF::IsGarbageCollectedType<T>::value, "crossThreadUnretained() + GCed type is forbidden");
+ return UnretainedWrapper<T, CrossThreadAffinity>(value);
+}
+
// A FunctionWrapper is a class template that can wrap a function pointer or a member function pointer and
// provide a unified interface for calling that function.
template <typename>
@@ -237,6 +266,14 @@ struct ParamStorageTraits<PassedWrapper<T>> {
static T unwrap(StorageType& value) { return value.moveOut(); }
};
+template <typename T, FunctionThreadAffinity threadAffinity>
+struct ParamStorageTraits<UnretainedWrapper<T, threadAffinity>> {
+ typedef UnretainedWrapper<T, threadAffinity> StorageType;
+
+ static StorageType wrap(const UnretainedWrapper<T, threadAffinity>& value) { return value; }
+ static T* unwrap(const StorageType& value) { return value.value(); }
+};
+
template<typename T, bool isGarbageCollected> struct PointerParamStorageTraits;
template<typename T>
@@ -262,11 +299,6 @@ struct ParamStorageTraits<T*> : public PointerParamStorageTraits<T*, IsGarbageCo
STATIC_ONLY(ParamStorageTraits);
};
-enum FunctionThreadAffinity {
- CrossThreadAffinity,
- SameThreadAffinity
-};
-
template<typename, FunctionThreadAffinity threadAffinity = SameThreadAffinity>
class Function;
@@ -377,6 +409,8 @@ typedef Function<void(), CrossThreadAffinity> CrossThreadClosure;
} // namespace WTF
using WTF::passed;
+using WTF::unretained;
+using WTF::crossThreadUnretained;
using WTF::Function;
using WTF::bind;
using WTF::SameThreadClosure;
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/FunctionalTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698