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_EXT_OBJECT_CONTAINER_H
17 #define META_EXT_OBJECT_CONTAINER_H
18
19 #include <meta/ext/object.h>
20 #include <meta/interface/interface_macros.h>
21 #include <meta/interface/intf_container.h>
22 #include <meta/interface/intf_required_interfaces.h>
23
META_BEGIN_NAMESPACE()24 META_BEGIN_NAMESPACE()
25
26 /**
27 * @brief A helper template class for implementing classes which implement the IContainer interface by
28 * using ClassId::ObjectContainer as their base class.
29 */
30 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, const META_NS::ClassInfo& SuperClassInfo,
31 class... Interfaces>
32 class CommonObjectContainerFwd : public ObjectFwd<FinalClass, ClassInfo, SuperClassInfo, IContainer,
33 IContainerProxyParent, IRequiredInterfaces, ILockable, IIterable, Interfaces...> {
34 public:
35 using Super = ObjectFwd<FinalClass, ClassInfo, SuperClassInfo, IContainer, IContainerProxyParent,
36 IRequiredInterfaces, ILockable, IIterable, Interfaces...>;
37
38 protected:
39 void SetSuperInstance(const META_NS::IObject::Ptr& aggr, const META_NS::IObject::Ptr& super) override
40 {
41 Super::SetSuperInstance(aggr, super);
42 container_ = interface_cast<IContainer>(super);
43 CORE_ASSERT(container_);
44 }
45
46 protected: // IContainer
47 BASE_NS::vector<IObject::Ptr> GetAll() const override
48 {
49 return container_->GetAll();
50 }
51 IContainer::SizeType GetSize() const override
52 {
53 return container_->GetSize();
54 }
55 BASE_NS::vector<IObject::Ptr> FindAll(const IContainer::FindOptions& options) const override
56 {
57 return container_->FindAll(options);
58 }
59 IObject::Ptr FindAny(const IContainer::FindOptions& options) const override
60 {
61 return container_->FindAny(options);
62 }
63 IObject::Ptr FindByName(BASE_NS::string_view name) const override
64 {
65 return container_->FindByName(name);
66 }
67 bool Add(const IObject::Ptr& object) override
68 {
69 return container_->Add(object);
70 }
71 bool Insert(IContainer::SizeType index, const IObject::Ptr& object) override
72 {
73 return container_->Insert(index, object);
74 }
75 using IContainer::GetAt;
76 IObject::Ptr GetAt(IContainer::SizeType index) const override
77 {
78 return container_->GetAt(index);
79 }
80 bool Remove(IContainer::SizeType index) override
81 {
82 return container_->Remove(index);
83 }
84 bool Remove(const IObject::Ptr& child) override
85 {
86 return container_->Remove(child);
87 }
88 bool Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways) override
89 {
90 return container_->Replace(child, replaceWith, addAlways);
91 }
92 bool Move(IContainer::SizeType fromIndex, IContainer::SizeType toIndex) override
93 {
94 return container_->Move(fromIndex, toIndex);
95 }
96 bool Move(const IObject::Ptr& child, IContainer::SizeType toIndex) override
97 {
98 return container_->Move(child, toIndex);
99 }
100 void RemoveAll() override
101 {
102 container_->RemoveAll();
103 }
104 META_FORWARD_EVENT(IOnChildChanged, OnAdded, container_->EventOnAdded())
105 META_FORWARD_EVENT(IOnChildChanged, OnRemoved, container_->EventOnRemoved())
106 META_FORWARD_EVENT(IOnChildMoved, OnMoved, container_->EventOnMoved())
107
108 bool IsAncestorOf(const IObject::ConstPtr& object) const override
109 {
110 return container_->IsAncestorOf(object);
111 }
112
113 protected: // IRequiredInterfaces
114 bool SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces) override
115 {
116 return META_EXT_CALL_BASE(IRequiredInterfaces, SetRequiredInterfaces(interfaces));
117 }
118 BASE_NS::vector<TypeId> GetRequiredInterfaces() const override
119 {
120 return META_EXT_CALL_BASE(IRequiredInterfaces, GetRequiredInterfaces());
121 }
122
123 protected: // IContainerProxyParent
124 bool SetProxyParent(const IContainer::Ptr& parent) override
125 {
126 return META_EXT_CALL_BASE(IContainerProxyParent, SetProxyParent(parent));
127 }
128
129 protected: // ILockable
130 void LockShared() const override
131 {
132 META_EXT_CALL_BASE(ILockable, LockShared());
133 }
134 void UnlockShared() const override
135 {
136 META_EXT_CALL_BASE(ILockable, UnlockShared());
137 }
138 void Lock() const override
139 {
140 META_EXT_CALL_BASE(ILockable, Lock());
141 }
142 void Unlock() const override
143 {
144 META_EXT_CALL_BASE(ILockable, Unlock());
145 }
146
147 protected: // IIterable
148 IterationResult Iterate(const IterationParameters& params) override
149 {
150 return META_EXT_CALL_BASE(IIterable, Iterate(params));
151 }
152 IterationResult Iterate(const IterationParameters& params) const override
153 {
154 return META_EXT_CALL_BASE(IIterable, Iterate(params));
155 }
156
157 private:
158 IContainer* container_ {};
159 };
160
161 /**
162 * @brief A helper template class for implementing classes which implement the IContainer interface by
163 * using ClassId::ObjectContainer as their base class.
164 */
165 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class... Interfaces>
166 class ObjectContainerFwd
167 : public CommonObjectContainerFwd<FinalClass, ClassInfo, ClassId::ObjectContainer, Interfaces...> {};
168
169 /**
170 * @brief A helper template class for implementing classes which implement the IContainer interface by
171 * using ClassId::ObjectFlatContainer as their base class.
172 */
173 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class... Interfaces>
174 class ObjectFlatContainerFwd
175 : public CommonObjectContainerFwd<FinalClass, ClassInfo, ClassId::ObjectFlatContainer, Interfaces...> {};
176
177 META_END_NAMESPACE()
178
179 #endif
180