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