• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <meta/base/namespace.h>
20 #include <meta/ext/minimal_object.h>
21 #include <meta/interface/builtin_objects.h>
22 #include <meta/interface/intf_attachment_container.h>
23 #include <meta/interface/intf_object_hierarchy_observer.h>
24 
25 #include "container/flat_container.h"
26 
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28 
29 class AttachmentContainer : public IntroduceInterfaces<MinimalObject, FlatContainer, IAttachmentContainer> {
30     using Super = IntroduceInterfaces<MinimalObject, FlatContainer, IAttachmentContainer>;
31     META_IMPLEMENT_OBJECT_TYPE_INTERFACE(ClassId::AttachmentContainer)
32     META_NO_COPY_MOVE(AttachmentContainer)
33 public:
34     AttachmentContainer();
35     ~AttachmentContainer() override;
36 
37 protected: // IContainer
38     bool Add(const IObject::Ptr& object) override;
39     bool Insert(IContainer::SizeType index, const IObject::Ptr& object) override;
40     bool Remove(IContainer::SizeType index) override;
41     bool Remove(const IObject::Ptr& child) override;
42     bool Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways) override;
43     void RemoveAll() override;
44     bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override;
45 
46 protected: // IAttachmentContainer
47     bool Initialize(const META_NS::IAttach::Ptr& owner) override;
48     using IAttachmentContainer::Attach;
49     bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
50     bool Attach(IContainer::SizeType pos, const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override;
51     using IAttachmentContainer::Detach;
52     bool Detach(const IObject::Ptr& attachment) override;
53     BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) override;
54     void RemoveAllAttachments() override;
55     IObject::Ptr FindByName(const BASE_NS::string& name) const override;
56 
57     template<typename Interface>
58     typename Interface::Ptr GetOwner() const
59     {
60         return interface_pointer_cast<Interface>(owner_);
61     }
62 
63     bool DetachFromOld(const IAttach::Ptr& me, const IObject::Ptr& attachment, bool& mine);
64     bool InternalAttach(const IObject::Ptr& attachment);
65     template<typename T>
66     bool InternalAttach(const T& obj)
67     {
68         return InternalAttach(interface_pointer_cast<IObject>(obj));
69     }
70 
71 private:
72     void RemovedFromContainer(const ChildChangedInfo& info);
73     void AddingToContainer(const ChildChangedInfo& info, bool& success);
74 
75     bool AlreadyAttached(const IObject::Ptr& object);
76     IAttach::WeakPtr owner_;
77     static constexpr IContainer::SizeType N_POS = std::numeric_limits<IContainer::SizeType>::max();
78 };
79 
80 META_END_NAMESPACE()
81 
82 #endif
83