1 #ifndef __DALI_OWNER_CONTAINER_H__
2 #define __DALI_OWNER_CONTAINER_H__
5 * Copyright (c) 2015 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>
29 * OwnerContainer is a vector which own heap-allocated objects.
30 * Unlike vector this will call delete on the stored pointers during destruction.
31 * For example, you can define a vector of heap-allocated Node objects:
33 * typedef OwnerContainer< Node* > NodeContainer;
35 * NodeContainer container;
36 * container.PushBack( new Node() );
37 * // container is now responsible for calling delete on Node
42 class OwnerContainer : public Dali::Vector< T >
46 typedef typename Dali::Vector< T >::SizeType SizeType;
47 typedef typename Vector< T >::Iterator Iterator;
48 typedef typename Vector< T >::ConstIterator ConstIterator;
51 * Create a pointer-container.
57 * Non-virtual destructor; OwnerContainer<T> is not suitable as base class.
62 VectorBase::Release();
66 * Test whether the container is empty.
67 * @return True if the container is empty
71 return VectorBase::Count() == 0u;
75 * Erase an object from the container (delete from heap).
76 * @param[in] position A dereferencable iterator to an element in mContainer.
77 * @return iterator pointing to next element
79 Iterator Erase( Iterator position )
82 return Vector< T >::Erase( position );
86 * Release the ownership of an object, without deleting it.
87 * @param[in] position A dereferencable iterator to an element in mContainer.
88 * @post iterators are invalidated by this method.
89 * @return pointer to the released item
91 T Release( Iterator position )
93 T pointer = *position;
94 Vector< T >::Erase( position );
99 * Destroy all of the elements in the container.
103 ConstIterator end = Vector< T >::End();
104 for( Iterator iter = Vector< T >::Begin(); iter != end; ++iter )
108 Vector< T >::Clear();
112 * Resizes the container to hold specific amount of elements
113 * @param size to resize to
115 void Resize( SizeType size )
117 if( size < VectorBase::Count() )
119 // OwnerContainer owns these heap-allocated objects
120 ConstIterator end = Vector< T >::End();
121 for( Iterator iter = Vector< T >::Begin() + size; iter != end; ++iter )
126 Vector< T >::Resize( size );
130 * Move the ownership of objects from another OwnerContainer to this one
131 * without deleting them. It will keep the original items here as well.
132 * @param[in] source where to move elements from to this OwnerContainer
134 void MoveFrom( OwnerContainer& source )
136 typename Vector< T >::SizeType sourceCount = source.Count();
137 // if source is empty, nothing to move
138 if( sourceCount > 0u )
140 // Optimisation for the case that this is empty
143 VectorBase::Swap( source );
147 // make space for new items
148 Vector< T >::Reserve( VectorBase::Count() + sourceCount );
149 Iterator iter = source.Begin();
150 ConstIterator end = source.End();
151 for( ; iter != end; ++iter )
154 Vector< T >::PushBack( pointer );
156 // cannot call Clear on OwnerContainer as that deletes the elements
157 source.Vector< T >::Clear();
164 // Undefined copy constructor.
165 OwnerContainer( const OwnerContainer& );
166 // Undefined assignment operator.
167 OwnerContainer& operator=( const OwnerContainer& );
173 #endif //__DALI_OWNER_CONTAINER_H__