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 "call_context.h"
17
18 META_BEGIN_NAMESPACE()
19
20 DefaultCallContext::~DefaultCallContext() = default;
21
22 DefaultCallContext::DefaultCallContext() = default;
23
DefaultCallContext(const DefaultCallContext & other)24 DefaultCallContext::DefaultCallContext(const DefaultCallContext& other) noexcept : succeeded_(other.succeeded_)
25 {
26 if (auto p = interface_cast<ICloneable>(other.result_)) {
27 result_ = interface_pointer_cast<IAny>(p->GetClone());
28 }
29 params_.resize(other.params_.size());
30 for (int i = 0; i != params_.size(); ++i) {
31 params_[i].name = other.params_[i].name;
32 if (auto p = interface_cast<ICloneable>(other.params_[i].value)) {
33 params_[i].value = interface_pointer_cast<IAny>(p->GetClone());
34 }
35 }
36 }
37
DefaultCallContext(DefaultCallContext && other)38 DefaultCallContext::DefaultCallContext(DefaultCallContext&& other) noexcept
39 : params_(std::move(other.params_)), succeeded_(other.succeeded_), result_(std::move(other.result_))
40 {}
41
operator =(const DefaultCallContext & other)42 DefaultCallContext& DefaultCallContext::operator=(const DefaultCallContext& other) noexcept
43 {
44 if (&other == this) {
45 return *this;
46 }
47 if (auto p = interface_cast<ICloneable>(other.result_)) {
48 result_ = interface_pointer_cast<IAny>(p->GetClone());
49 } else {
50 result_.reset();
51 }
52 params_.clear();
53 params_.resize(other.params_.size());
54 for (int i = 0; i != params_.size(); ++i) {
55 params_[i].name = other.params_[i].name;
56 if (auto p = interface_cast<ICloneable>(other.params_[i].value)) {
57 params_[i].value = interface_pointer_cast<IAny>(p->GetClone());
58 }
59 }
60 return *this;
61 }
62
operator =(DefaultCallContext && other)63 DefaultCallContext& DefaultCallContext::operator=(DefaultCallContext&& other) noexcept
64 {
65 if (&other == this) {
66 return *this;
67 }
68 result_ = std::move(other.result_);
69 params_ = std::move(other.params_);
70 return *this;
71 }
72
GetClone() const73 BASE_NS::shared_ptr<CORE_NS::IInterface> DefaultCallContext::GetClone() const
74 {
75 BASE_NS::shared_ptr<DefaultCallContext> p(new DefaultCallContext(*this));
76 return interface_pointer_cast<CORE_NS::IInterface>(p);
77 }
78
DefineParameter(BASE_NS::string_view name,const IAny::Ptr & value)79 bool DefaultCallContext::DefineParameter(BASE_NS::string_view name, const IAny::Ptr& value)
80 {
81 if (!name.empty() && Get(name)) {
82 return false;
83 }
84 params_.push_back(ArgumentNameValue { BASE_NS::string(name), value });
85 return true;
86 }
87
Set(BASE_NS::string_view name,const IAny & value)88 bool DefaultCallContext::Set(BASE_NS::string_view name, const IAny& value)
89 {
90 for (auto&& v : params_) {
91 if (v.name == name) {
92 return v.value->CopyFrom(value);
93 }
94 }
95 return false;
96 }
97
Get(BASE_NS::string_view name) const98 IAny::Ptr DefaultCallContext::Get(BASE_NS::string_view name) const
99 {
100 if (!name.empty()) {
101 for (auto&& v : params_) {
102 if (v.name == name) {
103 return v.value;
104 }
105 }
106 }
107 return nullptr;
108 }
109
GetParameters() const110 BASE_NS::array_view<const ArgumentNameValue> DefaultCallContext::GetParameters() const
111 {
112 return params_;
113 }
114
Succeeded() const115 bool DefaultCallContext::Succeeded() const
116 {
117 return succeeded_;
118 }
119
DefineResult(const IAny::Ptr & value)120 bool DefaultCallContext::DefineResult(const IAny::Ptr& value)
121 {
122 result_ = value;
123 return true;
124 }
125
SetResult(const IAny & value)126 bool DefaultCallContext::SetResult(const IAny& value)
127 {
128 succeeded_ = result_ && result_->CopyFrom(value);
129 if (!succeeded_) {
130 ReportError("Invalid return type for meta function call");
131 }
132 return succeeded_;
133 }
134
SetResult()135 bool DefaultCallContext::SetResult()
136 {
137 // null for void, or otherwise return type
138 succeeded_ = !result_;
139 if (!succeeded_) {
140 ReportError("Invalid return type for meta function call");
141 }
142 return succeeded_;
143 }
144
GetResult() const145 IAny::Ptr DefaultCallContext::GetResult() const
146 {
147 return result_;
148 }
149
Reset()150 void DefaultCallContext::Reset()
151 {
152 succeeded_ = false;
153 }
154
ReportError(BASE_NS::string_view error)155 void DefaultCallContext::ReportError(BASE_NS::string_view error)
156 {
157 // make sure it is null terminated
158 CORE_LOG_W("Call context error: %s", BASE_NS::string(error).c_str());
159 }
160
161 META_END_NAMESPACE()
162