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 META_INTERFACE_IEVENT_H
17 #define META_INTERFACE_IEVENT_H
18 
19 #include <base/containers/array_view.h>
20 #include <base/containers/string_view.h>
21 #include <base/util/uid.h>
22 #include <core/plugin/intf_interface.h>
23 
24 #include <meta/base/interface_macros.h>
25 #include <meta/base/namespace.h>
26 #include <meta/interface/intf_callable.h>
27 
28 META_BEGIN_NAMESPACE()
29 
30 META_REGISTER_INTERFACE(IEvent, "8b3c8322-54d6-4d1c-943c-207190576a4e")
31 
32 class IEvent : public CORE_NS::IInterface {
33     META_INTERFACE(CORE_NS::IInterface, IEvent);
34 
35 public:
36     /** handler identifier, at least big enough to keep an object pointer. */
37     using Token = uint64_t;
38 
39     /** Returns the interface id that the callables must implement. */
40     virtual BASE_NS::Uid GetCallableUid() const = 0;
41     /** Returns the name of this event. Same as GetEventTypeName if not explicitly set. */
42     virtual BASE_NS::string_view GetName() const = 0;
43     /** Returns the event type name. */
44     virtual BASE_NS::string_view GetEventTypeName() const = 0;
45 
46     /**
47      * @brief Add handler which is invoked when event is triggered
48      * @param userToken Optional token that is used to identify the handler.
49      *                  Notice that this should be unique or it can collide with other handler tokens.
50      * @return Default constructed Token if "invalid bindable", token already in use or other error.
51      */
52     virtual Token AddHandler(const ICallable::Ptr&, Token userToken) = 0;
AddHandler(const ICallable::Ptr & p)53     Token AddHandler(const ICallable::Ptr& p)
54     {
55         return AddHandler(p, {});
56     }
57 
58     /**
59      * @brief Remove handler with given token (Token returned by AddHandler previously).
60      */
61     virtual bool RemoveHandler(Token) = 0;
62 
63     /**
64      * @brief Remove all handlers
65      */
66     virtual void Reset() = 0;
67 
68     /**
69      * @brief Check if callable can be added as handler.
70      */
71     virtual bool IsCompatibleWith(const ICallable::Ptr&) const = 0;
72 
73     /**
74      * @brief Get list of current handlers.
75      */
76     virtual BASE_NS::vector<ICallable::ConstPtr> GetHandlers() const = 0;
77 
78     /**
79      * @brief Returns true if the event has any handlers, false otherwise.
80      */
81     virtual bool HasHandlers() const = 0;
82 };
83 
84 template<typename EventType, typename Ret, typename... Args>
85 class IEventCallable : public ICallable {
86     META_INTERFACE(ICallable, IEventCallable, EventType::UID)
87 public:
88     using ReturnType = Ret;
89     using InterfaceType = IEventCallable<EventType, Ret, Args...>;
90     using InterfaceTypePtr = typename InterfaceType::Ptr;
91     using FunctionType = Ret(Args...);
92     virtual Ret Invoke(Args...) = 0;
93 };
94 
95 template<typename MyEvent, typename... Args>
Invoke(const IEvent::Ptr & event,Args &&...args)96 inline auto Invoke(const IEvent::Ptr& event, Args&&... args)
97 {
98     if (auto i = interface_cast<typename MyEvent::InterfaceType>(event)) {
99         return i->Invoke(BASE_NS::forward<Args>(args)...);
100     }
101     CORE_LOG_W("Trying to Invoke wrong type of event");
102     if constexpr (!BASE_NS::is_same_v<typename MyEvent::ReturnType, void>) {
103         return typename MyEvent::ReturnType {};
104     }
105 }
106 
107 META_END_NAMESPACE()
108 
109 #endif
110