1 #ifndef DALI_OWNER_CONTAINER_H
2 #define DALI_OWNER_CONTAINER_H
5 * Copyright (c) 2020 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/public-api/common/dali-common.h>
23 #include <dali/public-api/common/dali-vector.h>
28 * OwnerContainer is a vector which own heap-allocated objects.
29 * Unlike vector this will call delete on the stored pointers during destruction.
30 * For example, you can define a vector of heap-allocated Node objects:
32 * typedef OwnerContainer< Node* > NodeContainer;
34 * NodeContainer container;
35 * container.PushBack( new Node() );
36 * // container is now responsible for calling delete on Node
41 class OwnerContainer : public Dali::Vector<T>
44 using SizeType = typename Dali::Vector<T>::SizeType;
45 using Iterator = typename Vector<T>::Iterator;
46 using ConstIterator = typename Vector<T>::ConstIterator;
49 * Create a pointer-container.
51 OwnerContainer() = default;
54 * Non-virtual destructor; OwnerContainer<T> is not suitable as base class.
59 VectorBase::Release();
62 // Not copyable or movable
63 OwnerContainer(const OwnerContainer&) = delete; ///< Deleted copy constructor
64 OwnerContainer(OwnerContainer&&) = delete; ///< Deleted move constructor
65 OwnerContainer& operator=(const OwnerContainer&) = delete; ///< Deleted copy assignment operator
66 OwnerContainer& operator=(OwnerContainer&&) = delete; ///< Deleted move assignment operator
69 * Test whether the container is empty.
70 * @return True if the container is empty
74 return VectorBase::Count() == 0u;
78 * Erase an object from the container (delete from heap).
79 * @param[in] position A dereferencable iterator to an element in mContainer.
80 * @return iterator pointing to next element
82 Iterator Erase(Iterator position)
85 return Vector<T>::Erase(position);
89 * Erases a range of elements.(delete from heap).
91 Iterator Erase(Iterator first, Iterator last)
100 return Vector<T>::Erase(first, last);
104 * Erase an object from OwnerContainer
105 * @param object to remove
107 inline void EraseObject(T object)
109 DALI_ASSERT_DEBUG(object && "NULL object not allowed");
111 Iterator iter = Vector<T>::Begin();
112 const ConstIterator endIter = Vector<T>::End();
113 for(; iter != endIter; ++iter)
124 * Release the ownership of an object, without deleting it.
125 * @param[in] position A dereferencable iterator to an element in mContainer.
126 * @post iterators are invalidated by this method.
127 * @return pointer to the released item
129 T Release(Iterator position)
131 T pointer = *position;
132 Vector<T>::Erase(position);
137 * Destroy all of the elements in the container.
141 ConstIterator end = Vector<T>::End();
142 for(Iterator iter = Vector<T>::Begin(); iter != end; ++iter)
150 * Resizes the container to hold specific amount of elements
151 * @param size to resize to
153 void Resize(SizeType size)
155 if(size < VectorBase::Count())
157 // OwnerContainer owns these heap-allocated objects
158 ConstIterator end = Vector<T>::End();
159 for(Iterator iter = Vector<T>::Begin() + size; iter != end; ++iter)
164 Vector<T>::Resize(size);
168 * Move the ownership of objects from another OwnerContainer to this one
169 * without deleting them. It will keep the original items here as well.
170 * @param[in] source where to move elements from to this OwnerContainer
172 void MoveFrom(OwnerContainer& source)
174 typename Vector<T>::SizeType sourceCount = source.Count();
175 // if source is empty, nothing to move
178 // Optimisation for the case that this is empty
181 VectorBase::Swap(source);
185 // make space for new items
186 Vector<T>::Reserve(VectorBase::Count() + sourceCount);
187 Iterator iter = source.Begin();
188 ConstIterator end = source.End();
189 for(; iter != end; ++iter)
192 Vector<T>::PushBack(pointer);
194 // cannot call Clear on OwnerContainer as that deletes the elements
195 source.Vector<T>::Clear();
202 * @brief delete the contents of the pointer
203 * Function provided to allow classes to provide a custom destructor through template specialisation
204 * @param pointer to the object
206 void Delete(T pointer)
214 #endif //DALI_OWNER_CONTAINER_H