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 #ifndef META_SRC_STACK_PROPERTY_H
17 #define META_SRC_STACK_PROPERTY_H
18
19 #include <atomic>
20
21 #include <meta/api/make_callback.h>
22 #include <meta/base/interface_macros.h>
23 #include <meta/base/namespace.h>
24 #include <meta/interface/property/intf_stack_property.h>
25 #include <meta/interface/serialization/intf_serializable.h>
26
27 #include "property.h"
28
META_BEGIN_NAMESPACE()29 META_BEGIN_NAMESPACE()
30 namespace Internal {
31
32 class StackProperty : public IntroduceInterfaces<GenericProperty, IStackProperty, ISerializable> {
33 using Super = IntroduceInterfaces<GenericProperty, IStackProperty, ISerializable>;
34
35 public:
36 META_NO_COPY_MOVE(StackProperty)
37
38 explicit StackProperty(BASE_NS::string name);
39 ~StackProperty() override;
40
41 AnyReturnValue SetValue(const IAny& value) override;
42 const IAny& GetValue() const override;
43
44 ReturnError PushValue(const IValue::Ptr& value) override;
45 ReturnError PopValue() override;
46 IValue::Ptr TopValue() const override;
47 ReturnError RemoveValue(const IValue::Ptr& value) override;
48 BASE_NS::vector<IValue::Ptr> GetValues(const BASE_NS::array_view<const TypeId>& ids, bool strict) const override;
49
50 ReturnError InsertModifier(IndexType pos, const IModifier::Ptr& mod) override;
51 IModifier::Ptr RemoveModifier(IndexType pos) override;
52 ReturnError RemoveModifier(const IModifier::Ptr& mod) override;
53 BASE_NS::vector<IModifier::Ptr> GetModifiers(
54 const BASE_NS::array_view<const TypeId>& ids, bool strict) const override;
55
56 AnyReturnValue SetDefaultValue(const IAny& value) override;
57 const IAny& GetDefaultValue() const override;
58
59 AnyReturnValue SetInternalAny(IAny::Ptr any) override;
60 void NotifyChange() const override;
61 bool IsDefaultValue() const override;
62 void ResetValue() override;
63 void RemoveAll() override;
64
65 ReturnError Export(IExportContext&) const override;
66 ReturnError Import(IImportContext&) override;
67
68 protected:
69 AnyReturnValue SetValueInValueStack(const IAny& value);
70 AnyReturnValue SetValueToStack(const IAny::Ptr& internal);
71 const IAny& GetValueFromStack() const;
72 const IAny& RawGetValue() const;
73 void CleanUp();
74
75 template<typename Vec>
76 bool ProcessResetables(Vec& vec);
77
78 ObjectId GetClassId() const override
79 {
80 return META_NS::ClassId::StackProperty.Id();
81 }
82
83 void InternalOnChanged()
84 {
85 requiresEvaluation_ = true;
86 if (auto p = onChangedAtomic_.load()) {
87 p->Invoke();
88 }
89 }
90
91 private:
92 mutable std::atomic<bool> requiresEvaluation_ {};
93 IAny::Ptr currentValue_;
94 IAny::Ptr defaultValue_;
95 BASE_NS::vector<IValue::Ptr> values_;
96 BASE_NS::vector<IModifier::Ptr> modifiers_;
97 ICallable::Ptr onChangedCallback_;
98 mutable bool evaluating_ {};
99 };
100
101 } // namespace Internal
102 META_END_NAMESPACE()
103
104 #endif
105