1 #ifndef DALI_INTERNAL_OWNER_KEY_CONTAINER_H
2 #define DALI_INTERNAL_OWNER_KEY_CONTAINER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/internal/common/memory-pool-key.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/public-api/common/dali-vector.h>
26 namespace Dali::Internal
29 * OwnerKeyContainer is a vector which is responsible for killing memory-pool allocated objects.
30 * Unlike vector this will call delete on the stored objects during destruction.
32 template<class ObjectType>
33 class OwnerKeyContainer : public Dali::Vector<MemoryPoolKey<ObjectType>>
36 using KeyType = MemoryPoolKey<ObjectType>;
37 using BaseType = Vector<KeyType>;
38 using SizeType = typename BaseType::SizeType;
39 using Iterator = typename BaseType::Iterator;
40 using ConstIterator = typename BaseType::ConstIterator;
43 * Create a pointer-container.
45 OwnerKeyContainer() = default;
48 * Non-virtual destructor; OwnerKeyContainer<T> is not suitable as base class.
53 VectorBase::Release();
56 // Not copyable or movable
57 OwnerKeyContainer(const OwnerKeyContainer&) = delete; ///< Deleted copy constructor
58 OwnerKeyContainer(OwnerKeyContainer&&) = delete; ///< Deleted move constructor
59 OwnerKeyContainer& operator=(const OwnerKeyContainer&) = delete; ///< Deleted copy assignment operator
60 OwnerKeyContainer& operator=(OwnerKeyContainer&&) = delete; ///< Deleted move assignment operator
63 * Test whether the container is empty.
64 * @return True if the container is empty
68 return VectorBase::Count() == 0u;
72 * Erase an object from the container (delete from heap).
73 * @param[in] position A dereferencable iterator to an element in mContainer.
74 * @return iterator pointing to next element
76 Iterator Erase(Iterator position)
79 return BaseType::Erase(position);
83 * @brief Erases all elements that satisfy the predicate from the OwnerKeyContainer.
85 * @param[in] predicate The predicate
87 template<class Predicate>
88 void EraseIf(Predicate predicate)
90 auto begin = BaseType::Begin();
91 auto end = BaseType::End();
93 auto function = [predicate](auto& key) {
94 if(predicate(key.Get()))
105 BaseType::Erase(std::remove_if(begin, end, function), end);
109 * Erases a range of elements.(delete from heap).
111 Iterator Erase(Iterator first, Iterator last)
120 return BaseType::Erase(first, last);
124 * Erase an object from OwnerKeyContainer
125 * @param[in] object to remove
127 inline void EraseObject(ObjectType* object)
129 DALI_ASSERT_DEBUG(object && "NULL object not allowed");
131 KeyType key = ObjectType::GetKey(object);
133 Iterator iter = BaseType::Begin();
134 const ConstIterator endIter = BaseType::End();
135 for(; iter != endIter; ++iter)
146 * Release the ownership of an object, without deleting it.
147 * @param[in] position A dereferencable iterator to an element in mContainer.
148 * @post iterators are invalidated by this method.
149 * @return key of the released item
151 KeyType Release(Iterator position)
153 KeyType key = *position;
154 BaseType::Erase(position);
159 * Destroy all of the elements in the container.
163 ConstIterator end = BaseType::End();
164 for(Iterator iter = BaseType::Begin(); iter != end; ++iter)
172 * Resizes the container to hold specific amount of elements
173 * @param size to resize to
175 void Resize(SizeType size)
177 if(size < VectorBase::Count())
179 // OwnerKeyContainer owns these heap-allocated objects
180 ConstIterator end = BaseType::End();
181 for(Iterator iter = BaseType::Begin() + size; iter != end; ++iter)
186 BaseType::Resize(size);
190 * Move the ownership of objects from another OwnerKeyContainer to this one
191 * without deleting them. It will keep the original items here as well.
192 * @param[in] source where to move elements from to this OwnerKeyContainer
194 void MoveFrom(OwnerKeyContainer& source)
196 typename BaseType::SizeType sourceCount = source.Count();
197 // if source is empty, nothing to move
200 // Optimisation for the case that this is empty
203 VectorBase::Swap(source);
207 // make space for new items
208 BaseType::Reserve(VectorBase::Count() + sourceCount);
209 Iterator iter = source.Begin();
210 ConstIterator end = source.End();
211 for(; iter != end; ++iter)
214 BaseType::PushBack(key);
216 // cannot call Clear on OwnerKeyContainer as that deletes the elements
217 source.BaseType::Clear();
224 * @brief delete the contents of the pointer
225 * Function provided to allow classes to provide a custom destructor through template specialisation
226 * @param pointer to the object
228 void Delete(KeyType key)
234 } // namespace Dali::Internal
236 #endif // DALI_INTERNAL_OWNER_KEY_CONTAINER_H