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