Fix memory leaks detected by Valgrind
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / object-owner-container.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_OBJECT_OWNER_CONTAINER
2 #define DALI_INTERNAL_SCENE_GRAPH_OBJECT_OWNER_CONTAINER
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 // EXTERNAL INCLUDES
21 #include <algorithm>
22
23 // INTERNAL INCLUDES
24 #include <dali/devel-api/common/owner-container.h>
25 #include <dali/internal/update/common/discard-queue.h>
26 #include <dali/internal/update/controllers/scene-controller.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 namespace SceneGraph
33 {
34 class UpdateManager;
35
36 /**
37  * ObjectOwnerContainer is an object which owns SceneGraph Objects.
38  * It is responsible for ensuring they are placed on a discard queue
39  * when removed from the container.
40  */
41 template< class Type >
42 class ObjectOwnerContainer
43 {
44 public:
45   typedef typename Dali::OwnerContainer< Type* > ObjectContainer;
46   typedef typename Dali::OwnerContainer< Type* >::Iterator Iterator;
47
48   /**
49    * @brief Constructor - create a new object container
50    *
51    * Object container own update side objects
52    *
53    * @param[in] sceneGraphBuffers Helper to get the correct buffer index
54    * @param[in] discardQueue Queue to discard objects that might still be in use in the render thread.
55    **/
56   ObjectOwnerContainer( SceneGraphBuffers& sceneGraphBuffers, DiscardQueue& discardQueue )
57   : mSceneController( NULL ),
58     mSceneGraphBuffers( sceneGraphBuffers ),
59     mDiscardQueue( discardQueue )
60   {
61   }
62
63   /**
64    * @brief Set the SceneController on this owner
65    *
66    * @param[in] sceneController The SceneController
67    **/
68   void SetSceneController( SceneController& sceneController )
69   {
70     mSceneController = &sceneController;
71   }
72
73   /**
74    * @brief Add an object to the owner
75    *
76    * @param[in] object Pointer to the object that will be owned
77    **/
78   void Add( Type* pointer )
79   {
80     DALI_ASSERT_DEBUG( pointer && "Pointer should not be null" );
81
82     mObjectContainer.PushBack( pointer );
83
84     pointer->ConnectToSceneGraph(*mSceneController, mSceneGraphBuffers.GetUpdateBufferIndex() );
85   }
86
87   /**
88    * @brief Remove an object from the owner.
89    *
90    * The object is put on the discard queue.
91    *
92    * @param[in] object Pointer to the object to be removed
93    **/
94   void Remove( Type* pointer )
95   {
96     DALI_ASSERT_DEBUG( pointer && "Pointer should not be null" );
97
98     // Find the object
99     Iterator match = std::find( mObjectContainer.Begin(), mObjectContainer.End(), pointer );
100     DALI_ASSERT_DEBUG( match != mObjectContainer.End() && "Should always find a match" );
101
102     mDiscardQueue.Add( mSceneGraphBuffers.GetUpdateBufferIndex(), mObjectContainer.Release( match ) );
103     pointer->DisconnectFromSceneGraph(*mSceneController, mSceneGraphBuffers.GetUpdateBufferIndex() );
104   }
105
106   /**
107    * @brief Method to call ResetToBaseValues on all the objects owned.
108    *
109    * @param[in] bufferIndex Buffer index for double buffered values.
110    **/
111   void ResetToBaseValues( BufferIndex bufferIndex )
112   {
113     for ( Iterator iter = mObjectContainer.Begin(); iter != mObjectContainer.End(); ++iter)
114     {
115       Type* object = (*iter);
116       object->ResetToBaseValues( bufferIndex );
117     }
118   }
119
120   /**
121    * @brief Method to call ConstrainObjects on all the objects owned.
122    *
123    * @param[in] bufferIndex Buffer index for double buffered values.
124    **/
125   void ConstrainObjects( BufferIndex bufferIndex )
126   {
127     for ( Iterator iter = mObjectContainer.Begin(); iter != mObjectContainer.End(); ++iter)
128     {
129       Type* object = (*iter);
130       ConstrainPropertyOwner( *object, bufferIndex );
131     }
132   }
133
134   const ObjectContainer& GetObjectContainer()
135   {
136     return mObjectContainer;
137   }
138
139 private:
140   SceneController* mSceneController;      ///< SceneController used to send messages
141   ObjectContainer mObjectContainer;       ///< Container for the objects owned
142   SceneGraphBuffers& mSceneGraphBuffers;  ///< Reference to a SceneGraphBuffers to get the indexBuffer
143   DiscardQueue& mDiscardQueue;            ///< Discard queue used for removed objects
144 };
145
146 } // namespace SceneGraph
147 } // namespace Internal
148 } // namespace Dali
149
150
151 #endif // DALI_INTERNAL_SCENE_GRAPH_OBJECT_OWNER_CONTAINER