• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 /// @brief Interface of class that should post a callback to remote side
27 class CallbackPoster {
28 public:
29     CallbackPoster() = default;
30     virtual ~CallbackPoster() = default;
31     NO_COPY_SEMANTIC(CallbackPoster);
32     NO_MOVE_SEMANTIC(CallbackPoster);
33 
34     template <class Callback, class... Args>
Post(Callback callback,Args...args)35     void Post(Callback callback, Args... args)
36     {
37         static_assert(std::is_invocable_v<Callback, Args...>);
38         static_assert(std::is_void_v<std::result_of_t<Callback(Args...)>>);
39         PostImpl([callback = std::move(callback), targs = std::tuple<Args...>(std::move(args)...)]() {
40             std::apply(std::move(callback), std::move(targs));
41         });
42     }
43 
44 protected:
45     using WrappedCallback = std::function<void()>;
46 
47     virtual void PostImpl(WrappedCallback &&callback) = 0;
48 };
49 
50 class CallbackPosterFactoryIface {
51 public:
52     CallbackPosterFactoryIface() = default;
53     virtual ~CallbackPosterFactoryIface() = default;
54     NO_COPY_SEMANTIC(CallbackPosterFactoryIface);
55     NO_MOVE_SEMANTIC(CallbackPosterFactoryIface);
56 
57     virtual PandaUniquePtr<CallbackPoster> CreatePoster() = 0;
58 };
59 
60 }  // namespace ark
61 
62 #endif  // PANDA_PLUGINS_ETS_RUNTIME_ETS_EXTERNAL_CALLBACK_POSTER_H