Chromium Code Reviews| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 // ... | 86 // ... |
| 87 // std::unique_ptr<Function<void()>> functor = bind(yourFunction, passed(Arg ument())); | 87 // std::unique_ptr<Function<void()>> functor = bind(yourFunction, passed(Arg ument())); |
| 88 // ... | 88 // ... |
| 89 // (*functor)(); | 89 // (*functor)(); |
| 90 // | 90 // |
| 91 // The underlying function must receive the argument wrapped by passed() by rval ue reference or by value. | 91 // The underlying function must receive the argument wrapped by passed() by rval ue reference or by value. |
| 92 // | 92 // |
| 93 // Obviously, if you create a functor this way, you shouldn't call the functor t wice or more; after the second call, | 93 // Obviously, if you create a functor this way, you shouldn't call the functor t wice or more; after the second call, |
| 94 // the passed argument may be invalid. | 94 // the passed argument may be invalid. |
| 95 | 95 |
| 96 enum FunctionThreadAffinity { | |
| 97 CrossThreadAffinity, | |
| 98 SameThreadAffinity | |
| 99 }; | |
| 100 | |
| 96 template <typename T> | 101 template <typename T> |
| 97 class PassedWrapper final { | 102 class PassedWrapper final { |
| 98 public: | 103 public: |
| 99 explicit PassedWrapper(T&& scoper) : m_scoper(std::move(scoper)) { } | 104 explicit PassedWrapper(T&& scoper) : m_scoper(std::move(scoper)) { } |
| 100 PassedWrapper(PassedWrapper&& other) : m_scoper(std::move(other.m_scoper)) { } | 105 PassedWrapper(PassedWrapper&& other) : m_scoper(std::move(other.m_scoper)) { } |
| 101 T moveOut() { return std::move(m_scoper); } | 106 T moveOut() { return std::move(m_scoper); } |
| 102 | 107 |
| 103 private: | 108 private: |
| 104 T m_scoper; | 109 T m_scoper; |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 template <typename T> | 112 template <typename T> |
| 108 PassedWrapper<T> passed(T&& value) | 113 PassedWrapper<T> passed(T&& value) |
| 109 { | 114 { |
| 110 static_assert(!std::is_reference<T>::value, | 115 static_assert(!std::is_reference<T>::value, |
| 111 "You must pass an rvalue to passed() so it can be moved. Add std::move() if necessary."); | 116 "You must pass an rvalue to passed() so it can be moved. Add std::move() if necessary."); |
| 112 static_assert(!std::is_const<T>::value, "|value| must not be const so it can be moved."); | 117 static_assert(!std::is_const<T>::value, "|value| must not be const so it can be moved."); |
| 113 return PassedWrapper<T>(std::move(value)); | 118 return PassedWrapper<T>(std::move(value)); |
| 114 } | 119 } |
| 115 | 120 |
| 121 template <typename T, FunctionThreadAffinity threadAffinity> | |
| 122 class UnretainedWrapper final { | |
| 123 public: | |
| 124 explicit UnretainedWrapper(T* ptr) : m_ptr(ptr) { } | |
| 125 T* value() const { return m_ptr; } | |
| 126 | |
| 127 private: | |
| 128 T* m_ptr; | |
| 129 }; | |
| 130 | |
| 131 template <typename T> | |
| 132 UnretainedWrapper<T, SameThreadAffinity> unretained(T* value) | |
| 133 { | |
| 134 static_assert(!WTF::IsGarbageCollectedType<T>::value, "unretained() + GCed t ype is forbidden"); | |
| 135 return UnretainedWrapper<T, SameThreadAffinity>(value); | |
| 136 } | |
| 137 | |
| 138 template <typename T> | |
| 139 UnretainedWrapper<T, CrossThreadAffinity> crossThreadUnretained(T* value) | |
| 140 { | |
| 141 static_assert(!WTF::IsGarbageCollectedType<T>::value, "crossThreadUnretained () + GCed type is forbidden"); | |
| 142 return UnretainedWrapper<T, CrossThreadAffinity>(value); | |
| 143 } | |
| 144 | |
| 116 // A FunctionWrapper is a class template that can wrap a function pointer or a m ember function pointer and | 145 // A FunctionWrapper is a class template that can wrap a function pointer or a m ember function pointer and |
| 117 // provide a unified interface for calling that function. | 146 // provide a unified interface for calling that function. |
| 118 template <typename> | 147 template <typename> |
| 119 class FunctionWrapper; | 148 class FunctionWrapper; |
| 120 | 149 |
| 121 // Bound static functions: | 150 // Bound static functions: |
| 122 template <typename R, typename... Parameters> | 151 template <typename R, typename... Parameters> |
| 123 class FunctionWrapper<R(*)(Parameters...)> { | 152 class FunctionWrapper<R(*)(Parameters...)> { |
| 124 DISALLOW_NEW(); | 153 DISALLOW_NEW(); |
| 125 public: | 154 public: |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 }; | 251 }; |
| 223 | 252 |
| 224 template <typename T> | 253 template <typename T> |
| 225 struct ParamStorageTraits<PassedWrapper<T>> { | 254 struct ParamStorageTraits<PassedWrapper<T>> { |
| 226 typedef PassedWrapper<T> StorageType; | 255 typedef PassedWrapper<T> StorageType; |
| 227 | 256 |
| 228 static StorageType wrap(PassedWrapper<T>&& value) { return std::move(value); } | 257 static StorageType wrap(PassedWrapper<T>&& value) { return std::move(value); } |
| 229 static T unwrap(StorageType& value) { return value.moveOut(); } | 258 static T unwrap(StorageType& value) { return value.moveOut(); } |
| 230 }; | 259 }; |
| 231 | 260 |
| 232 enum FunctionThreadAffinity { | 261 template <typename T, FunctionThreadAffinity threadAffinity> |
| 233 CrossThreadAffinity, | 262 struct ParamStorageTraits<UnretainedWrapper<T, threadAffinity>> { |
| 234 SameThreadAffinity | 263 typedef UnretainedWrapper<T, threadAffinity> StorageType; |
| 264 | |
| 265 static StorageType wrap(UnretainedWrapper<T, threadAffinity>&& value) { retu rn std::move(value); } | |
|
Yuta Kitamura
2016/05/17 09:37:04
rvalue reference + std::move() sounds like an over
hiroshige
2016/06/09 09:21:53
Done.
| |
| 266 static T* unwrap(const StorageType& value) { return value.value(); } | |
| 235 }; | 267 }; |
| 236 | 268 |
| 237 template<typename, FunctionThreadAffinity threadAffinity = SameThreadAffinity> | 269 template<typename, FunctionThreadAffinity threadAffinity = SameThreadAffinity> |
| 238 class Function; | 270 class Function; |
| 239 | 271 |
| 240 template<FunctionThreadAffinity threadAffinity, typename R, typename... Args> | 272 template<FunctionThreadAffinity threadAffinity, typename R, typename... Args> |
| 241 class Function<R(Args...), threadAffinity> { | 273 class Function<R(Args...), threadAffinity> { |
| 242 USING_FAST_MALLOC(Function); | 274 USING_FAST_MALLOC(Function); |
| 243 WTF_MAKE_NONCOPYABLE(Function); | 275 WTF_MAKE_NONCOPYABLE(Function); |
| 244 public: | 276 public: |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 { | 369 { |
| 338 return bindInternal<SameThreadAffinity, UnboundParameters...>(function, std: :forward<BoundParameters>(boundParameters)...); | 370 return bindInternal<SameThreadAffinity, UnboundParameters...>(function, std: :forward<BoundParameters>(boundParameters)...); |
| 339 } | 371 } |
| 340 | 372 |
| 341 typedef Function<void(), SameThreadAffinity> SameThreadClosure; | 373 typedef Function<void(), SameThreadAffinity> SameThreadClosure; |
| 342 typedef Function<void(), CrossThreadAffinity> CrossThreadClosure; | 374 typedef Function<void(), CrossThreadAffinity> CrossThreadClosure; |
| 343 | 375 |
| 344 } // namespace WTF | 376 } // namespace WTF |
| 345 | 377 |
| 346 using WTF::passed; | 378 using WTF::passed; |
| 379 using WTF::unretained; | |
| 380 using WTF::crossThreadUnretained; | |
| 347 using WTF::Function; | 381 using WTF::Function; |
| 348 using WTF::bind; | 382 using WTF::bind; |
| 349 using WTF::SameThreadClosure; | 383 using WTF::SameThreadClosure; |
| 350 using WTF::CrossThreadClosure; | 384 using WTF::CrossThreadClosure; |
| 351 | 385 |
| 352 #endif // WTF_Functional_h | 386 #endif // WTF_Functional_h |
| OLD | NEW |