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 "engine_value.h"
16
17 #include <core/property/intf_property_api.h>
18
19 #include "any.h"
20
21 META_BEGIN_NAMESPACE()
22
23 namespace Internal {
24
EngineValue(BASE_NS::string name,IEngineInternalValueAccess::ConstPtr access,const EnginePropertyParams & p)25 EngineValue::EngineValue(
26 BASE_NS::string name, IEngineInternalValueAccess::ConstPtr access, const EnginePropertyParams& p)
27 : Super(ObjectFlagBits::INTERNAL | ObjectFlagBits::SERIALIZE), params_(p), access_(BASE_NS::move(access)),
28 name_(BASE_NS::move(name)), value_(access_->CreateAny(p.property))
29 {}
30
Sync(EngineSyncDirection dir)31 AnyReturnValue EngineValue::Sync(EngineSyncDirection dir)
32 {
33 if (!params_.handle) {
34 return AnyReturn::INVALID_ARGUMENT;
35 }
36 if (dir == EngineSyncDirection::TO_ENGINE || (dir == EngineSyncDirection::AUTO && valueChanged_)) {
37 if (valueChanged_) {
38 valueChanged_ = false;
39 return access_->SyncToEngine(*value_, params_) ? AnyReturn::NOTHING_TO_DO : AnyReturn::FAIL;
40 }
41 return AnyReturn::NOTHING_TO_DO;
42 }
43 auto res = access_->SyncFromEngine(params_, *value_);
44 pendingNotify_ = pendingNotify_ || (res && res != AnyReturn::NOTHING_TO_DO);
45 return res;
46 }
SetValue(const IAny & value)47 AnyReturnValue EngineValue::SetValue(const IAny& value)
48 {
49 AnyReturnValue res = value_->CopyFrom(value);
50 valueChanged_ |= static_cast<bool>(res);
51 if (params_.pushValueToEngineDirectly) {
52 if (valueChanged_) {
53 if (!params_.handle) {
54 return AnyReturn::INVALID_ARGUMENT;
55 }
56 valueChanged_ = false;
57 // this returns intentionally NOTHING_TO_DO for now as the ets scene plugin completely breaks if notify
58 // about this.
59 res = access_->SyncToEngine(*value_, params_) ? AnyReturn::NOTHING_TO_DO : AnyReturn::FAIL;
60 }
61 }
62 return res;
63 }
GetValue() const64 const IAny& EngineValue::GetValue() const
65 {
66 if (params_.pushValueToEngineDirectly) {
67 if (!params_.handle || !access_->SyncFromEngine(params_, *value_)) {
68 return INVALID_ANY;
69 }
70 }
71 return *value_;
72 }
IsCompatible(const TypeId & id) const73 bool EngineValue::IsCompatible(const TypeId& id) const
74 {
75 return META_NS::IsCompatible(*value_, id);
76 }
GetName() const77 BASE_NS::string EngineValue::GetName() const
78 {
79 return name_;
80 }
Lock() const81 void EngineValue::Lock() const
82 {
83 mutex_.lock();
84 }
Unlock() const85 void EngineValue::Unlock() const
86 {
87 mutex_.unlock();
88 }
LockShared() const89 void EngineValue::LockShared() const
90 {
91 mutex_.lock_shared();
92 }
UnlockShared() const93 void EngineValue::UnlockShared() const
94 {
95 mutex_.unlock_shared();
96 }
ProcessOnReset(const IAny & value)97 ResetResult EngineValue::ProcessOnReset(const IAny& value)
98 {
99 SetValue(value);
100 return RESET_CONTINUE;
101 }
EventOnChanged(MetadataQuery) const102 BASE_NS::shared_ptr<IEvent> EngineValue::EventOnChanged(MetadataQuery) const
103 {
104 return event_;
105 }
GetPropertyParams() const106 EnginePropertyParams EngineValue::GetPropertyParams() const
107 {
108 return params_;
109 }
SetPropertyParams(const EnginePropertyParams & p)110 bool EngineValue::SetPropertyParams(const EnginePropertyParams& p)
111 {
112 params_ = p;
113 // todo: make new any to reflect the metadata
114 return true;
115 }
CreateAny() const116 IAny::Ptr EngineValue::CreateAny() const
117 {
118 return access_ ? access_->CreateSerializableAny() : nullptr;
119 }
ResetPendingNotify()120 bool EngineValue::ResetPendingNotify()
121 {
122 auto res = pendingNotify_;
123 pendingNotify_ = false;
124 return res;
125 }
Export(IExportContext & c) const126 ReturnError EngineValue::Export(IExportContext& c) const
127 {
128 META_NS::IAny::Ptr any = access_->CreateSerializableAny();
129 if (any->CopyFrom(*value_)) {
130 if (auto node = c.ExportValueToNode(any)) {
131 return c.SubstituteThis(node);
132 }
133 }
134 return GenericError::FAIL;
135 }
Import(IImportContext &)136 ReturnError EngineValue::Import(IImportContext&)
137 {
138 return GenericError::FAIL;
139 }
140
141 } // namespace Internal
142
143 META_END_NAMESPACE()
144