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 #ifndef META_API_MAKE_CALLBACK_H
16 #define META_API_MAKE_CALLBACK_H
17 
18 #include <meta/base/capture.h>
19 #include <meta/interface/interface_helpers.h>
20 #include <meta/interface/intf_lifecycle.h>
21 #include <meta/interface/intf_object_registry.h>
22 #include <meta/interface/property/intf_property.h>
23 
24 META_BEGIN_NAMESPACE();
25 
26 namespace {
27 
28 template<typename Callable, typename Func, typename Signature = typename Callable::FunctionType>
29 class Callback;
30 
31 template<typename Callable, typename Func, typename R, typename... ARG>
32 class Callback<Callable, Func, R(ARG...)> : public IntroduceInterfaces<Callable> {
33 public:
Callback(Func f)34     Callback(Func f) : func_(BASE_NS::move(f)) {}
35 
36 protected:
37     Func func_;
Invoke(ARG...args)38     R Invoke(ARG... args) override
39     {
40         return func_(args...);
41     }
42 };
43 
44 } // namespace
45 
46 /**
47  * @brief MakeCallable creates a generic callable from callable entity (e.g. lambda).
48  * @param CallableType Type that defines the callable interface, e.g. ITaskQueueTask
49  */
50 template<typename CallableType, typename Func>
MakeCallback(Func f)51 auto MakeCallback(Func f)
52 {
53     return typename CallableType::Ptr(new Callback<CallableType, Func>(BASE_NS::move(f)));
54 }
55 
56 /**
57  * @brief As above MakeCallable but using capture helper. @see Capture.
58  */
59 template<typename CallableType, typename Func, typename... Args>
MakeCallback(Func f,Args &&...args)60 auto MakeCallback(Func f, Args&&... args)
61 {
62     return MakeCallback<CallableType>(Capture(BASE_NS::move(f), BASE_NS::forward<Args>(args)...));
63 }
64 
65 /**
66  * @brief Creates a generic callable from a class method.
67  * Note: User needs to ensure that the lifetime of the class is longer than the created callback.
68  */
69 template<typename CallableType, typename o, typename R, typename... ARG>
MakeCallback(o * instance,R (o::* func)(ARG...))70 auto MakeCallback(o* instance, R (o::*func)(ARG...))
71 {
72     return MakeCallback<CallableType>([instance, func](ARG... args) { return (instance->*func)(args...); });
73 }
74 
75 /**
76  * @brief Creates a generic callable from callable entity (e.g. lambda). The callable entity is executed only if
77  * all captured shared pointers are valid.
78  * @param BaseClass Interface that defines event, e.g. IOnChanged.
79  * @param args Capture helper support.
80  * @see Capture
81  */
82 template<typename CallableType, typename Func, typename... Args>
MakeCallbackSafe(Func f,Args &&...args)83 auto MakeCallbackSafe(Func f, Args&&... args)
84 {
85     return MakeCallback<CallableType>(CaptureSafe(BASE_NS::move(f), BASE_NS::forward<Args>(args)...));
86 }
87 
88 META_END_NAMESPACE();
89 #endif
90