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/log.h>
23 #include <core/plugin/intf_interface.h>
24
25 #include <meta/base/interface_macros.h>
26 #include <meta/base/namespace.h>
27 #include <meta/interface/intf_callable.h>
28
29 META_BEGIN_NAMESPACE()
30
31 META_REGISTER_INTERFACE(IEvent, "8b3c8322-54d6-4d1c-943c-207190576a4e")
32
33 class IEvent : public CORE_NS::IInterface {
34 META_INTERFACE(CORE_NS::IInterface, IEvent)
35
36 public:
37 /** handler identifier, at least big enough to keep an object pointer. */
38 using Token = uint64_t;
39
40 /** Returns the interface id that the callables must implement. */
41 virtual BASE_NS::Uid GetCallableUid() const = 0;
42 /** Returns the name of this event. Same as GetEventTypeName if not explicitly set. */
43 virtual BASE_NS::string GetName() const = 0;
44 /** Returns the event type name. */
45 virtual BASE_NS::string GetEventTypeName() const = 0;
46
47 /**
48 * @brief Add handler which is invoked when event is triggered
49 * @param userToken Optional token that is used to identify the handler.
50 * Notice that this should be unique or it can collide with other handler tokens.
51 * @return Default constructed Token if "invalid bindable", token already in use or other error.
52 */
53 virtual Token AddHandler(const ICallable::Ptr&, Token userToken) = 0;
AddHandler(const ICallable::Ptr & p)54 Token AddHandler(const ICallable::Ptr& p)
55 {
56 return AddHandler(p, {});
57 }
58
59 /**
60 * @brief Remove handler with given token (Token returned by AddHandler previously).
61 * if handler added multiple times, see if re-enable it (so you don't have to call EnableHandler separately)
62 */
63 virtual bool RemoveHandler(Token, bool reenable) = 0;
RemoveHandler(Token t)64 virtual bool RemoveHandler(Token t)
65 {
66 return RemoveHandler(t, false);
67 }
68
69 /**
70 * @brief Enabled/Disable handler without removing it
71 */
72 virtual void EnableHandler(Token, bool enabled) = 0;
73
74 /**
75 * @brief Remove all handlers
76 */
77 virtual void Reset() = 0;
78
79 /**
80 * @brief Check if callable can be added as handler.
81 */
82 virtual bool IsCompatibleWith(const ICallable::Ptr&) const = 0;
83
84 /**
85 * @brief Get list of current handlers.
86 */
87 virtual BASE_NS::vector<ICallable::ConstPtr> GetHandlers() const = 0;
88
89 /**
90 * @brief Returns true if the event has any handlers, false otherwise.
91 */
92 virtual bool HasHandlers() const = 0;
93 };
94
95 template<typename EventType, typename Ret, typename... Args>
96 class IEventCallable : public ICallable {
97 META_INTERFACE(ICallable, IEventCallable, EventType::UID)
98 public:
99 using ReturnType = Ret;
100 using InterfaceType = IEventCallable<EventType, Ret, Args...>;
101 using InterfaceTypePtr = typename InterfaceType::Ptr;
102 using FunctionType = Ret(Args...);
103 virtual Ret Invoke(Args...) = 0;
104 };
105
106 template<typename MyEvent, typename... Args>
Invoke(const IEvent::Ptr & event,Args &&...args)107 inline auto Invoke(const IEvent::Ptr& event, Args&&... args)
108 {
109 // interpret null as the event not being constructed as not needed
110 if (event) {
111 if (auto i = interface_cast<typename MyEvent::InterfaceType>(event)) {
112 return i->Invoke(BASE_NS::forward<Args>(args)...);
113 }
114 CORE_LOG_W("Trying to Invoke wrong type of event");
115 }
116 if constexpr (!BASE_NS::is_same_v<typename MyEvent::ReturnType, void>) {
117 return typename MyEvent::ReturnType {};
118 }
119 }
120
121 class ScopedDisableEventHandler {
META_NO_COPY_MOVE(ScopedDisableEventHandler)122 META_NO_COPY_MOVE(ScopedDisableEventHandler)
123 public:
124 ScopedDisableEventHandler(BASE_NS::shared_ptr<IEvent> event, IEvent::Token token)
125 : event_(BASE_NS::move(event)), token_(token)
126 {
127 if (event_ && token_) {
128 event_->EnableHandler(token_, false);
129 }
130 }
~ScopedDisableEventHandler()131 ~ScopedDisableEventHandler()
132 {
133 if (event_ && token_) {
134 event_->EnableHandler(token_, true);
135 }
136 }
137
138 private:
139 BASE_NS::shared_ptr<IEvent> event_ {};
140 IEvent::Token token_ {};
141 };
142
143 META_END_NAMESPACE()
144
145 #endif
146