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
19META_BEGIN_NAMESPACE()
20
21namespace Internal {
22
23EngineValue::EngineValue(
24    BASE_NS::string name, IEngineInternalValueAccess::ConstPtr access, const EnginePropertyParams& p)
25    : params_(p), access_(BASE_NS::move(access)), name_(BASE_NS::move(name)), value_(access_->CreateAny())
26{}
27
28AnyReturnValue EngineValue::Sync(EngineSyncDirection dir)
29{
30    if (!params_.handle) {
31        return AnyReturn::INVALID_ARGUMENT;
32    }
33    if (dir == EngineSyncDirection::TO_ENGINE || (dir == EngineSyncDirection::AUTO && valueChanged_)) {
34        if (valueChanged_) {
35        valueChanged_ = false;
36        return access_->SyncToEngine(*value_, params_) ? AnyReturn::NOTHING_TO_DO : AnyReturn::FAIL;
37        }
38        return AnyReturn::NOTHING_TO_DO;
39    }
40    return access_->SyncFromEngine(params_, *value_);
41}
42AnyReturnValue EngineValue::SetValue(const IAny& value)
43{
44    AnyReturnValue res = value_->CopyFrom(value);
45    valueChanged_ |= static_cast<bool>(res);
46    if (params_.pushValueToEngineDirectly) {
47        if (valueChanged_) {
48            if (!params_.handle) {
49                return AnyReturn::INVALID_ARGUMENT;
50            }
51            valueChanged_ = false;
52            res = access_->SyncToEngine(*value_, params_) ? AnyReturn::NOTHING_TO_DO : AnyReturn::FAIL;
53        }
54    }
55    return res;
56}
57const IAny& EngineValue::GetValue() const
58{
59    return *value_;
60}
61bool EngineValue::IsCompatible(const TypeId& id) const
62{
63    return META_NS::IsCompatible(*value_, id);
64}
65BASE_NS::string EngineValue::GetName() const
66{
67    return name_;
68}
69void EngineValue::Lock() const
70{
71    mutex_.lock();
72}
73void EngineValue::Unlock() const
74{
75    mutex_.unlock();
76}
77void EngineValue::LockShared() const
78{
79    mutex_.lock_shared();
80}
81void EngineValue::UnlockShared() const
82{
83    mutex_.unlock_shared();
84}
85ResetResult EngineValue::ProcessOnReset(const IAny&)
86{
87    return RESET_CONTINUE;
88}
89BASE_NS::shared_ptr<IEvent> EngineValue::EventOnChanged() const
90{
91    return event_;
92}
93EnginePropertyParams EngineValue::GetPropertyParams() const
94{
95    return params_;
96}
97bool EngineValue::SetPropertyParams(const EnginePropertyParams& p)
98{
99    params_ = p;
100    return true;
101}
102
103} // namespace Internal
104
105META_END_NAMESPACE()
106