[dali_2.3.39] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / size-negotiation / memory-pool-relayout-container.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // FILE HEADER
19 #include "memory-pool-relayout-container.h"
20
21 namespace Dali
22 {
23 namespace Internal
24 {
25 MemoryPoolRelayoutContainer::MemoryPoolRelayoutContainer(MemoryPoolObjectAllocator<RelayoutInfo>& objectAllocator)
26 : mAllocator(objectAllocator)
27 {
28   mDummyRelayoutInfo.reset(new RelayoutInfo());
29 }
30
31 MemoryPoolRelayoutContainer::~MemoryPoolRelayoutContainer() = default;
32
33 bool MemoryPoolRelayoutContainer::Contains(const Dali::Actor& actor)
34 {
35   // Store actor into dummy info.
36   // It will be used for check comparision.
37   mDummyRelayoutInfo->actor = actor;
38
39   bool ret = (mRelayoutInfos.Find(mDummyRelayoutInfo.get()) != mRelayoutInfos.End());
40
41   // Reset empty handle for deference.
42   mDummyRelayoutInfo->actor = Dali::Actor();
43   return ret;
44 }
45
46 void MemoryPoolRelayoutContainer::Add(const Dali::Actor& actor, const Vector2& size)
47 {
48   if(!Contains(actor))
49   {
50     void*         ptr  = mAllocator.AllocateRaw();
51     RelayoutInfo* info = new(ptr) RelayoutInfo();
52     info->actor        = actor;
53     info->size         = size;
54
55     mRelayoutInfos.PushBack(info);
56   }
57 }
58
59 void MemoryPoolRelayoutContainer::PopBack()
60 {
61   if(mRelayoutInfos.Count() > 0)
62   {
63     RelayoutInfoContainer::Iterator back = mRelayoutInfos.End();
64     back--;
65     RelayoutInfo* info = *back;
66     mRelayoutInfos.Erase(back);
67
68     // Need to be destroyed after mRelayoutInfos erased.
69     mAllocator.Destroy(info);
70   }
71 }
72
73 void MemoryPoolRelayoutContainer::GetBack(Dali::Actor& actorOut, Vector2& sizeOut) const
74 {
75   if(mRelayoutInfos.Count() > 0)
76   {
77     RelayoutInfoContainer::ConstIterator back = mRelayoutInfos.End();
78     back--;
79     RelayoutInfo* info = *back;
80     actorOut           = info->actor;
81     sizeOut            = info->size;
82   }
83 }
84
85 size_t MemoryPoolRelayoutContainer::Size() const
86 {
87   return mRelayoutInfos.Count();
88 }
89
90 void MemoryPoolRelayoutContainer::Reserve(size_t capacity)
91 {
92   mRelayoutInfos.Reserve(capacity);
93 }
94
95 void MemoryPoolRelayoutContainer::Clear()
96 {
97   for(auto& info : mRelayoutInfos)
98   {
99     mAllocator.Destroy(info);
100   }
101
102   mRelayoutInfos.Clear();
103 }
104
105 } // namespace Internal
106
107 } // namespace Dali