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 #include "connector.h"
17
18 #include <meta/ext/serialization/serializer.h>
19
META_BEGIN_NAMESPACE()20 META_BEGIN_NAMESPACE()
21
22 bool Connector::AttachTo(const META_NS::IAttach::Ptr& target, const META_NS::IObject::Ptr& dataContext)
23 {
24 auto source = source_.lock();
25 auto dest = interface_cast<IMetadata>(target);
26 if (!source || !dest) {
27 CORE_LOG_E("Failed to attach Connector");
28 return false;
29 }
30
31 auto event = source->GetEvent(eventName_);
32 if (!event) {
33 CORE_LOG_E("Failed to attach Connector: No such event");
34 return false;
35 }
36
37 auto func = dest->GetFunction(funcName_);
38 if (!func) {
39 CORE_LOG_E("Failed to attach Connector: No such function");
40 return false;
41 }
42
43 handle_ = event->AddHandler(func);
44 return handle_ != IEvent::Token {};
45 }
46
DetachFrom(const META_NS::IAttach::Ptr & target)47 bool Connector::DetachFrom(const META_NS::IAttach::Ptr& target)
48 {
49 if (handle_) {
50 auto source = source_.lock();
51 if (source) {
52 auto event = source->GetEvent(eventName_);
53 if (!event) {
54 return false;
55 }
56 event->RemoveHandler(handle_);
57 handle_ = IEvent::Token {};
58 }
59 }
60 return true;
61 }
62
Connect(const IObject::Ptr & source,const BASE_NS::string & event,const BASE_NS::string & func)63 bool Connector::Connect(const IObject::Ptr& source, const BASE_NS::string& event, const BASE_NS::string& func)
64 {
65 source_ = interface_pointer_cast<IMetadata>(source);
66 eventName_ = event;
67 funcName_ = func;
68 return !source_.expired() && !eventName_.empty() && !funcName_.empty();
69 }
70
GetSource() const71 IObject::Ptr Connector::GetSource() const
72 {
73 return interface_pointer_cast<IObject>(source_);
74 }
GetEventName() const75 BASE_NS::string Connector::GetEventName() const
76 {
77 return eventName_;
78 }
GetFunctionName() const79 BASE_NS::string Connector::GetFunctionName() const
80 {
81 return funcName_;
82 }
83
Export(IExportContext & c) const84 ReturnError Connector::Export(IExportContext& c) const
85 {
86 return Serializer(c) & NamedValue("Source", source_) & NamedValue("Event", eventName_) &
87 NamedValue("Function", funcName_);
88 }
Import(IImportContext & c)89 ReturnError Connector::Import(IImportContext& c)
90 {
91 IMetadata::Ptr p;
92 Serializer ser(c);
93 ser& NamedValue("Source", p) & NamedValue("Event", eventName_) & NamedValue("Function", funcName_);
94 if (ser && p) {
95 source_ = p;
96 }
97 return ser;
98 }
99
100 META_END_NAMESPACE()
101