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