123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // Copyright @ 2014 Hangzhou Topzen Ltd.
- // Author: Yab Bin (yanb@es.zju.edu.cn) @ 2014-12
- //
- #ifndef __OBJECTPOOL_H
- #define __OBJECTPOOL_H
- #include "BaseTypes.h"
- #include "BaseHeaders.h"
- #include "Locks.h"
- namespace tzc {
- template <typename T>
- class ObjectPool {
- public:
- enum __OBJPOOL_ERROR {
- OPSucess = 0,
- OPNoFreeObj,
- OPNoTaskObj,
- OPEmptyPool
- };
- public:
- ObjectPool();
- ~ObjectPool();
- // return 0 if successful, -1 while error occurs
- TZ_INT PopObj(T & Obj);
- TZ_INT PopFreeObj(T & Obj);
- TZ_INT PopTaskObj(T & Obj);
- void PushFreeObj(T & Obj);
- void PushTaskObj(T & Obj);
- TZ_INT GetFreeCnt();
- TZ_INT GetTaskCnt();
- TZ_INT GetOPError() const;
- private:
- void SetOPError(const TZ_INT errcode);
- private:
- Mutex m_mtxFreePool;
- std::list<T> m_vFreePool;
- Mutex m_mtxTaskPool;
- std::list<T> m_vTaskPool;
- TZ_INT m_OPErrno;
- };
- template <typename T>
- ObjectPool<T>::ObjectPool():
- m_OPErrno(OPSucess)
- {
- }
- template <typename T>
- ObjectPool<T>::~ObjectPool()
- {
- m_vFreePool.clear();
- m_vTaskPool.clear();
- }
- template <typename T>
- TZ_INT ObjectPool<T>::GetOPError() const
- {
- return m_OPErrno;
- }
- template <typename T>
- void ObjectPool<T>::SetOPError(const TZ_INT errcode)
- {
- m_OPErrno = errcode;
- }
- template<typename T>
- TZ_INT ObjectPool<T>::PopObj(T & Obj)
- {
- if (this->GetTaskCnt())
- {
- return this->PopTaskObj(Obj);
- }
- if (this->GetFreeCnt())
- {
- return this->PopFreeObj(Obj);
- }
- this->SetOPError(OPEmptyPool);
- return -1;
- }
- template<typename T>
- TZ_INT ObjectPool<T>::PopFreeObj(T & Obj)
- {
- m_mtxFreePool.Lock();
- if(m_vFreePool.empty())
- {
- m_mtxFreePool.Unlock();
- this->SetOPError(OPNoFreeObj);
- return -1;
- }
- Obj = m_vFreePool.front();
- m_vFreePool.pop_front();
- m_mtxFreePool.Unlock();
- this->SetOPError(OPSucess);
- return 0;
- }
- template <typename T>
- TZ_INT ObjectPool<T>::PopTaskObj(T & Obj)
- {
- m_mtxTaskPool.Lock();
- if(m_vTaskPool.empty())
- {
- m_mtxTaskPool.Unlock();
- this->SetOPError(OPNoTaskObj);
-
- return -1;
- }
- Obj = m_vTaskPool.front();
- m_vTaskPool.pop_front();
- m_mtxTaskPool.Unlock();
- this->SetOPError(OPSucess);
- return 0;
- }
- template <typename T>
- void ObjectPool<T>::PushFreeObj(T & Obj)
- {
- m_mtxFreePool.Lock();
- m_vFreePool.push_back(Obj);
- m_mtxFreePool.Unlock();
- }
- template <typename T>
- void ObjectPool<T>::PushTaskObj(T & Obj)
- {
- m_mtxTaskPool.Lock();
- m_vTaskPool.push_back(Obj);
- m_mtxTaskPool.Unlock();
- }
- template <typename T>
- TZ_INT ObjectPool<T>::GetFreeCnt()
- {
- m_mtxFreePool.Lock();
- TZ_INT iTmpCount(m_vFreePool.size());
- m_mtxFreePool.Unlock();
- return iTmpCount;
- }
- template <typename T>
- TZ_INT ObjectPool<T>::GetTaskCnt()
- {
- m_mtxTaskPool.Lock();
- TZ_INT iTmpCount(m_vTaskPool.size());
- m_mtxTaskPool.Unlock();
- return iTmpCount;
- }
- } // namespace tzc
- #endif /* ----- #ifndef __OBJECTPOOL_H ----- */
|