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 "functions.h"
16 
17 #include <meta/ext/serialization/serializer.h>
18 
META_BEGIN_NAMESPACE()19 META_BEGIN_NAMESPACE()
20 
21 BASE_NS::string SettableFunction::GetName() const
22 {
23     auto p = func_.lock();
24     return p ? p->GetName() : BASE_NS::string();
25 }
GetDestination() const26 IObject::ConstPtr SettableFunction::GetDestination() const
27 {
28     auto p = func_.lock();
29     return p ? p->GetDestination() : nullptr;
30 }
Invoke(const ICallContext::Ptr & context) const31 void SettableFunction::Invoke(const ICallContext::Ptr& context) const
32 {
33     if (auto p = func_.lock()) {
34         p->Invoke(context);
35     }
36 }
CreateCallContext() const37 ICallContext::Ptr SettableFunction::CreateCallContext() const
38 {
39     auto p = func_.lock();
40     return p ? p->CreateCallContext() : nullptr;
41 }
SetTarget(const IObject::Ptr & obj,BASE_NS::string_view name)42 bool SettableFunction::SetTarget(const IObject::Ptr& obj, BASE_NS::string_view name)
43 {
44     return ResolveFunction(obj, name);
45 }
ResolveFunction(const IObject::Ptr & obj,BASE_NS::string_view name)46 bool SettableFunction::ResolveFunction(const IObject::Ptr& obj, BASE_NS::string_view name)
47 {
48     func_ = nullptr;
49     if (auto metad = interface_cast<IMetadata>(obj)) {
50         func_ = metad->GetFunctionByName(name);
51     }
52     return !func_.expired();
53 }
Export(IExportContext & c) const54 ReturnError SettableFunction::Export(IExportContext& c) const
55 {
56     if (auto func = func_.lock()) {
57         if (auto dest = func->GetDestination()) {
58             return Serializer(c) & NamedValue("Destination", dest) & NamedValue("Function", func->GetName());
59         }
60         CORE_LOG_E("No destination object with Function");
61     }
62     return GenericError::FAIL;
63 }
Import(IImportContext & c)64 ReturnError SettableFunction::Import(IImportContext& c)
65 {
66     IObject::Ptr p;
67     BASE_NS::string func;
68     Serializer ser(c);
69     if (ser & NamedValue("Destination", p) & NamedValue("Function", func)) {
70         if (ResolveFunction(p, func)) {
71             return GenericError::SUCCESS;
72         }
73     }
74     return GenericError::FAIL;
75 }
76 
GetName() const77 BASE_NS::string PropertyFunction::GetName() const
78 {
79     auto p = prop_.lock();
80     return p ? p->GetName() : BASE_NS::string();
81 }
GetDestination() const82 IObject::ConstPtr PropertyFunction::GetDestination() const
83 {
84     return interface_pointer_cast<IObject>(prop_.lock());
85 }
Invoke(const ICallContext::Ptr & context) const86 void PropertyFunction::Invoke(const ICallContext::Ptr& context) const
87 {
88     if (auto p = prop_.lock()) {
89         context->SetResult(p->GetValue());
90     } else {
91         CORE_LOG_W("Invoked property function without valid property");
92     }
93 }
CreateCallContext() const94 ICallContext::Ptr PropertyFunction::CreateCallContext() const
95 {
96     ICallContext::Ptr context;
97     if (auto i = interface_pointer_cast<IPropertyInternalAny>(prop_)) {
98         if (auto any = i->GetInternalAny()) {
99             context = GetObjectRegistry().ConstructDefaultCallContext();
100             if (context) {
101                 context->DefineResult(any->Clone(false));
102             }
103         }
104     }
105     return context;
106 }
SetTarget(const IProperty::ConstPtr & prop)107 bool PropertyFunction::SetTarget(const IProperty::ConstPtr& prop)
108 {
109     prop_ = prop;
110     return false;
111 }
Export(IExportContext & c) const112 ReturnError PropertyFunction::Export(IExportContext& c) const
113 {
114     return Serializer(c) & NamedValue("source", prop_);
115 }
Import(IImportContext & c)116 ReturnError PropertyFunction::Import(IImportContext& c)
117 {
118     return Serializer(c) & NamedValue("source", uri_);
119 }
Finalize(IImportFunctions & funcs)120 ReturnError PropertyFunction::Finalize(IImportFunctions& funcs)
121 {
122     if (uri_.IsValid() && uri_.ReferencesProperty()) {
123         auto objUri = uri_;
124         objUri.TakeLastNode();
125         if (auto obj = interface_pointer_cast<IMetadata>(funcs.ResolveRefUri(objUri))) {
126             if (auto prop = obj->GetPropertyByName(uri_.ReferencedName())) {
127                 prop_ = prop;
128             }
129             return GenericError::SUCCESS;
130         }
131     }
132     return GenericError::FAIL;
133 }
134 
135 META_END_NAMESPACE()
136