[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / size-negotiation / memory-pool-relayout-container.h
1 #ifndef DALI_INTERNAL_MEMORY_POOL_RELAYOUT_CONTAINER_H
2 #define DALI_INTERNAL_MEMORY_POOL_RELAYOUT_CONTAINER_H
3
4 /*
5  * Copyright (c) 2024 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 // EXTERNAL INCLUDES
22 #include <memory> // for std::unique_ptr
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/actors/actor.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/size-negotiation/relayout-container.h>
28
29 #include <dali/integration-api/ordered-set.h>
30 #include <dali/internal/common/memory-pool-object-allocator.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 /**
37  * @brief Container to encapsulate information required for relayout.
38  *
39  * Uses a memory pool to manage data allocations.
40  */
41 class MemoryPoolRelayoutContainer : public RelayoutContainer
42 {
43 public:
44   /**
45    * Struct to store the relayout information
46    */
47   struct RelayoutInfo
48   {
49     Dali::Actor actor; ///< The actor to relayout
50     Vector2     size;  ///< The desired size of the actor
51     struct RelayoutInfoHash
52     {
53       std::size_t operator()(const RelayoutInfo* x) const noexcept
54       {
55         return reinterpret_cast<std::size_t>(x->actor.GetObjectPtr());
56       }
57     };
58     struct RelayoutInfoCompare
59     {
60       bool operator()(const RelayoutInfo* lhs, const RelayoutInfo* rhs) const noexcept
61       {
62         return lhs->actor == rhs->actor;
63       }
64     };
65   };
66
67   /**
68    * @brief Default constructor
69    *
70    * @param objectAllocator A memory pool that can allocate memory for RelayoutInfos
71    */
72   MemoryPoolRelayoutContainer(MemoryPoolObjectAllocator<RelayoutInfo>& objectAllocator);
73
74   /**
75    * Virtual destructor
76    */
77   ~MemoryPoolRelayoutContainer() override;
78
79   /**
80    * @brief Add relayout information to the container if it does'nt already exist
81    *
82    * @param actor The actor to relayout
83    * @param size The size to relayout
84    */
85   void Add(const Dali::Actor& actor, const Vector2& size) override;
86
87   /**
88    * @brief Remove information from the container
89    */
90   void PopBack();
91
92   /**
93    * @brief Retrieve relayout information for the latest added
94    *
95    * @param[out] actorOut Latest added actor
96    * @param[out] sizeOt Latest added size
97    */
98   void GetBack(Dali::Actor& actorOut, Vector2& sizeOut) const;
99
100   /**
101    * @brief The count of information in the container
102    */
103   size_t Size() const;
104
105   /**
106    * @brief Set the capacity of the container
107    *
108    * @param capacity The new capacity for the container
109    */
110   void Reserve(size_t capacity);
111
112   /**
113    * @brief Reset the container, freeing all memory
114    */
115   void Clear();
116
117 private:
118   /**
119    * @brief Returns if the container contains the actor or not
120    *
121    * @param actor The actor to search for
122    * @return Return if the actor was found or not
123    */
124   bool Contains(const Dali::Actor& actor);
125
126 private:
127   using RelayoutInfoContainer = Dali::Integration::OrderedSet<RelayoutInfo, false, RelayoutInfo::RelayoutInfoHash, RelayoutInfo::RelayoutInfoCompare>;
128
129   RelayoutInfoContainer mRelayoutInfos; ///< The list of relayout infos
130
131   MemoryPoolObjectAllocator<RelayoutInfo>& mAllocator; ///< The memory pool from which the infos are allocated
132
133   std::unique_ptr<RelayoutInfo> mDummyRelayoutInfo; ///< Dummy pointer that will be used to compare relayout infors what we already holded.
134 };
135
136 } // namespace Internal
137
138 } // namespace Dali
139
140 #endif // DALI_INTERNAL_MEMORY_POOL_RELAYOUT_CONTAINER_H