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_EXT_EVENT_UTIL_H
17 #define META_EXT_EVENT_UTIL_H
18
19 #include <meta/interface/intf_metadata.h>
20
META_BEGIN_NAMESPACE()21 META_BEGIN_NAMESPACE()
22
23 template<typename MyEvent, typename MyObj, typename... Args>
24 inline auto InvokeByName(const MyObj& obj, BASE_NS::string_view name, Args&&... args)
25 {
26 if (auto i = interface_cast<IMetadata>(obj)) {
27 if (auto ev = i->GetEvent(name, MetadataQuery::EXISTING)) {
28 if (auto event = interface_cast<typename MyEvent::InterfaceType>(ev)) {
29 return event->Invoke(BASE_NS::forward<Args>(args)...);
30 }
31 CORE_LOG_W("Trying to Invoke wrong type of event");
32 }
33 } else {
34 CORE_LOG_W("Trying to Invoke event by name with type that doesn't support IMetadata");
35 }
36 if constexpr (!BASE_NS::is_same_v<typename MyEvent::ReturnType, void>) {
37 return typename MyEvent::ReturnType {};
38 }
39 }
40
41 META_END_NAMESPACE()
42 #endif
43