1 #ifndef DALI_INTERNAL_MEMORY_POOL_KEY_H
2 #define DALI_INTERNAL_MEMORY_POOL_KEY_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dali/internal/common/fixed-size-memory-pool.h>
22 namespace Dali::Internal
25 * MemoryPoolKey is a 32bit replacement for pointers to objects stored
26 * within MemoryPools. In a 32 bit environment, it is an actual ptr.
28 * @tparam Class The class of object stored within a memory pool. The
29 * class grants limited access to the actual memory pool.
31 * The key has pointer semantics.
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:
38 * struct TypeTraits<SpecializedClass> : public BasicTypes<SpecializedClass>
42 * IS_TRIVIAL_TYPE = true
46 template<typename Class>
50 using KeyType = FixedSizeMemoryPool::KeyType;
53 * Default Constructor - generates an empty key.
61 * Constructor - Construct a key object from an int key.
62 * @param[in] aKey A key of an object in the pool
64 explicit MemoryPoolKey(KeyType aKey)
71 * @param[in] rhs Key to copy
73 MemoryPoolKey(const MemoryPoolKey<Class>& rhs)
80 * @param[in] rhs Key to copy
82 MemoryPoolKey& operator=(MemoryPoolKey& rhs)
90 * @param[in] rhs Key to copy
92 const MemoryPoolKey& operator=(const MemoryPoolKey& rhs)
100 * @return A pointer to the object that the key references, or nullptr
102 Class* operator->() const
104 #if defined(__LP64__)
105 return Class::Get(key);
107 return static_cast<Class*>(key);
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
117 #if defined(__LP64__)
118 return Class::Get(key);
120 return static_cast<Class*>(key);
125 * Boolean operator (tests against INVALID, not 0)
126 * @return true if the key is not invalid
128 explicit operator bool() const
130 return key != INVALID;
135 * @param[in] rhs The key to test against
136 * @return true if the keys match
138 bool operator==(const MemoryPoolKey<Class>& rhs) const
140 return key == rhs.key;
144 * Inequality operator
145 * @param[in] rhs The key to test against
146 * @return true if the keys don't match
148 bool operator!=(const MemoryPoolKey<Class>& rhs) const
150 return key != rhs.key;
154 * Equality operator for nullptr
155 * @param[in] np nullptr
156 * @return true if the key is invalid
158 bool operator==(std::nullptr_t np) const
160 return key == INVALID;
164 * Inequality operator for nullptr
165 * @param[in] np nullptr
166 * @return true if the keys is valid
168 bool operator!=(std::nullptr_t np) const
170 return key != INVALID;
173 uint32_t Value() const
175 return reinterpret_cast<uint32_t>(key);
179 // Ensure that INVALID constant can't be used directly.
180 #if defined(__LP64__)
181 static const KeyType INVALID{0xffffffff}; ///< Null or Invalid constant.
183 static constexpr KeyType INVALID{nullptr};
186 KeyType key{INVALID}; ///< The actual key.
189 } // namespace Dali::Internal
191 #endif // DALI_INTERNAL_MEMORY_POOL_KEY_H