1 #ifndef __DALI_OWNER_CONTAINER_H__
2 #define __DALI_OWNER_CONTAINER_H__
5 * Copyright (c) 2017 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>
24 #include <dali/devel-api/common/dali-vector-devel.h>
30 * OwnerContainer is a vector which own heap-allocated objects.
31 * Unlike vector this will call delete on the stored pointers during destruction.
32 * For example, you can define a vector of heap-allocated Node objects:
34 * typedef OwnerContainer< Node* > NodeContainer;
36 * NodeContainer container;
37 * container.PushBack( new Node() );
38 * // container is now responsible for calling delete on Node
43 class OwnerContainer : public Dali::Vector< T >
47 typedef typename Dali::Vector< T >::SizeType SizeType;
48 typedef typename Vector< T >::Iterator Iterator;
49 typedef typename Vector< T >::ConstIterator ConstIterator;
52 * Create a pointer-container.
58 * Non-virtual destructor; OwnerContainer<T> is not suitable as base class.
63 VectorBase::Release();
67 * Test whether the container is empty.
68 * @return True if the container is empty
72 return VectorBase::Count() == 0u;
76 * Erase an object from the container (delete from heap).
77 * @param[in] position A dereferencable iterator to an element in mContainer.
78 * @return iterator pointing to next element
80 Iterator Erase( Iterator position )
83 return Vector< T >::Erase( position );
87 * Erase an object from OwnerContainer
88 * @param object to remove
90 inline void EraseObject( T object )
92 DALI_ASSERT_DEBUG( object && "NULL object not allowed" );
94 Iterator iter = Vector< T >::Begin();
95 const ConstIterator endIter = Vector< T >::End();
96 for ( ; iter != endIter; ++iter )
98 if ( *iter == object )
107 * Release the ownership of an object, without deleting it.
108 * @param[in] position A dereferencable iterator to an element in mContainer.
109 * @post iterators are invalidated by this method.
110 * @return pointer to the released item
112 T Release( Iterator position )
114 T pointer = *position;
115 Vector< T >::Erase( position );
120 * Destroy all of the elements in the container.
124 ConstIterator end = Vector< T >::End();
125 for( Iterator iter = Vector< T >::Begin(); iter != end; ++iter )
129 Vector< T >::Clear();
133 * Resizes the container to hold specific amount of elements
134 * @param size to resize to
136 void Resize( SizeType size )
138 if( size < VectorBase::Count() )
140 // OwnerContainer owns these heap-allocated objects
141 ConstIterator end = Vector< T >::End();
142 for( Iterator iter = Vector< T >::Begin() + size; iter != end; ++iter )
147 Vector< T >::Resize( size );
151 * Move the ownership of objects from another OwnerContainer to this one
152 * without deleting them. It will keep the original items here as well.
153 * @param[in] source where to move elements from to this OwnerContainer
155 void MoveFrom( OwnerContainer& source )
157 typename Vector< T >::SizeType sourceCount = source.Count();
158 // if source is empty, nothing to move
159 if( sourceCount > 0u )
161 // Optimisation for the case that this is empty
164 VectorBase::Swap( source );
168 // make space for new items
169 Vector< T >::Reserve( VectorBase::Count() + sourceCount );
170 Iterator iter = source.Begin();
171 ConstIterator end = source.End();
172 for( ; iter != end; ++iter )
175 Vector< T >::PushBack( pointer );
177 // cannot call Clear on OwnerContainer as that deletes the elements
178 source.Vector< T >::Clear();
185 // Undefined copy constructor.
186 OwnerContainer( const OwnerContainer& );
187 // Undefined assignment operator.
188 OwnerContainer& operator=( const OwnerContainer& );
191 * @brief delete the contents of the pointer
192 * Function provided to allow classes to provide a custom destructor through template specialisation
193 * @param pointer to the object
195 void Delete( T pointer )
205 #endif //__DALI_OWNER_CONTAINER_H__