| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple 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 | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // because there needs to be at least one intermediary function call taking an | 80 // because there needs to be at least one intermediary function call taking an |
| 81 // argument of type "Argument" (i.e. passed by value). The former case does not | 81 // argument of type "Argument" (i.e. passed by value). The former case does not |
| 82 // require any move constructions inbetween. | 82 // require any move constructions inbetween. |
| 83 // | 83 // |
| 84 // For bound parameters (arguments supplied on the creation of a functor), you | 84 // For bound parameters (arguments supplied on the creation of a functor), you |
| 85 // can move your argument into the internal storage of the functor by supplying | 85 // can move your argument into the internal storage of the functor by supplying |
| 86 // an rvalue to that argument (this is done in wrap() of ParamStorageTraits). | 86 // an rvalue to that argument (this is done in wrap() of ParamStorageTraits). |
| 87 // However, to make the functor be able to get called multiple times, the | 87 // However, to make the functor be able to get called multiple times, the |
| 88 // stored object does not get moved out automatically when the underlying | 88 // stored object does not get moved out automatically when the underlying |
| 89 // function is actually invoked. If you want to make an argument "auto-passed", | 89 // function is actually invoked. If you want to make an argument "auto-passed", |
| 90 // you can do so by wrapping your bound argument with passed() function, as | 90 // you can do so by wrapping your bound argument with WTF::passed() function, as |
| 91 // shown below: | 91 // shown below: |
| 92 // | 92 // |
| 93 // void yourFunction(Argument argument) | 93 // void yourFunction(Argument argument) |
| 94 // { | 94 // { |
| 95 // // |argument| is passed from the internal storage of functor. | 95 // // |argument| is passed from the internal storage of functor. |
| 96 // ... | 96 // ... |
| 97 // } | 97 // } |
| 98 // | 98 // |
| 99 // ... | 99 // ... |
| 100 // std::unique_ptr<Function<void()>> functor = bind(yourFunction, | 100 // std::unique_ptr<Function<void()>> functor = bind(yourFunction, |
| 101 // passed(Argument())); | 101 // WTF::passed(Argument())); |
| 102 // ... | 102 // ... |
| 103 // (*functor)(); | 103 // (*functor)(); |
| 104 // | 104 // |
| 105 // The underlying function must receive the argument wrapped by passed() by | 105 // The underlying function must receive the argument wrapped by WTF::passed() by |
| 106 // rvalue reference or by value. | 106 // rvalue reference or by value. |
| 107 // | 107 // |
| 108 // Obviously, if you create a functor this way, you shouldn't call the functor | 108 // Obviously, if you create a functor this way, you shouldn't call the functor |
| 109 // twice or more; after the second call, the passed argument may be invalid. | 109 // twice or more; after the second call, the passed argument may be invalid. |
| 110 | 110 |
| 111 enum FunctionThreadAffinity { CrossThreadAffinity, SameThreadAffinity }; | 111 enum FunctionThreadAffinity { CrossThreadAffinity, SameThreadAffinity }; |
| 112 | 112 |
| 113 template <typename T> | 113 template <typename T> |
| 114 class PassedWrapper final { | 114 class PassedWrapper final { |
| 115 public: | 115 public: |
| 116 explicit PassedWrapper(T&& scoper) : m_scoper(std::move(scoper)) {} | 116 explicit PassedWrapper(T&& scoper) : m_scoper(std::move(scoper)) {} |
| 117 PassedWrapper(PassedWrapper&& other) : m_scoper(std::move(other.m_scoper)) {} | 117 PassedWrapper(PassedWrapper&& other) : m_scoper(std::move(other.m_scoper)) {} |
| 118 T moveOut() const { return std::move(m_scoper); } | 118 T moveOut() const { return std::move(m_scoper); } |
| 119 | 119 |
| 120 private: | 120 private: |
| 121 mutable T m_scoper; | 121 mutable T m_scoper; |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 template <typename T> | 124 template <typename T> |
| 125 PassedWrapper<T> passed(T&& value) { | 125 PassedWrapper<T> passed(T&& value) { |
| 126 static_assert(!std::is_reference<T>::value, | 126 static_assert( |
| 127 "You must pass an rvalue to passed() so it can be moved. Add " | 127 !std::is_reference<T>::value, |
| 128 "std::move() if necessary."); | 128 "You must pass an rvalue to WTF::passed() so it can be moved. Add " |
| 129 "std::move() if necessary."); |
| 129 static_assert(!std::is_const<T>::value, | 130 static_assert(!std::is_const<T>::value, |
| 130 "|value| must not be const so it can be moved."); | 131 "|value| must not be const so it can be moved."); |
| 131 return PassedWrapper<T>(std::move(value)); | 132 return PassedWrapper<T>(std::move(value)); |
| 132 } | 133 } |
| 133 | 134 |
| 134 template <typename T, FunctionThreadAffinity threadAffinity> | 135 template <typename T, FunctionThreadAffinity threadAffinity> |
| 135 class UnretainedWrapper final { | 136 class UnretainedWrapper final { |
| 136 public: | 137 public: |
| 137 explicit UnretainedWrapper(T* ptr) : m_ptr(ptr) {} | 138 explicit UnretainedWrapper(T* ptr) : m_ptr(ptr) {} |
| 138 T* value() const { return m_ptr; } | 139 T* value() const { return m_ptr; } |
| 139 | 140 |
| 140 private: | 141 private: |
| 141 T* m_ptr; | 142 T* m_ptr; |
| 142 }; | 143 }; |
| 143 | 144 |
| 144 template <typename T> | 145 template <typename T> |
| 145 UnretainedWrapper<T, SameThreadAffinity> unretained(T* value) { | 146 UnretainedWrapper<T, SameThreadAffinity> unretained(T* value) { |
| 146 static_assert(!WTF::IsGarbageCollectedType<T>::value, | 147 static_assert(!WTF::IsGarbageCollectedType<T>::value, |
| 147 "unretained() + GCed type is forbidden"); | 148 "WTF::unretained() + GCed type is forbidden"); |
| 148 return UnretainedWrapper<T, SameThreadAffinity>(value); | 149 return UnretainedWrapper<T, SameThreadAffinity>(value); |
| 149 } | 150 } |
| 150 | 151 |
| 151 template <typename T> | 152 template <typename T> |
| 152 UnretainedWrapper<T, CrossThreadAffinity> crossThreadUnretained(T* value) { | 153 UnretainedWrapper<T, CrossThreadAffinity> crossThreadUnretained(T* value) { |
| 153 static_assert(!WTF::IsGarbageCollectedType<T>::value, | 154 static_assert(!WTF::IsGarbageCollectedType<T>::value, |
| 154 "crossThreadUnretained() + GCed type is forbidden"); | 155 "crossThreadUnretained() + GCed type is forbidden"); |
| 155 return UnretainedWrapper<T, CrossThreadAffinity>(value); | 156 return UnretainedWrapper<T, CrossThreadAffinity>(value); |
| 156 } | 157 } |
| 157 | 158 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 241 |
| 241 template <FunctionThreadAffinity threadAffinity, | 242 template <FunctionThreadAffinity threadAffinity, |
| 242 typename FunctionType, | 243 typename FunctionType, |
| 243 typename... BoundParameters> | 244 typename... BoundParameters> |
| 244 std::unique_ptr< | 245 std::unique_ptr< |
| 245 Function<base::MakeUnboundRunType<FunctionType, BoundParameters...>, | 246 Function<base::MakeUnboundRunType<FunctionType, BoundParameters...>, |
| 246 threadAffinity>> | 247 threadAffinity>> |
| 247 bindInternal(FunctionType function, BoundParameters&&... boundParameters) { | 248 bindInternal(FunctionType function, BoundParameters&&... boundParameters) { |
| 248 using UnboundRunType = | 249 using UnboundRunType = |
| 249 base::MakeUnboundRunType<FunctionType, BoundParameters...>; | 250 base::MakeUnboundRunType<FunctionType, BoundParameters...>; |
| 250 return wrapUnique(new Function<UnboundRunType, threadAffinity>(base::Bind( | 251 return WTF::wrapUnique(new Function<UnboundRunType, |
| 252 threadAffinity>(base::Bind( |
| 251 function, | 253 function, |
| 252 typename ParamStorageTraits<typename std::decay<BoundParameters>::type>:: | 254 typename ParamStorageTraits<typename std::decay<BoundParameters>::type>:: |
| 253 StorageType(std::forward<BoundParameters>(boundParameters))...))); | 255 StorageType(std::forward<BoundParameters>(boundParameters))...))); |
| 254 } | 256 } |
| 255 | 257 |
| 256 template <typename FunctionType, typename... BoundParameters> | 258 template <typename FunctionType, typename... BoundParameters> |
| 257 std::unique_ptr< | 259 std::unique_ptr< |
| 258 Function<base::MakeUnboundRunType<FunctionType, BoundParameters...>, | 260 Function<base::MakeUnboundRunType<FunctionType, BoundParameters...>, |
| 259 SameThreadAffinity>> | 261 SameThreadAffinity>> |
| 260 bind(FunctionType function, BoundParameters&&... boundParameters) { | 262 bind(FunctionType function, BoundParameters&&... boundParameters) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 283 | 285 |
| 284 template <typename T, WTF::FunctionThreadAffinity threadAffinity> | 286 template <typename T, WTF::FunctionThreadAffinity threadAffinity> |
| 285 struct BindUnwrapTraits<WTF::UnretainedWrapper<T, threadAffinity>> { | 287 struct BindUnwrapTraits<WTF::UnretainedWrapper<T, threadAffinity>> { |
| 286 static T* Unwrap(const WTF::UnretainedWrapper<T, threadAffinity>& wrapped) { | 288 static T* Unwrap(const WTF::UnretainedWrapper<T, threadAffinity>& wrapped) { |
| 287 return wrapped.value(); | 289 return wrapped.value(); |
| 288 } | 290 } |
| 289 }; | 291 }; |
| 290 | 292 |
| 291 } // namespace base | 293 } // namespace base |
| 292 | 294 |
| 293 using WTF::passed; | |
| 294 using WTF::unretained; | |
| 295 using WTF::crossThreadUnretained; | 295 using WTF::crossThreadUnretained; |
| 296 | 296 |
| 297 using WTF::Function; | 297 using WTF::Function; |
| 298 using WTF::CrossThreadClosure; | 298 using WTF::CrossThreadClosure; |
| 299 | 299 |
| 300 #endif // WTF_Functional_h | 300 #endif // WTF_Functional_h |
| OLD | NEW |