1 #ifndef __DALI_INTERNAL_OWNER_CONTAINER_H__
2 #define __DALI_INTERNAL_OWNER_CONTAINER_H__
5 * Copyright (c) 2014 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>
32 * OwnerContainer is a vector which own heap-allocated objects.
33 * Unlike vector this will call delete on the stored pointers during destruction.
34 * For example, you can define a vector of heap-allocated Node objects:
36 * typedef OwnerContainer< Node* > NodeContainer;
38 * NodeContainer container;
39 * container.PushBack( new Node() );
40 * // container is now responsible for calling delete on Node
45 class OwnerContainer : public Dali::Vector< T >
49 typedef typename Dali::Vector< T >::SizeType SizeType;
50 typedef typename Vector< T >::Iterator Iterator;
51 typedef typename Vector< T >::ConstIterator ConstIterator;
54 * Create a pointer-container.
60 * Non-virtual destructor; OwnerContainer<T> is not suitable as base class.
65 VectorBase::Release();
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 * Release the ownership of an object, without deleting it.
90 * @param[in] position A dereferencable iterator to an element in mContainer.
91 * @post iterators are invalidated by this method.
92 * @return pointer to the released item
94 T Release( Iterator position )
96 T pointer = *position;
97 Vector< T >::Erase( position );
102 * Destroy all of the elements in the container.
106 ConstIterator end = Vector< T >::End();
107 for( Iterator iter = Vector< T >::Begin(); iter != end; ++iter )
111 Vector< T >::Clear();
115 * Resizes the container to hold specific amount of elements
116 * @param size to resize to
118 void Resize( SizeType size )
120 if( size < VectorBase::Count() )
122 // OwnerContainer owns these heap-allocated objects
123 ConstIterator end = Vector< T >::End();
124 for( Iterator iter = Vector< T >::Begin() + size; iter != end; ++iter )
129 Vector< T >::Resize( size );
133 * Move the ownership of objects from another OwnerContainer to this one
134 * without deleting them. It will keep the original items here as well.
135 * @param[in] source where to move elements from to this OwnerContainer
137 void MoveFrom( OwnerContainer& source )
139 // Optimisation for the case that this is empty
146 // make space for new items
147 Reserve( VectorBase::Count() + source.Count() );
148 Iterator iter = source.Begin();
149 ConstIterator end = source.End();
150 for( ; iter != end; ++iter )
155 // cannot call Clear on OwnerContainer as that deletes the elements
156 source.Vector< T >::Clear();
162 // Undefined copy constructor.
163 OwnerContainer( const OwnerContainer& );
164 // Undefined assignment operator.
165 OwnerContainer& operator=( const OwnerContainer& );
169 } // namespace Internal
173 #endif //__DALI_INTERNAL_OWNER_CONTAINER_H__