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 #ifndef META_SRC_ENGINE_ENGINE_VALUE_H
16 #define META_SRC_ENGINE_ENGINE_VALUE_H
17 
18 #include <shared_mutex>
19 
20 #include <meta/ext/event_impl.h>
21 #include <meta/ext/minimal_object.h>
22 #include <meta/interface/engine/intf_engine_value.h>
23 #include <meta/interface/interface_helpers.h>
24 #include <meta/interface/intf_lockable.h>
25 #include <meta/interface/intf_notify_on_change.h>
26 #include <meta/interface/property/intf_stack_resetable.h>
27 
META_BEGIN_NAMESPACE()28 META_BEGIN_NAMESPACE()
29 
30 namespace Internal {
31 
32 class IEngineValueInternal : public CORE_NS::IInterface {
33     META_INTERFACE(CORE_NS::IInterface, IEngineValueInternal, "86afd68c-7924-46b8-8bb8-aeace0029945")
34 public:
35     virtual IEngineInternalValueAccess::ConstPtr GetInternalAccess() const = 0;
36     virtual EnginePropertyParams GetPropertyParams() const = 0;
37     virtual bool SetPropertyParams(const EnginePropertyParams& p) = 0;
38 };
39 
40 META_REGISTER_CLASS(EngineValue, "a8a9c80f-3501-48da-b552-f1f323ee399b", ObjectCategoryBits::NO_CATEGORY)
41 
42 class EngineValue : public MinimalObject<ClassId::EngineValue, IEngineValue, INotifyOnChange, ILockable,
43                         IStackResetable, IEngineValueInternal> {
44 public:
45     EngineValue(BASE_NS::string name, IEngineInternalValueAccess::ConstPtr access, const EnginePropertyParams& p);
46 
47     AnyReturnValue Sync(EngineSyncDirection) override;
48 
49     AnyReturnValue SetValue(const IAny& value) override;
50     const IAny& GetValue() const override;
51     bool IsCompatible(const TypeId& id) const override;
52 
53     BASE_NS::string GetName() const override;
54 
55     void Lock() const override;
56     void Unlock() const override;
57     void LockShared() const override;
58     void UnlockShared() const override;
59 
60     ResetResult ProcessOnReset(const IAny& defaultValue) override;
61 
62     BASE_NS::shared_ptr<IEvent> EventOnChanged() const override;
63 
64 private:
65     IEngineInternalValueAccess::ConstPtr GetInternalAccess() const override
66     {
67         return access_;
68     }
69     EnginePropertyParams GetPropertyParams() const override;
70     bool SetPropertyParams(const EnginePropertyParams& p) override;
71 
72 private:
73     mutable std::shared_mutex mutex_;
74     EnginePropertyParams params_;
75     IEngineInternalValueAccess::ConstPtr access_;
76     bool valueChanged_ {};
77     const BASE_NS::string name_;
78     IAny::Ptr value_;
79     BASE_NS::shared_ptr<EventImpl<IOnChanged>> event_ { new EventImpl<IOnChanged>("OnChanged") };
80 };
81 
82 } // namespace Internal
83 
84 META_END_NAMESPACE()
85 
86 #endif