1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_TASK_IMPL_H 17 #define OHOS_TASK_IMPL_H 18 19 #include <functional> 20 21 #include "task_impl_interface.h" 22 23 namespace OHOS { 24 namespace DistributedHardware { 25 class TaskImpl : public TaskImplInterface { 26 public: 27 TaskImpl(std::function<int32_t()> taskFunc, std::function<void(int32_t)> taskCallback); 28 ~TaskImpl() override = default; 29 void Run() override; 30 31 private: 32 std::function<int32_t()> taskFunc_ = nullptr; 33 std::function<void(int32_t)> taskCallback_ = nullptr; 34 }; 35 36 template <class T> using TaskFunc = int32_t (T::*)(const std::string &); 37 template <class T> using TaskCallback = void (T::*)(int32_t, const std::string &, const std::string &); 38 39 template <typename T> 40 std::shared_ptr<TaskImplInterface> GenerateTask( 41 T *object, TaskFunc<T> taskFunc, const std::string &args, const std::string &funcName, 42 TaskCallback<T> taskCallback = [](int32_t ret, const std::string &result, const std::string &funcName) { 43 (void)ret; 44 (void)result; 45 (void)funcName; 46 }) 47 { 48 auto task = std::make_shared<TaskImpl>(std::bind(taskFunc, object, args), 49 std::bind(taskCallback, object, std::placeholders::_1, args, funcName)); 50 return task; 51 } 52 } // DistributedHardware 53 } // OHOS 54 #endif // OHOS_TASK_IMPL_H 55