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