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