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