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 "engine_value.h"
17
18 #include <core/property/intf_property_api.h>
19
20 #include "../any.h"
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 : params_(p), access_(BASE_NS::move(access)), name_(BASE_NS::move(name)), value_(access_->CreateAny(p.property))
28 {}
29
Sync(EngineSyncDirection dir)30 AnyReturnValue EngineValue::Sync(EngineSyncDirection dir)
31 {
32 if (!params_.handle) {
33 return AnyReturn::INVALID_ARGUMENT;
34 }
35 if (dir == EngineSyncDirection::TO_ENGINE || (dir == EngineSyncDirection::AUTO && valueChanged_)) {
36 if (valueChanged_) {
37 valueChanged_ = false;
38 return access_->SyncToEngine(*value_, params_) ? AnyReturn::NOTHING_TO_DO : AnyReturn::FAIL;
39 }
40 return AnyReturn::NOTHING_TO_DO;
41 }
42 auto res = access_->SyncFromEngine(params_, *value_);
43 pendingNotify_ = pendingNotify_ || (res && res != AnyReturn::NOTHING_TO_DO);
44 return res;
45 }
SetValue(const IAny & value)46 AnyReturnValue EngineValue::SetValue(const IAny& value)
47 {
48 AnyReturnValue res = value_->CopyFrom(value);
49 valueChanged_ |= static_cast<bool>(res);
50 if (params_.pushValueToEngineDirectly) {
51 if (valueChanged_) {
52 if (!params_.handle) {
53 return AnyReturn::INVALID_ARGUMENT;
54 }
55 valueChanged_ = false;
56 // this returns intentionally NOTHING_TO_DO for now as the ets scene plugin completely breaks if notify
57 // about this.
58 res = access_->SyncToEngine(*value_, params_) ? AnyReturn::NOTHING_TO_DO : AnyReturn::FAIL;
59 }
60 }
61 return res;
62 }
GetValue() const63 const IAny& EngineValue::GetValue() const
64 {
65 if (params_.pushValueToEngineDirectly) {
66 if (!params_.handle || !access_->SyncFromEngine(params_, *value_)) {
67 return INVALID_ANY;
68 }
69 }
70 return *value_;
71 }
IsCompatible(const TypeId & id) const72 bool EngineValue::IsCompatible(const TypeId& id) const
73 {
74 return META_NS::IsCompatible(*value_, id);
75 }
GetName() const76 BASE_NS::string EngineValue::GetName() const
77 {
78 return name_;
79 }
Lock() const80 void EngineValue::Lock() const
81 {
82 mutex_.lock();
83 }
Unlock() const84 void EngineValue::Unlock() const
85 {
86 mutex_.unlock();
87 }
LockShared() const88 void EngineValue::LockShared() const
89 {
90 mutex_.lock_shared();
91 }
UnlockShared() const92 void EngineValue::UnlockShared() const
93 {
94 mutex_.unlock_shared();
95 }
ProcessOnReset(const IAny &)96 ResetResult EngineValue::ProcessOnReset(const IAny&)
97 {
98 return RESET_CONTINUE;
99 }
EventOnChanged(MetadataQuery) const100 BASE_NS::shared_ptr<IEvent> EngineValue::EventOnChanged(MetadataQuery) const
101 {
102 return event_;
103 }
GetPropertyParams() const104 EnginePropertyParams EngineValue::GetPropertyParams() const
105 {
106 return params_;
107 }
SetPropertyParams(const EnginePropertyParams & p)108 bool EngineValue::SetPropertyParams(const EnginePropertyParams& p)
109 {
110 params_ = p;
111 return true;
112 }
CreateAny() const113 IAny::Ptr EngineValue::CreateAny() const
114 {
115 return access_->CreateAny(params_.property);
116 }
ResetPendingNotify()117 bool EngineValue::ResetPendingNotify()
118 {
119 auto res = pendingNotify_;
120 pendingNotify_ = false;
121 return res;
122 }
123
124 } // namespace Internal
125
126 META_END_NAMESPACE()
127