Merge changes I8783ad29,I2c860a84 into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / resetter-container.h
1 #ifndef DALI_INTERNAL_UPDATE_RESETTER_CONTAINER_H
2 #define DALI_INTERNAL_UPDATE_RESETTER_CONTAINER_H
3
4 /*
5  * Copyright (c) 2023 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 namespace Dali::Internal::SceneGraph
21 {
22
23 /**
24  * Template class to manage node/property resetters
25  */
26 template<class ResetterType>
27 class ResetterContainer
28 {
29 public:
30   // std::list offers fast delete and fast iteration.
31   using ContainerType = std::list<ResetterType*>;
32   using Iterator      = typename ContainerType::iterator;
33   using ConstIterator = typename ContainerType::const_iterator;
34
35   ResetterContainer() = default;
36
37   ResetterContainer(const ResetterContainer&) = delete;
38   ResetterContainer(ResetterContainer&&)      = delete;
39
40   /**
41    * Ensure all resetters are destroyed when the container is destroyed
42    */
43   ~ResetterContainer()
44   {
45     Clear();
46   }
47
48   /**
49    * Add a resetter to the container
50    * @param[in] resetterPtr A pointer to the resetter.
51    * The container takes ownership.
52    */
53   void PushBack(ResetterType* resetterPtr)
54   {
55     mContainer.push_back(resetterPtr);
56   }
57
58   /**
59    * @return true if the container is empty
60    */
61   bool Empty()
62   {
63     return mContainer.empty();
64   }
65
66   /**
67    * Clear the container, destroying all extant resetters.
68    */
69   void Clear()
70   {
71     auto iter = Begin();
72     while(iter != End())
73     {
74       iter = EraseObject(iter);
75     }
76   }
77
78   /**
79    * @return an iterator to the start of the container
80    */
81   Iterator Begin()
82   {
83     return mContainer.begin();
84   }
85   /**
86    * Support for C++11 Range-based for loop
87    * @return an iterator to the start of the container.
88    */
89   Iterator begin()
90   {
91     return mContainer.begin();
92   }
93   /**
94    * @return an iterator to the start of the container
95    */
96   ConstIterator Begin() const
97   {
98     return mContainer.begin();
99   }
100   /**
101    * Support for C++11 Range-based for loop
102    * @return an iterator to the start of the container.
103    */
104   ConstIterator begin() const
105   {
106     return mContainer.begin();
107   }
108
109   /**
110    * @return an iterator to the end of the container.
111    */
112   Iterator End()
113   {
114     return mContainer.end();
115   }
116   /**
117    * Support for C++11 Range-based for loop
118    * @return an iterator to the end of the container.
119    */
120   Iterator end()
121   {
122     return mContainer.end();
123   }
124   /**
125    * @return an iterator to the end of the container.
126    */
127   ConstIterator End() const
128   {
129     return mContainer.end();
130   }
131   /**
132    * Support for C++11 Range-based for loop
133    * @return an iterator to the end of the container.
134    */
135   ConstIterator end() const
136   {
137     return mContainer.end();
138   }
139
140   /**
141    * Erase a resetter from the container
142    */
143   Iterator EraseObject(Iterator iter)
144   {
145     delete *iter;
146     return mContainer.erase(iter);
147   }
148
149   /**
150    * Iterate over the container, resetting all the referenced
151    * properties. If a resetter has finished (e.g. it's animation /
152    * constraint has ended, or it's baked 2 values), then it is removed
153    * from the list.
154    * @param[in] bufferIndex The buffer index of the property to be reset
155    */
156   void ResetToBaseValues(BufferIndex bufferIndex)
157   {
158     if(!mContainer.empty())
159     {
160       auto end  = mContainer.end();
161       auto iter = mContainer.begin();
162       while(iter != end)
163       {
164         (*iter)->ResetToBaseValue(bufferIndex);
165         if((*iter)->IsFinished())
166         {
167           iter = EraseObject(iter);
168         }
169         else // Skip to next element
170         {
171           ++iter;
172         }
173       }
174     }
175   }
176
177 private:
178   ContainerType mContainer; ///< The list of resetters
179 };
180
181 } // namespace Dali::Internal::SceneGraph
182
183 #endif // DALI_INTERNAL_UPDATE_RESETTER_CONTAINER_H