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 GetName() const = 0;
43 /** Returns the event type name. */
44 virtual BASE_NS::string 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 * if handler added multiple times, see if re-enable it (so you don't have to call EnableHandler separately)
61 */
62 virtual bool RemoveHandler(Token, bool reenable) = 0;
RemoveHandler(Token t)63 virtual bool RemoveHandler(Token t)
64 {
65 return RemoveHandler(t, false);
66 }
67
68 /**
69 * @brief Enabled/Disable handler without removing it
70 */
71 virtual void EnableHandler(Token, bool enabled) = 0;
72
73 /**
74 * @brief Remove all handlers
75 */
76 virtual void Reset() = 0;
77
78 /**
79 * @brief Check if callable can be added as handler.
80 */
81 virtual bool IsCompatibleWith(const ICallable::Ptr&) const = 0;
82
83 /**
84 * @brief Get list of current handlers.
85 */
86 virtual BASE_NS::vector<ICallable::ConstPtr> GetHandlers() const = 0;
87
88 /**
89 * @brief Returns true if the event has any handlers, false otherwise.
90 */
91 virtual bool HasHandlers() const = 0;
92 };
93
94 template<typename EventType, typename Ret, typename... Args>
95 class IEventCallable : public ICallable {
96 META_INTERFACE(ICallable, IEventCallable, EventType::UID)
97 public:
98 using ReturnType = Ret;
99 using InterfaceType = IEventCallable<EventType, Ret, Args...>;
100 using InterfaceTypePtr = typename InterfaceType::Ptr;
101 using FunctionType = Ret(Args...);
102 virtual Ret Invoke(Args...) = 0;
103 };
104
105 template<typename MyEvent, typename... Args>
Invoke(const IEvent::Ptr & event,Args &&...args)106 inline auto Invoke(const IEvent::Ptr& event, Args&&... args)
107 {
108 // interpret null as the event not being constructed as not needed
109 if (event) {
110 if (auto i = interface_cast<typename MyEvent::InterfaceType>(event)) {
111 return i->Invoke(BASE_NS::forward<Args>(args)...);
112 }
113 CORE_LOG_W("Trying to Invoke wrong type of event");
114 }
115 if constexpr (!BASE_NS::is_same_v<typename MyEvent::ReturnType, void>) {
116 return typename MyEvent::ReturnType {};
117 }
118 }
119
120 class ScopedDisableEventHandler {
META_NO_COPY_MOVE(ScopedDisableEventHandler)121 META_NO_COPY_MOVE(ScopedDisableEventHandler)
122 public:
123 ScopedDisableEventHandler(BASE_NS::shared_ptr<IEvent> event, IEvent::Token token)
124 : event_(BASE_NS::move(event)), token_(token)
125 {
126 if (event_ && token_) {
127 event_->EnableHandler(token_, false);
128 }
129 }
~ScopedDisableEventHandler()130 ~ScopedDisableEventHandler()
131 {
132 if (event_ && token_) {
133 event_->EnableHandler(token_, true);
134 }
135 }
136
137 private:
138 BASE_NS::shared_ptr<IEvent> event_ {};
139 IEvent::Token token_ {};
140 };
141
142 META_END_NAMESPACE()
143
144 #endif
145