Merge "Remove uniform hash" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / common / memory-pool-key.h
1 #ifndef DALI_INTERNAL_MEMORY_POOL_KEY_H
2 #define DALI_INTERNAL_MEMORY_POOL_KEY_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 #include <dali/internal/common/fixed-size-memory-pool.h>
21
22 namespace Dali::Internal
23 {
24 /**
25  * MemoryPoolKey is a 32bit replacement for pointers to objects stored
26  * within MemoryPools. In a 32 bit environment, it is an actual ptr.
27  *
28  * @tparam Class The class of object stored within a memory pool. The
29  * class grants limited access to the actual memory pool.
30  *
31  * The key has pointer semantics.
32  *
33  * As this has a copy constructor, it's not a trivial type, however because
34  * it encapsulates an integer, specialized types can use Dali::Vector by
35  * overriding the type traits using:
36  *
37  * template<>
38  * struct TypeTraits<SpecializedClass> : public BasicTypes<SpecializedClass>
39  * {
40  *   enum
41  *   {
42  *     IS_TRIVIAL_TYPE = true
43  *   };
44  * };
45  */
46 template<typename Class>
47 class MemoryPoolKey
48 {
49 public:
50   using KeyType = FixedSizeMemoryPool::KeyType;
51
52   /**
53    * Default Constructor - generates an empty key.
54    */
55   MemoryPoolKey()
56   : key(INVALID)
57   {
58   }
59
60   /**
61    * Constructor - Construct a key object from an int key.
62    * @param[in] aKey A key of an object in the pool
63    */
64   explicit MemoryPoolKey(KeyType aKey)
65   : key(aKey)
66   {
67   }
68
69   /**
70    * Copy constructor.
71    * @param[in] rhs Key to copy
72    */
73   MemoryPoolKey(const MemoryPoolKey<Class>& rhs)
74   {
75     key = rhs.key;
76   }
77
78   /**
79    * Assignment operator
80    * @param[in] rhs Key to copy
81    */
82   MemoryPoolKey& operator=(MemoryPoolKey& rhs)
83   {
84     key = rhs.key;
85     return *this;
86   }
87
88   /**
89    * Assignment operator
90    * @param[in] rhs Key to copy
91    */
92   const MemoryPoolKey& operator=(const MemoryPoolKey& rhs)
93   {
94     key = rhs.key;
95     return *this;
96   }
97
98   /**
99    * Member operator
100    * @return A pointer to the object that the key references, or nullptr
101    */
102   Class* operator->() const
103   {
104 #if defined(__LP64__)
105     return Class::Get(key);
106 #else
107     return static_cast<Class*>(key);
108 #endif
109   }
110
111   /**
112    * Method to get a pointer to the object referenced by the key
113    * @return A pointer to the referenced object, or nullptr if not in the memory pool
114    */
115   Class* Get() const
116   {
117 #if defined(__LP64__)
118     return Class::Get(key);
119 #else
120     return static_cast<Class*>(key);
121 #endif
122   }
123
124   /**
125    * Boolean operator (tests against INVALID, not 0)
126    * @return true if the key is not invalid
127    */
128   explicit operator bool() const
129   {
130     return key != INVALID;
131   }
132
133   /**
134    * Equality operator
135    * @param[in] rhs The key to test against
136    * @return true if the keys match
137    */
138   bool operator==(const MemoryPoolKey<Class>& rhs) const
139   {
140     return key == rhs.key;
141   }
142
143   /**
144    * Inequality operator
145    * @param[in] rhs The key to test against
146    * @return true if the keys don't match
147    */
148   bool operator!=(const MemoryPoolKey<Class>& rhs) const
149   {
150     return key != rhs.key;
151   }
152
153   /**
154    * Equality operator for nullptr
155    * @param[in] np nullptr
156    * @return true if the key is invalid
157    */
158   bool operator==(std::nullptr_t np) const
159   {
160     return key == INVALID;
161   }
162
163   /**
164    * Inequality operator for nullptr
165    * @param[in] np nullptr
166    * @return true if the keys is valid
167    */
168   bool operator!=(std::nullptr_t np) const
169   {
170     return key != INVALID;
171   }
172
173   uint32_t Value() const
174   {
175     return reinterpret_cast<uint32_t>(key);
176   }
177
178 private:
179   // Ensure that INVALID constant can't be used directly.
180 #if defined(__LP64__)
181   static const KeyType INVALID{0xffffffff}; ///< Null or Invalid constant.
182 #else
183   static constexpr KeyType INVALID{nullptr};
184 #endif
185
186   KeyType key{INVALID}; ///< The actual key.
187 };
188
189 } // namespace Dali::Internal
190
191 #endif // DALI_INTERNAL_MEMORY_POOL_KEY_H