2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBase_ObjectManagerImpl.h
19 * @brief This is the header file for the %_ObjectManagerImpl class.
21 * This file contains the declarations of the %_ObjectManagerImpl class.
23 #ifndef _FBASE_INTERNAL_OBJECT_MANAGER_IMPL_H_
24 #define _FBASE_INTERNAL_OBJECT_MANAGER_IMPL_H_
26 #include <FBaseObject.h>
27 #include <FBaseColLinkedListT.h>
28 #include <FBaseRtMutex.h>
29 #include <FBaseSysLog.h>
31 namespace Tizen { namespace Base
34 class _ObjectManagerImpl
37 struct _HandleElement;
41 * This is the default constructor for this class.
45 _ObjectManagerImpl(void)
48 __slotCapacity = DEFAULT_CAPACITY;
49 __pSlots = new _HandleElement[__slotCapacity];
51 __pLock = new (std::nothrow) Tizen::Base::Runtime::Mutex();
52 SysTryLog(NID_BASE, __pLock != null, "Failed to allocate the mutex instance");
54 result r = __pLock->Create();
55 SysTryLog(NID_BASE, !IsFailed(r), "Failed to create the mutex instance");
59 * This is the destructor for this class.
63 virtual ~_ObjectManagerImpl(void)
70 * This is the copy constructor for this class.
74 _ObjectManagerImpl(_ObjectManagerImpl& pObjectManagerImpl)
76 __slotCount = pObjectManagerImpl.__slotCount;
77 __slotCapacity = pObjectManagerImpl.__slotCapacity;
78 __pSlots = new _HandleElement[pObjectManagerImpl.__slotCapacity];
80 for (unsigned int i = 0; i < pObjectManagerImpl.__slotCapacity; i++)
82 __pSlots[i] = pObjectManagerImpl.__pSlots[i];
85 __pLock = new (std::nothrow) Tizen::Base::Runtime::Mutex();
86 SysTryLog(NID_BASE, __pLock != null, "Failed to allocate the mutex instance");
88 result r = __pLock->Create();
89 SysTryLog(NID_BASE, !IsFailed(r), "Failed to create the mutex instance");
93 * This is the assignement operator for this class.
97 _ObjectManagerImpl& operator =(const _ObjectManagerImpl& pObjectManagerImpl)
99 //Check for self-assignment
100 if (this == &pObjectManagerImpl)
108 __slotCount = pObjectManagerImpl.__slotCount;
109 __slotCapacity = pObjectManagerImpl.__slotCapacity;
110 __pSlots = new _HandleElement[pObjectManagerImpl.__slotCapacity];
112 for (unsigned int i = 0; i < pObjectManagerImpl.__slotCapacity; i++)
114 __pSlots[i] = pObjectManagerImpl.__pSlots[i];
121 * Registers the specified object to the object manager.
124 * @return The handle of the object
125 * @param[in] object An object to register
126 * @exception E_SUCCESS The method was successful.
129 int Register(void* pObject)
131 result r = __pLock->Acquire();
132 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
136 if (__freeSlots.GetCount() == 0)
138 unsigned int index = __slotCount;
141 if (__slotCount == __slotCapacity)
146 InitializeHandle(index, handle);
148 __pSlots[index].__id = handle.__id;
149 __pSlots[index].__pObject = pObject;
153 unsigned int index = 0;
155 __freeSlots.GetAt(0, index);
156 __freeSlots.RemoveAt(0);
158 InitializeHandle(index, handle);
160 __pSlots[index].__id = handle.__id;
161 __pSlots[index].__pObject = pObject;
164 r = __pLock->Release();
165 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
167 return handle.__handle;
171 * Unregisters the specified handle from the object manager.
174 * @return The object specified by the handle
175 * @param[in] handle A handle to release
176 * @exception E_SUCCESS The method was successful.
177 * @exception E_OBJ_NOT_FOUND The specified handle was not found.
180 void* Unregister(unsigned int handle)
182 result r = __pLock->Acquire();
183 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
185 void* pObject = null;
186 const unsigned int index = ((_Handle*)&handle)->__index;
188 if (index >= __slotCount)
190 r = __pLock->Release();
191 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
193 SetLastResult(E_OBJ_NOT_FOUND);
197 _HandleElement& element = __pSlots[index];
198 if (element.__id != ((_Handle*)&handle)->__id)
200 r = __pLock->Release();
201 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
203 SetLastResult(E_OBJ_NOT_FOUND);
207 pObject = element.__pObject;
210 element.__pObject = null;
212 __freeSlots.Add(index);
214 r = __pLock->Release();
215 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
221 * Gets an object specified by the handle.
224 * @return The object specified by the handle
225 * @param[in] handle A handle to release
226 * @exception E_SUCCESS The method was successful.
227 * @exception E_OBJ_NOT_FOUND The specified handle was not found.
229 const void* GetObject(unsigned int handle) const
231 result r = __pLock->Acquire();
232 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
234 const unsigned int index = ((_Handle*)&handle)->__index;
236 if (index >= __slotCount)
238 r = __pLock->Release();
239 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
241 SetLastResult(E_OBJ_NOT_FOUND);
245 const _HandleElement& element = __pSlots[index];
246 if (element.__id != ((_Handle*)&handle)->__id)
248 r = __pLock->Release();
249 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
251 SetLastResult(E_OBJ_NOT_FOUND);
255 r = __pLock->Release();
256 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
258 return element.__pObject;
262 * Gets an object specified by the handle.
265 * @return The object specified by the handle
266 * @param[in] handle A handle to release
267 * @exception E_SUCCESS The method was successful.
268 * @exception E_OBJ_NOT_FOUND The specified handle was not found.
270 void* GetObject(unsigned int handle)
272 const void* pObj = (static_cast<const _ObjectManagerImpl*>(this))->GetObject(handle);
273 return const_cast<void*>(pObj);
277 * Gets the number of objects in the object manager.
280 * @return The number of object
282 int GetObjectCount(void) const
284 result r = __pLock->Acquire();
285 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
287 int cnt = static_cast <int>(__slotCount - __freeSlots.GetCount());
289 r = __pLock->Release();
290 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
295 bool IsValidHandle(unsigned int handle) const
297 const void* pObject = GetObject(handle);
310 void InitializeHandle(unsigned int index, _Handle& handle)
312 static unsigned int autoId = 0;
313 if (++autoId > MAX_ID)
318 handle.__index = index;
319 handle.__id = autoId;
322 void ExpandSlots(void)
324 unsigned int newCapacity = __slotCapacity*2;
325 _HandleElement* pNewArray = new _HandleElement[newCapacity];
327 for (unsigned int i = 0; i < __slotCapacity; i++)
329 pNewArray[i] = __pSlots[i];
334 __pSlots = pNewArray;
335 __slotCapacity = newCapacity;
338 void SetElementAt(int index)
342 _HandleElement GetElementAt(int index)
344 _HandleElement element;
354 MAX_INDEX = (1 << MAX_BITS_INDEX) - 1,
355 MAX_ID = (1 << MAX_BITS_ID) - 1,
360 unsigned int __handle;
363 unsigned __index : MAX_BITS_INDEX;
364 unsigned __id : MAX_BITS_ID;
368 struct _HandleElement
370 _HandleElement(unsigned int id = 0, void* pObject = null)
376 _HandleElement(const _HandleElement& element)
379 __pObject = element.__pObject;
382 _HandleElement& operator = (const _HandleElement& element)
384 if (this == &element)
390 __pObject = element.__pObject;
399 static const int DEFAULT_CAPACITY = 64;
402 _HandleElement* __pSlots;
403 unsigned int __slotCount;
404 unsigned int __slotCapacity;
405 Tizen::Base::Collection::LinkedListT<unsigned int> __freeSlots;
406 Tizen::Base::Runtime::Mutex* __pLock;
410 #endif // _FBASE_INTERNAL_OBJECT_MANAGER_IMPL_H_