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