Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-container.h
1 #ifndef GAME_CONTAINER_H
2 #define GAME_CONTAINER_H
3
4 /*
5  * Copyright (c) 2020 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 #include <dali/public-api/common/dali-vector.h>
22
23 /**
24  * GameContainer is a vector which owns heap-allocated objects.
25  * Unlike vector this will call delete on the stored pointers during destruction.
26  * For example, you can define a vector of heap-allocated Node objects:
27  * @code
28  *   typedef GameContainer< GameEntity* > EntityArray;
29  *
30  *   EntityArray entites;
31  *   entites.PushBack( new GameEntity() );
32  *   // container is now responsible for calling delete on GameEntity
33  *
34  * @endcode
35  */
36 template<class T>
37 class GameContainer : public Dali::Vector<T>
38 {
39 public:
40   typedef typename Dali::Vector<T>::Iterator      Iterator;
41   typedef typename Dali::Vector<T>::ConstIterator ConstIterator;
42
43   /**
44    * Create a owner container.
45    */
46   GameContainer()
47   {
48   }
49
50   /**
51    * Non-virtual destructor; GameCache<T> is not suitable as base class.
52    */
53   ~GameContainer()
54   {
55     Clear();
56     Dali::VectorBase::Release();
57   }
58
59   /**
60    * Destroy all of the elements in the container.
61    */
62   void Clear()
63   {
64     ConstIterator end = Dali::Vector<T>::End();
65     for(Iterator iter = Dali::Vector<T>::Begin(); iter != end; ++iter)
66     {
67       delete(*iter);
68     }
69     Dali::Vector<T>::Clear();
70   }
71
72 private:
73   // Undefined copy constructor.
74   GameContainer(const GameContainer&);
75
76   // Undefined assignment operator.
77   GameContainer& operator=(const GameContainer&);
78 };
79
80 #endif // GAMECACHE_H