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_API_CONNECTOR_H
16 #define META_API_CONNECTOR_H
17
18 #include <meta/interface/builtin_objects.h>
19 #include <meta/interface/intf_attach.h>
20 #include <meta/interface/intf_attachment.h>
21 #include <meta/interface/intf_connector.h>
22 #include <meta/interface/intf_object_registry.h>
23
META_BEGIN_NAMESPACE()24 META_BEGIN_NAMESPACE()
25
26 /**
27 * @brief Connect source object event to destination object meta function.
28 * @param source Object that contains the specified event.
29 * @param event Event name.
30 * @param dest Destination object which implements the specified meta function, must implement IAttach interface.
31 * @func Function name.
32 * @return True on success, otherwise false.
33 */
34 inline bool Connect(
35 const IObject::Ptr& source, const BASE_NS::string& event, const IObject::Ptr& dest, const BASE_NS::string& func)
36 {
37 auto attach = interface_cast<IAttach>(dest);
38 if (!attach) {
39 CORE_LOG_E("Object does not implement IAttach");
40 return false;
41 }
42 auto connector = META_NS::GetObjectRegistry().Create<IConnector>(META_NS::ClassId::Connector);
43 if (!connector) {
44 CORE_LOG_E("Failed to create connector object");
45 return false;
46 }
47 return connector->Connect(source, event, func) && attach->Attach(interface_pointer_cast<IAttachment>(connector));
48 }
49
50 /**
51 * @brief Disconnect connection created with above Connect function.
52 * @param source Object that contains the specified event.
53 * @param event Event name.
54 * @param dest Destination object which implements the specified meta function, must implement IAttach interface.
55 * @func Function name.
56 * @return True if connector object was detached successfully, otherwise false.
57 */
Disconnect(const IObject::Ptr & source,const BASE_NS::string & event,const IObject::Ptr & dest,const BASE_NS::string & func)58 inline bool Disconnect(
59 const IObject::Ptr& source, const BASE_NS::string& event, const IObject::Ptr& dest, const BASE_NS::string& func)
60 {
61 auto attach = interface_cast<IAttach>(dest);
62 if (!attach) {
63 CORE_LOG_E("Object does not implement IAttach");
64 return false;
65 }
66 auto cont = attach->GetAttachmentContainer();
67 if (cont) {
68 auto list = cont->GetAll<IConnector>();
69 for (auto&& v : list) {
70 if (v->GetSource() == source && v->GetEventName() == event && v->GetFunctionName() == func) {
71 return attach->Detach(interface_pointer_cast<IAttachment>(v));
72 }
73 }
74 }
75 return false;
76 }
77
78 META_END_NAMESPACE()
79
80 #endif
81