1 /** 2 * Copyright (c) 2024-2025 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 PANDA_PLUGINS_ETS_RUNTIME_ETS_EXTERNAL_CALLBACK_POSTER_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_ETS_EXTERNAL_CALLBACK_POSTER_H 18 19 #include <functional> 20 21 #include "libpandabase/macros.h" 22 #include "runtime/include/mem/panda_smart_pointers.h" 23 24 namespace ark { 25 26 class Coroutine; 27 28 /// @brief Interface of class that should post a callback to remote side 29 class CallbackPoster { 30 public: 31 CallbackPoster() = default; 32 virtual ~CallbackPoster() = default; 33 NO_COPY_SEMANTIC(CallbackPoster); 34 NO_MOVE_SEMANTIC(CallbackPoster); 35 36 template <class Callback, class... Args> Post(Callback callback,Args...args)37 void Post(Callback callback, Args... args) 38 { 39 static_assert(std::is_invocable_v<Callback, Args...>); 40 static_assert(std::is_void_v<std::result_of_t<Callback(Args...)>>); 41 PostImpl([callback = std::move(callback), targs = std::tuple<Args...>(std::move(args)...)]() { 42 std::apply(std::move(callback), std::move(targs)); 43 }); 44 } 45 SetDestroyInPlace()46 void SetDestroyInPlace() 47 { 48 destroyInPlace_ = true; 49 } 50 NeedDestroyInPlace()51 bool NeedDestroyInPlace() const 52 { 53 return destroyInPlace_; 54 } 55 56 protected: 57 using WrappedCallback = std::function<void()>; 58 59 virtual void PostImpl(WrappedCallback &&callback) = 0; 60 61 private: 62 bool destroyInPlace_ = false; 63 }; 64 65 class CallbackPosterFactoryIface { 66 public: 67 CallbackPosterFactoryIface() = default; 68 virtual ~CallbackPosterFactoryIface() = default; 69 NO_COPY_SEMANTIC(CallbackPosterFactoryIface); 70 NO_MOVE_SEMANTIC(CallbackPosterFactoryIface); 71 72 virtual PandaUniquePtr<CallbackPoster> CreatePoster() = 0; 73 }; 74 75 } // namespace ark 76 77 #endif // PANDA_PLUGINS_ETS_RUNTIME_ETS_EXTERNAL_CALLBACK_POSTER_H