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_ATTACHMENT_CONTAINER_H
17 #define META_SRC_ATTACHMENT_CONTAINER_H
18
19 #include <shared_mutex>
20
21 #include <base/containers/unordered_map.h>
22
23 #include <meta/base/namespace.h>
24 #include <meta/ext/object.h>
25 #include <meta/ext/object_container.h>
26 #include <meta/interface/builtin_objects.h>
27 #include <meta/interface/intf_attachment_container.h>
28 #include <meta/interface/intf_object_hierarchy_observer.h>
29
META_BEGIN_NAMESPACE()30 META_BEGIN_NAMESPACE()
31
32 class AttachmentContainer final : public ObjectContainerFwd<AttachmentContainer, ClassId::AttachmentContainer,
33 IAttachmentContainer, IContainerPreTransaction> {
34 using Super = ObjectContainerFwd<AttachmentContainer, ClassId::AttachmentContainer, IAttachmentContainer,
35 IContainerPreTransaction>;
36
37 public:
38 ~AttachmentContainer() override;
39 AttachmentContainer();
40 META_NO_COPY_MOVE(AttachmentContainer)
41 // ILifecycle
42 bool Build(const IMetadata::Ptr& data) override;
43
44 protected: // IDerived
45 void SetSuperInstance(const IObject::Ptr& aggr, const IObject::Ptr& super) override;
46
47 protected: // IContainer
48 bool Add(const IObject::Ptr& object) override;
49 bool Insert(IContainer::SizeType index, const IObject::Ptr& object) override;
50 bool Remove(IContainer::SizeType index) override;
51 bool Remove(const IObject::Ptr& child) override;
52 bool Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways) override;
53 void RemoveAll() override;
54 bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override;
55
56 protected: // IContainerPreTransaction
57 META_FORWARD_EVENT(IOnChildChanging, OnAdding, transaction_->EventOnAdding());
58 META_FORWARD_EVENT(IOnChildChanging, OnRemoving, transaction_->EventOnRemoving());
59
60 protected: // IAttachmentContainer
61 bool Initialize(const META_NS::IAttach::Ptr& owner) override;
62 bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
63 bool Attach(IContainer::SizeType pos, const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
64 bool Detach(const IObject::Ptr& attachment) override;
65 BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) override;
66 void RemoveAllAttachments() override;
67 IObject::Ptr FindByName(const BASE_NS::string& name) const override;
68
69 private:
70 void RemovedFromContainer(const ChildChangedInfo& info);
71 void AddingToContainer(const ChildChangedInfo& info, bool& success);
72
73 bool AlreadyAttached(const IObject::Ptr& object);
74 IContainerPreTransaction* transaction_ {};
75 META_NS::IAttach::WeakPtr owner_;
76 BASE_NS::vector<BASE_NS::pair<IObject*, IObject::WeakPtr>> addingContexts_;
77 static constexpr IContainer::SizeType N_POS = std::numeric_limits<IContainer::SizeType>::max();
78 };
79
80 META_END_NAMESPACE()
81
82 #endif
83