SamRecordPool.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdexcept>
00019 #include "SamRecordPool.h"
00020
00021 SamRecordPool::SamRecordPool()
00022 : myFreeSamRecords(),
00023 myMaxAllowedRecs(-1),
00024 myAllocatedRecs(0)
00025 {
00026 }
00027
00028
00029 SamRecordPool::SamRecordPool(int maxNumRecs)
00030 : myFreeSamRecords(),
00031 myMaxAllowedRecs(maxNumRecs),
00032 myAllocatedRecs(0)
00033 {
00034 }
00035
00036
00037 SamRecordPool::~SamRecordPool()
00038 {
00039
00040 while (!myFreeSamRecords.empty())
00041 {
00042 delete(myFreeSamRecords.front());
00043 myFreeSamRecords.pop();
00044 }
00045 }
00046
00047
00048 SamRecord* SamRecordPool::getRecord()
00049 {
00050
00051 SamRecord* returnSam = NULL;
00052 if(!myFreeSamRecords.empty())
00053 {
00054
00055 returnSam = myFreeSamRecords.front();
00056 myFreeSamRecords.pop();
00057 }
00058 else if((myMaxAllowedRecs == -1) || (myAllocatedRecs < myMaxAllowedRecs))
00059 {
00060
00061
00062 returnSam = new SamRecord();
00063 ++myAllocatedRecs;
00064 if(returnSam == NULL)
00065 {
00066
00067 throw(std::runtime_error("Failed to allocate SamRecord"));
00068 }
00069 }
00070 else
00071 {
00072
00073
00074
00075 returnSam = NULL;
00076 }
00077 return(returnSam);
00078 }
00079
00080
00081 void SamRecordPool::releaseRecord(SamRecord* record)
00082 {
00083 if(record == NULL)
00084 {
00085
00086 return;
00087 }
00088
00089
00090 myFreeSamRecords.push(record);
00091 }
00092
00093
00094 void SamRecordPool::setMaxAllocatedRecs(int maxNumRecs)
00095 {
00096 myMaxAllowedRecs = maxNumRecs;
00097 }