Merge branch 'devel/master' into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / common / owner-container.h
1 #ifndef __DALI_INTERNAL_OWNER_CONTAINER_H__
2 #define __DALI_INTERNAL_OWNER_CONTAINER_H__
3
4 /*
5  * Copyright (c) 2014 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
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/dali-vector.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 /**
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:
35  * @code
36  *   typedef OwnerContainer< Node* > NodeContainer;
37  *
38  *   NodeContainer container;
39  *   container.PushBack( new Node() );
40  *   // container is now responsible for calling delete on Node
41  *
42  * @endcode
43  */
44 template< class T >
45 class OwnerContainer : public Dali::Vector< T >
46 {
47 public:
48
49   typedef typename Dali::Vector< T >::SizeType SizeType;
50   typedef typename Vector< T >::Iterator Iterator;
51   typedef typename Vector< T >::ConstIterator ConstIterator;
52
53   /**
54    * Create a pointer-container.
55    */
56   OwnerContainer()
57   { }
58
59   /**
60    * Non-virtual destructor; OwnerContainer<T> is not suitable as base class.
61    */
62   ~OwnerContainer()
63   {
64     Clear();
65     VectorBase::Release();
66   }
67
68   /**
69    * Test whether the container is empty.
70    * @return True if the container is empty
71    */
72   bool IsEmpty() const
73   {
74     return VectorBase::Count() == 0u;
75   }
76
77   /**
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
81    */
82   Iterator Erase( Iterator position )
83   {
84     delete (*position);
85     return Vector< T >::Erase( position );
86   }
87
88   /**
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
93    */
94   T Release( Iterator position )
95   {
96     T pointer = *position;
97     Vector< T >::Erase( position );
98     return pointer;
99   }
100
101   /**
102    * Destroy all of the elements in the container.
103    */
104   void Clear()
105   {
106     ConstIterator end = Vector< T >::End();
107     for( Iterator iter = Vector< T >::Begin(); iter != end; ++iter )
108     {
109       delete (*iter);
110     }
111     Vector< T >::Clear();
112   }
113
114   /**
115    * Resizes the container to hold specific amount of elements
116    * @param size to resize to
117    */
118   void Resize( SizeType size )
119   {
120     if( size < VectorBase::Count() )
121     {
122       // OwnerContainer owns these heap-allocated objects
123       ConstIterator end = Vector< T >::End();
124       for( Iterator iter = Vector< T >::Begin() + size; iter != end; ++iter )
125       {
126         delete (*iter);
127       }
128     }
129     Vector< T >::Resize( size );
130   }
131
132   /**
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
136    */
137   void MoveFrom( OwnerContainer& source )
138   {
139     typename Vector< T >::SizeType sourceCount = source.Count();
140     // if source is empty, nothing to move
141     if( sourceCount > 0u )
142     {
143       // Optimisation for the case that this is empty
144       if( IsEmpty() )
145       {
146         VectorBase::Swap( source );
147       }
148       else
149       {
150         // make space for new items
151         Vector< T >::Reserve( VectorBase::Count() + sourceCount );
152         Iterator iter = source.Begin();
153         ConstIterator end = source.End();
154         for( ; iter != end; ++iter )
155         {
156           T pointer = *iter;
157           Vector< T >::PushBack( pointer );
158         }
159         // cannot call Clear on OwnerContainer as that deletes the elements
160         source.Vector< T >::Clear();
161       }
162     }
163   }
164
165 private:
166
167   // Undefined copy constructor.
168   OwnerContainer( const OwnerContainer& );
169   // Undefined assignment operator.
170   OwnerContainer& operator=( const OwnerContainer& );
171
172 };
173
174 } // namespace Internal
175
176 } // namespace Dali
177
178 #endif //__DALI_INTERNAL_OWNER_CONTAINER_H__