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