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#ifndef META_SRC_CONTAINER_CONTAINER_BASE_H
168bf80f4bSopenharmony_ci#define META_SRC_CONTAINER_CONTAINER_BASE_H
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <shared_mutex>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ci#include <meta/ext/event_impl.h>
218bf80f4bSopenharmony_ci#include <meta/interface/builtin_objects.h>
228bf80f4bSopenharmony_ci#include <meta/interface/intf_container.h>
238bf80f4bSopenharmony_ci#include <meta/interface/intf_iterable.h>
248bf80f4bSopenharmony_ci#include <meta/interface/intf_lockable.h>
258bf80f4bSopenharmony_ci#include <meta/interface/intf_required_interfaces.h>
268bf80f4bSopenharmony_ci
278bf80f4bSopenharmony_ci#include "../object.h"
288bf80f4bSopenharmony_ci
298bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
308bf80f4bSopenharmony_ci
318bf80f4bSopenharmony_ciMETA_REGISTER_CLASS(ContainerBase, "28ff5f18-2bf0-45c7-b82f-830e94d15cc9", ObjectCategory::NO_CATEGORY);
328bf80f4bSopenharmony_ci
338bf80f4bSopenharmony_ciclass ContainerBase : public IntroduceInterfaces<IContainer, IContainerProxyParent, IRequiredInterfaces, ILockable,
348bf80f4bSopenharmony_ci                          IContainerPreTransaction, IIterable> {
358bf80f4bSopenharmony_cipublic:
368bf80f4bSopenharmony_ci    void SetImplementingIContainer(IObject*, IContainer*);
378bf80f4bSopenharmony_ci
388bf80f4bSopenharmony_cipublic: // IContainer
398bf80f4bSopenharmony_ci    using typename IContainer::SizeType;
408bf80f4bSopenharmony_ci
418bf80f4bSopenharmony_ci    IObject::Ptr FindAnyImpl(const META_NS::IContainer::FindOptions& options, bool isFlat) const;
428bf80f4bSopenharmony_ci    BASE_NS::vector<IObject::Ptr> FindAllImpl(const META_NS::IContainer::FindOptions& options, bool isFlat) const;
438bf80f4bSopenharmony_ci
448bf80f4bSopenharmony_ci    BASE_NS::vector<IObject::Ptr> GetAll() const override;
458bf80f4bSopenharmony_ci    IObject::Ptr GetAt(SizeType index) const override;
468bf80f4bSopenharmony_ci    SizeType GetSize() const override;
478bf80f4bSopenharmony_ci    IObject::Ptr FindByName(BASE_NS::string_view name) const override;
488bf80f4bSopenharmony_ci    bool Remove(SizeType index) override;
498bf80f4bSopenharmony_ci    bool Remove(const META_NS::IObject::Ptr& child) override;
508bf80f4bSopenharmony_ci    bool Move(SizeType fromIndex, SizeType toIndex) override;
518bf80f4bSopenharmony_ci    bool Move(const IObject::Ptr& child, SizeType toIndex) override;
528bf80f4bSopenharmony_ci    bool IsAncestorOf(const IObject::ConstPtr& object) const override;
538bf80f4bSopenharmony_ci
548bf80f4bSopenharmony_ci    void RemoveAll() override;
558bf80f4bSopenharmony_ci
568bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<IEvent> EventOnAdded() const override;
578bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<IEvent> EventOnRemoved() const override;
588bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<IEvent> EventOnMoved() const override;
598bf80f4bSopenharmony_ci
608bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<IEvent> EventOnAdding() const override;
618bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<IEvent> EventOnRemoving() const override;
628bf80f4bSopenharmony_ci
638bf80f4bSopenharmony_ci    bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override;
648bf80f4bSopenharmony_ci    BASE_NS::vector<TypeId> GetRequiredInterfaces() const override;
658bf80f4bSopenharmony_ci
668bf80f4bSopenharmony_cipublic: // ILockable
678bf80f4bSopenharmony_ci    void LockShared() const override;
688bf80f4bSopenharmony_ci    void UnlockShared() const override;
698bf80f4bSopenharmony_ci    void Lock() const override;
708bf80f4bSopenharmony_ci    void Unlock() const override;
718bf80f4bSopenharmony_ci
728bf80f4bSopenharmony_cipublic: // IContainerProxyParent
738bf80f4bSopenharmony_ci    bool SetProxyParent(const IContainer::Ptr& parent) override;
748bf80f4bSopenharmony_ci
758bf80f4bSopenharmony_cipublic: // IIterable
768bf80f4bSopenharmony_ci    IterationResult Iterate(const IterationParameters& params) override;
778bf80f4bSopenharmony_ci    IterationResult Iterate(const IterationParameters& params) const override;
788bf80f4bSopenharmony_ci
798bf80f4bSopenharmony_ciprotected:
808bf80f4bSopenharmony_ci    void InternalRemoveAll();
818bf80f4bSopenharmony_ci    virtual void SetObjectParent(const IObject::Ptr& object, const IObject::Ptr& parent) const = 0;
828bf80f4bSopenharmony_ci    ChildMovedInfo MoveInternal(SizeType fromIndex, SizeType toIndex);
838bf80f4bSopenharmony_ci    bool IsCompatible(const IObject::Ptr& object) const;
848bf80f4bSopenharmony_ci    bool MatchCriteria(const META_NS::IContainer::FindOptions& options, const IObject::Ptr& object) const;
858bf80f4bSopenharmony_ci
868bf80f4bSopenharmony_ciprotected:
878bf80f4bSopenharmony_ci    mutable std::shared_mutex mutex_;
888bf80f4bSopenharmony_ci    IObject* me_;
898bf80f4bSopenharmony_ci    IContainer* impl_ {};
908bf80f4bSopenharmony_ci    IContainerPreTransaction* implPreTrans_ {};
918bf80f4bSopenharmony_ci    IContainer::WeakPtr parent_; // Proxy parent (or me_ if not set)
928bf80f4bSopenharmony_ci    BASE_NS::vector<IObject::Ptr> children_;
938bf80f4bSopenharmony_ci    BASE_NS::vector<TypeId> required_;
948bf80f4bSopenharmony_ci};
958bf80f4bSopenharmony_ci
968bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
978bf80f4bSopenharmony_ci
988bf80f4bSopenharmony_ci#endif // META_SRC_OBJECT_CONTAINER_H
99