1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TASK_POST_TASK_AND_REPLY_WITH_RESULT_INTERNAL_H_ 6 #define BASE_TASK_POST_TASK_AND_REPLY_WITH_RESULT_INTERNAL_H_ 7 8 #include <memory> 9 #include <utility> 10 11 #include "base/check.h" 12 #include "base/functional/callback.h" 13 14 namespace base { 15 16 namespace internal { 17 18 // Adapts a function that produces a result via a return value to 19 // one that returns via an output parameter. 20 template <typename ReturnType> ReturnAsParamAdapter(OnceCallback<ReturnType ()> func,std::unique_ptr<ReturnType> * result)21void ReturnAsParamAdapter(OnceCallback<ReturnType()> func, 22 std::unique_ptr<ReturnType>* result) { 23 result->reset(new ReturnType(std::move(func).Run())); 24 } 25 26 // Adapts a T* result to a callblack that expects a T. 27 template <typename TaskReturnType, typename ReplyArgType> ReplyAdapter(OnceCallback<void (ReplyArgType)> callback,std::unique_ptr<TaskReturnType> * result)28void ReplyAdapter(OnceCallback<void(ReplyArgType)> callback, 29 std::unique_ptr<TaskReturnType>* result) { 30 DCHECK(result->get()); 31 std::move(callback).Run(std::move(**result)); 32 } 33 34 } // namespace internal 35 36 } // namespace base 37 38 #endif // BASE_TASK_POST_TASK_AND_REPLY_WITH_RESULT_INTERNAL_H_ 39