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