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_CONTAINER_CONTAINER_BASE_H
16 #define META_SRC_CONTAINER_CONTAINER_BASE_H
17 
18 #include <shared_mutex>
19 
20 #include <meta/ext/event_impl.h>
21 #include <meta/interface/builtin_objects.h>
22 #include <meta/interface/intf_container.h>
23 #include <meta/interface/intf_iterable.h>
24 #include <meta/interface/intf_lockable.h>
25 #include <meta/interface/intf_required_interfaces.h>
26 
27 #include "../object.h"
28 
29 META_BEGIN_NAMESPACE()
30 
31 META_REGISTER_CLASS(ContainerBase, "28ff5f18-2bf0-45c7-b82f-830e94d15cc9", ObjectCategory::NO_CATEGORY);
32 
33 class ContainerBase : public IntroduceInterfaces<IContainer, IContainerProxyParent, IRequiredInterfaces, ILockable,
34                           IContainerPreTransaction, IIterable> {
35 public:
36     void SetImplementingIContainer(IObject*, IContainer*);
37 
38 public: // IContainer
39     using typename IContainer::SizeType;
40 
41     IObject::Ptr FindAnyImpl(const META_NS::IContainer::FindOptions& options, bool isFlat) const;
42     BASE_NS::vector<IObject::Ptr> FindAllImpl(const META_NS::IContainer::FindOptions& options, bool isFlat) const;
43 
44     BASE_NS::vector<IObject::Ptr> GetAll() const override;
45     IObject::Ptr GetAt(SizeType index) const override;
46     SizeType GetSize() const override;
47     IObject::Ptr FindByName(BASE_NS::string_view name) const override;
48     bool Remove(SizeType index) override;
49     bool Remove(const META_NS::IObject::Ptr& child) override;
50     bool Move(SizeType fromIndex, SizeType toIndex) override;
51     bool Move(const IObject::Ptr& child, SizeType toIndex) override;
52     bool IsAncestorOf(const IObject::ConstPtr& object) const override;
53 
54     void RemoveAll() override;
55 
56     BASE_NS::shared_ptr<IEvent> EventOnAdded() const override;
57     BASE_NS::shared_ptr<IEvent> EventOnRemoved() const override;
58     BASE_NS::shared_ptr<IEvent> EventOnMoved() const override;
59 
60     BASE_NS::shared_ptr<IEvent> EventOnAdding() const override;
61     BASE_NS::shared_ptr<IEvent> EventOnRemoving() const override;
62 
63     bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override;
64     BASE_NS::vector<TypeId> GetRequiredInterfaces() const override;
65 
66 public: // ILockable
67     void LockShared() const override;
68     void UnlockShared() const override;
69     void Lock() const override;
70     void Unlock() const override;
71 
72 public: // IContainerProxyParent
73     bool SetProxyParent(const IContainer::Ptr& parent) override;
74 
75 public: // IIterable
76     IterationResult Iterate(const IterationParameters& params) override;
77     IterationResult Iterate(const IterationParameters& params) const override;
78 
79 protected:
80     void InternalRemoveAll();
81     virtual void SetObjectParent(const IObject::Ptr& object, const IObject::Ptr& parent) const = 0;
82     ChildMovedInfo MoveInternal(SizeType fromIndex, SizeType toIndex);
83     bool IsCompatible(const IObject::Ptr& object) const;
84     bool MatchCriteria(const META_NS::IContainer::FindOptions& options, const IObject::Ptr& object) const;
85 
86 protected:
87     mutable std::shared_mutex mutex_;
88     IObject* me_;
89     IContainer* impl_ {};
90     IContainerPreTransaction* implPreTrans_ {};
91     IContainer::WeakPtr parent_; // Proxy parent (or me_ if not set)
92     BASE_NS::vector<IObject::Ptr> children_;
93     BASE_NS::vector<TypeId> required_;
94 };
95 
96 META_END_NAMESPACE()
97 
98 #endif // META_SRC_OBJECT_CONTAINER_H
99