| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 template <typename T> | 64 template <typename T> |
| 65 struct CrossThreadCopierPassThrough { | 65 struct CrossThreadCopierPassThrough { |
| 66 STATIC_ONLY(CrossThreadCopierPassThrough); | 66 STATIC_ONLY(CrossThreadCopierPassThrough); |
| 67 typedef T Type; | 67 typedef T Type; |
| 68 static Type copy(const T& parameter) | 68 static Type copy(const T& parameter) |
| 69 { | 69 { |
| 70 return parameter; | 70 return parameter; |
| 71 } | 71 } |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 template <typename T, bool isArithmeticOrEnum, bool isThreadSafeRefCounted> | 74 template <typename T, bool isArithmeticOrEnum> |
| 75 struct CrossThreadCopierBase; | 75 struct CrossThreadCopierBase; |
| 76 | 76 |
| 77 // Arithmetic values (integers or floats) and enums can be safely copied. | 77 // Arithmetic values (integers or floats) and enums can be safely copied. |
| 78 template <typename T, bool isThreadSafeRefCounted> | 78 template <typename T> |
| 79 struct CrossThreadCopierBase<T, true, isThreadSafeRefCounted> : public CrossThre
adCopierPassThrough<T> { | 79 struct CrossThreadCopierBase<T, true> : public CrossThreadCopierPassThrough<T> { |
| 80 STATIC_ONLY(CrossThreadCopierBase); | 80 STATIC_ONLY(CrossThreadCopierBase); |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 // Custom copy method for ThreadSafeRefCounted. | |
| 84 template <typename T> | 83 template <typename T> |
| 85 struct CrossThreadCopierBase<T, false, true> { | 84 struct CrossThreadCopier : public CrossThreadCopierBase<T, std::is_arithmetic<T>
::value || std::is_enum<T>::value> { |
| 86 STATIC_ONLY(CrossThreadCopierBase); | |
| 87 typedef typename WTF::RemoveTemplate<T, RefPtr>::Type TypeWithoutRefPtr; | |
| 88 typedef typename WTF::RemoveTemplate<TypeWithoutRefPtr, PassRefPtr>::Type Ty
peWithoutPassRefPtr; | |
| 89 typedef typename std::remove_pointer<TypeWithoutPassRefPtr>::type RefCounted
Type; | |
| 90 | |
| 91 // Verify that only one of the above did a change. | |
| 92 static_assert((std::is_same<RefPtr<RefCountedType>, T>::value | |
| 93 || std::is_same<PassRefPtr<RefCountedType>, T>::value | |
| 94 || std::is_same<RefCountedType*, T>::value), | |
| 95 "only one type modification should be allowed"); | |
| 96 | |
| 97 typedef PassRefPtr<RefCountedType> Type; | |
| 98 static Type copy(const T& refPtr) | |
| 99 { | |
| 100 return refPtr; | |
| 101 } | |
| 102 }; | |
| 103 | |
| 104 template <typename T> | |
| 105 struct CrossThreadCopier : public CrossThreadCopierBase< | |
| 106 T, | |
| 107 std::is_arithmetic<T>::value || std::is_enum<T>::value, | |
| 108 WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, RefPtr>::Type, Thr
eadSafeRefCounted>::value | |
| 109 || WTF::IsSubclassOfTemplate<typename std::remove_pointer<T>::type, ThreadSa
feRefCounted>::value | |
| 110 || WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, PassRefPtr>::Ty
pe, ThreadSafeRefCounted>::value | |
| 111 || std::is_base_of<SkRefCnt, typename WTF::RemoveTemplate<T, RefPtr>::Type>:
:value | |
| 112 || std::is_base_of<SkRefCnt, typename std::remove_pointer<T>::type>::value | |
| 113 || std::is_base_of<SkRefCnt, typename WTF::RemoveTemplate<T, PassRefPtr>::Ty
pe>::value> { | |
| 114 STATIC_ONLY(CrossThreadCopier); | 85 STATIC_ONLY(CrossThreadCopier); |
| 115 }; | 86 }; |
| 116 | 87 |
| 117 // CrossThreadCopier specializations follow. | 88 // CrossThreadCopier specializations follow. |
| 89 template <typename T> |
| 90 struct CrossThreadCopier<PassRefPtr<T>> : public CrossThreadCopierPassThrough<Pa
ssRefPtr<T>> { |
| 91 STATIC_ONLY(CrossThreadCopier); |
| 92 static_assert(WTF::IsSubclassOfTemplate<T, ThreadSafeRefCounted>::value || s
td::is_base_of<SkRefCnt, T>::value, "PassRefPtr<T> can be passed across threads
only if T is ThreadSafeRefCounted or SkRefCnt."); |
| 93 }; |
| 94 template <typename T> |
| 95 struct CrossThreadCopier<RefPtr<T>> : public CrossThreadCopierPassThrough<RefPtr
<T>> { |
| 96 STATIC_ONLY(CrossThreadCopier); |
| 97 static_assert(WTF::IsSubclassOfTemplate<T, ThreadSafeRefCounted>::value || s
td::is_base_of<SkRefCnt, T>::value, "RefPtr<T> can be passed across threads only
if T is ThreadSafeRefCounted or SkRefCnt."); |
| 98 }; |
| 118 | 99 |
| 119 // nullptr_t can be passed through without any changes. | 100 // nullptr_t can be passed through without any changes. |
| 120 template <> | 101 template <> |
| 121 struct CrossThreadCopier<std::nullptr_t> : public CrossThreadCopierPassThrough<s
td::nullptr_t> { | 102 struct CrossThreadCopier<std::nullptr_t> : public CrossThreadCopierPassThrough<s
td::nullptr_t> { |
| 122 STATIC_ONLY(CrossThreadCopier); | 103 STATIC_ONLY(CrossThreadCopier); |
| 123 }; | 104 }; |
| 124 | 105 |
| 125 // To allow a type to be passed across threads using its copy constructor, add a
forward declaration of the type and | 106 // To allow a type to be passed across threads using its copy constructor, add a
forward declaration of the type and |
| 126 // provide a specialization of CrossThreadCopier<T> in this file, like IntRect b
elow. | 107 // provide a specialization of CrossThreadCopier<T> in this file, like IntRect b
elow. |
| 127 template <> | 108 template <> |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 typedef T* Type; | 194 typedef T* Type; |
| 214 static Type copy(const Member<T>& ptr) | 195 static Type copy(const Member<T>& ptr) |
| 215 { | 196 { |
| 216 return ptr; | 197 return ptr; |
| 217 } | 198 } |
| 218 }; | 199 }; |
| 219 | 200 |
| 220 } // namespace blink | 201 } // namespace blink |
| 221 | 202 |
| 222 #endif // CrossThreadCopier_h | 203 #endif // CrossThreadCopier_h |
| OLD | NEW |