Dali 3D User Interface Engine
owner-container.h
Go to the documentation of this file.
1 #ifndef __DALI_OWNER_CONTAINER_H__
2 #define __DALI_OWNER_CONTAINER_H__
3 
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20 
21 // INTERNAL INCLUDES
24 
25 namespace Dali
26 {
27 
41 template< class T >
42 class OwnerContainer : public Dali::Vector< T >
43 {
44 public:
45 
47  typedef typename Vector< T >::Iterator Iterator;
49 
54  { }
55 
60  {
61  Clear();
63  }
64 
69  bool IsEmpty() const
70  {
71  return VectorBase::Count() == 0u;
72  }
73 
79  Iterator Erase( Iterator position )
80  {
81  delete (*position);
82  return Vector< T >::Erase( position );
83  }
84 
91  T Release( Iterator position )
92  {
93  T pointer = *position;
94  Vector< T >::Erase( position );
95  return pointer;
96  }
97 
101  void Clear()
102  {
104  for( Iterator iter = Vector< T >::Begin(); iter != end; ++iter )
105  {
106  delete (*iter);
107  }
109  }
110 
115  void Resize( SizeType size )
116  {
117  if( size < VectorBase::Count() )
118  {
119  // OwnerContainer owns these heap-allocated objects
121  for( Iterator iter = Vector< T >::Begin() + size; iter != end; ++iter )
122  {
123  delete (*iter);
124  }
125  }
126  Vector< T >::Resize( size );
127  }
128 
134  void MoveFrom( OwnerContainer& source )
135  {
136  typename Vector< T >::SizeType sourceCount = source.Count();
137  // if source is empty, nothing to move
138  if( sourceCount > 0u )
139  {
140  // Optimisation for the case that this is empty
141  if( IsEmpty() )
142  {
143  VectorBase::Swap( source );
144  }
145  else
146  {
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 )
152  {
153  T pointer = *iter;
154  Vector< T >::PushBack( pointer );
155  }
156  // cannot call Clear on OwnerContainer as that deletes the elements
157  source.Vector< T >::Clear();
158  }
159  }
160  }
161 
162 private:
163 
164  // Undefined copy constructor.
165  OwnerContainer( const OwnerContainer& );
166  // Undefined assignment operator.
168 
169 };
170 
171 } // namespace Dali
172 
173 #endif //__DALI_OWNER_CONTAINER_H__
Dali Docs Home
Read more about Dali