Created
February 23, 2019 07:25
-
-
Save BriFuture/24e4dbef4d3a46eb542d6aa3c22318ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma execution_character_set("utf-8") | |
#include "cameradatapool.h" | |
#include <QDebug> | |
template<class T> | |
DataObjPool<T>::DataObjPool(int size) | |
{ | |
T *pt; | |
pool.reserve(size); | |
for(int i = 0; i < size; i++) { | |
pt = new T(); | |
pt->setValid(true); | |
pt->setId(i); | |
pool.append(pt); | |
} | |
numAvailable = size; | |
} | |
template <class T> template <class T2> | |
DataObjPool<T>::operator DataObjPool<T2>() { | |
Stack<T2> StackT2; | |
for (int i = 0; i < m_size; i++) { | |
StackT2.push((T2)m_pT[m_size - 1]); | |
} | |
return StackT2; | |
} | |
RawDataObjPool::RawDataObjPool(int size) : DataObjPool(size) | |
{ | |
} | |
LineDataPool::LineDataPool(int size) : DataObjPool(size) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CAMERADATAPOOL_H | |
#define CAMERADATAPOOL_H | |
#include <QObject> | |
#include <QMap> | |
#include <QDebug> | |
#include "cameradata.h" | |
template<class T> | |
class DataObjPool | |
{ | |
public: | |
virtual ~DataObjPool() { | |
qDeleteAll(pool); | |
numAvailable = 0; | |
} | |
T *getAvailable() { | |
if( numAvailable == 0 ) { | |
return 0; | |
} | |
T *item; | |
for(int i = 0; i < pool.size(); i++) { | |
item = pool[i]; | |
if(item->isValid()) { | |
item->setValid(false); | |
numAvailable -= 1; | |
return item; | |
} | |
} | |
return 0; | |
} | |
/** | |
* @brief getItemCirclely | |
* @return | |
* 循环使用池中的对象,如果池中的对象已被标记为 inValid, 那么直接调用 | |
*/ | |
T *getItemCirclely() { | |
if(circleFlag == pool.size()) { | |
circleFlag = 0; | |
} | |
T *item = pool[circleFlag]; | |
if(!item->isValid()) | |
item->reset(); | |
item->setValid(false); | |
circleFlag += 1; | |
return item; | |
} | |
T *get(int id) { | |
return pool[id]; | |
} | |
inline bool release(T *dobj) { | |
dobj->reset(); | |
numAvailable += 1; | |
return true; | |
} | |
int releaseTimeout(int now, int timeout = 100) { | |
int releaseCount = 0; | |
for(int i = 0; i < pool.size(); i++) { | |
T *item = pool[i]; | |
if(now > item->getGenerateMs() + timeout) { | |
item->reset(); | |
numAvailable += 1; | |
releaseCount += 1; | |
} | |
} | |
return releaseCount; | |
} | |
void releaseAll() { | |
for(int i = 0; i < pool.size(); i++) { | |
T *item = pool[i]; | |
if(item->isValid()) { | |
continue; | |
} | |
item->reset(); | |
numAvailable += 1; | |
} | |
} | |
int getNumAvailable() const { | |
return numAvailable; | |
} | |
template<class T2> operator DataObjPool<T2>(); | |
protected: | |
DataObjPool(int size = 100); | |
private: | |
QList<T *> pool; | |
int numAvailable = 0; | |
int circleFlag = 0; | |
}; | |
class RawDataObjPool: public DataObjPool<CameraData> | |
{ | |
public: | |
RawDataObjPool(int size = 100); | |
}; | |
class LineDataPool : public DataObjPool<PreProcessData> | |
{ | |
public: | |
LineDataPool(int size = 100); | |
}; | |
#endif // CAMERADATAPOOL_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment