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