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