18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#ifndef META_SRC_ATTACHMENT_CONTAINER_H
178bf80f4bSopenharmony_ci#define META_SRC_ATTACHMENT_CONTAINER_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <shared_mutex>
208bf80f4bSopenharmony_ci
218bf80f4bSopenharmony_ci#include <base/containers/unordered_map.h>
228bf80f4bSopenharmony_ci
238bf80f4bSopenharmony_ci#include <meta/base/namespace.h>
248bf80f4bSopenharmony_ci#include <meta/ext/object.h>
258bf80f4bSopenharmony_ci#include <meta/ext/object_container.h>
268bf80f4bSopenharmony_ci#include <meta/interface/builtin_objects.h>
278bf80f4bSopenharmony_ci#include <meta/interface/intf_attachment_container.h>
288bf80f4bSopenharmony_ci#include <meta/interface/intf_object_hierarchy_observer.h>
298bf80f4bSopenharmony_ci
308bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
318bf80f4bSopenharmony_ci
328bf80f4bSopenharmony_ciclass AttachmentContainer final : public ObjectContainerFwd<AttachmentContainer, ClassId::AttachmentContainer,
338bf80f4bSopenharmony_ci                                      IAttachmentContainer, IContainerPreTransaction> {
348bf80f4bSopenharmony_ci    using Super = ObjectContainerFwd<AttachmentContainer, ClassId::AttachmentContainer, IAttachmentContainer,
358bf80f4bSopenharmony_ci        IContainerPreTransaction>;
368bf80f4bSopenharmony_ci
378bf80f4bSopenharmony_cipublic:
388bf80f4bSopenharmony_ci    ~AttachmentContainer() override;
398bf80f4bSopenharmony_ci    AttachmentContainer();
408bf80f4bSopenharmony_ci    META_NO_COPY_MOVE(AttachmentContainer)
418bf80f4bSopenharmony_ci    // ILifecycle
428bf80f4bSopenharmony_ci    bool Build(const IMetadata::Ptr& data) override;
438bf80f4bSopenharmony_ci
448bf80f4bSopenharmony_ciprotected: // IDerived
458bf80f4bSopenharmony_ci    void SetSuperInstance(const IObject::Ptr& aggr, const IObject::Ptr& super) override;
468bf80f4bSopenharmony_ci
478bf80f4bSopenharmony_ciprotected: // IContainer
488bf80f4bSopenharmony_ci    bool Add(const IObject::Ptr& object) override;
498bf80f4bSopenharmony_ci    bool Insert(IContainer::SizeType index, const IObject::Ptr& object) override;
508bf80f4bSopenharmony_ci    bool Remove(IContainer::SizeType index) override;
518bf80f4bSopenharmony_ci    bool Remove(const IObject::Ptr& child) override;
528bf80f4bSopenharmony_ci    bool Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways) override;
538bf80f4bSopenharmony_ci    void RemoveAll() override;
548bf80f4bSopenharmony_ci    bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override;
558bf80f4bSopenharmony_ci
568bf80f4bSopenharmony_ciprotected: // IContainerPreTransaction
578bf80f4bSopenharmony_ci    META_FORWARD_EVENT(IOnChildChanging, OnAdding, transaction_->EventOnAdding());
588bf80f4bSopenharmony_ci    META_FORWARD_EVENT(IOnChildChanging, OnRemoving, transaction_->EventOnRemoving());
598bf80f4bSopenharmony_ci
608bf80f4bSopenharmony_ciprotected: // IAttachmentContainer
618bf80f4bSopenharmony_ci    bool Initialize(const META_NS::IAttach::Ptr& owner) override;
628bf80f4bSopenharmony_ci    bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
638bf80f4bSopenharmony_ci    bool Attach(IContainer::SizeType pos, const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
648bf80f4bSopenharmony_ci    bool Detach(const IObject::Ptr& attachment) override;
658bf80f4bSopenharmony_ci    BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) override;
668bf80f4bSopenharmony_ci    void RemoveAllAttachments() override;
678bf80f4bSopenharmony_ci    IObject::Ptr FindByName(const BASE_NS::string& name) const override;
688bf80f4bSopenharmony_ci
698bf80f4bSopenharmony_ciprivate:
708bf80f4bSopenharmony_ci    void RemovedFromContainer(const ChildChangedInfo& info);
718bf80f4bSopenharmony_ci    void AddingToContainer(const ChildChangedInfo& info, bool& success);
728bf80f4bSopenharmony_ci
738bf80f4bSopenharmony_ci    bool AlreadyAttached(const IObject::Ptr& object);
748bf80f4bSopenharmony_ci    IContainerPreTransaction* transaction_ {};
758bf80f4bSopenharmony_ci    META_NS::IAttach::WeakPtr owner_;
768bf80f4bSopenharmony_ci    BASE_NS::vector<BASE_NS::pair<IObject*, IObject::WeakPtr>> addingContexts_;
778bf80f4bSopenharmony_ci    static constexpr IContainer::SizeType N_POS = std::numeric_limits<IContainer::SizeType>::max();
788bf80f4bSopenharmony_ci};
798bf80f4bSopenharmony_ci
808bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
818bf80f4bSopenharmony_ci
828bf80f4bSopenharmony_ci#endif
83