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