SamRecordPool Class Reference

Collaboration diagram for SamRecordPool:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SamRecordPool ()
 Constructor that sets there to be no max number of allocated records.
 SamRecordPool (int maxNumRecs)
 Constructor that sets the maximum number of allocated records.
 ~SamRecordPool ()
 Destructor.
SamRecordgetRecord ()
 Get a SamRecord.
void releaseRecord (SamRecord *record)
 If record is not NULL, adds it back to the free list.
void setMaxAllocatedRecs (int maxNumRecs)
 Set the maximum number of records allowed to be allocated.

Detailed Description

Definition at line 25 of file SamRecordPool.h.


Constructor & Destructor Documentation

SamRecordPool::SamRecordPool ( int  maxNumRecs  ) 

Constructor that sets the maximum number of allocated records.

Parameters:
maxNumRecs maximum number of allocated records (-1 means no max)

Definition at line 29 of file SamRecordPool.cpp.

00030     : myFreeSamRecords(),
00031       myMaxAllowedRecs(maxNumRecs),
00032       myAllocatedRecs(0)
00033 {
00034 }

SamRecordPool::~SamRecordPool (  ) 

Destructor.

Any records that were allocated without calling "releaseRecord" will not get cleaned up and the user will need to delete them.

Definition at line 37 of file SamRecordPool.cpp.

00038 {
00039     // Loop through the stack deleting the free records.
00040     while (!myFreeSamRecords.empty())
00041     {
00042         delete(myFreeSamRecords.front());
00043         myFreeSamRecords.pop();
00044     }
00045 }


Member Function Documentation

SamRecord * SamRecordPool::getRecord (  ) 

Get a SamRecord.

If records are already allocated and free use those, if not and there are still more that are allowed to be allocated, allocate a new one. If no more records are allowed to be allocated, NULL is returned. NOTE: The user should call releaseRecord when done using the record. If the user deletes the record instead, it still counts as allocated when comparing against the maxNumRecs but cannot be reused.

Returns:
pointer to a SamRecord available for use, or NULL if no more records are allowed to be allocated.

Definition at line 48 of file SamRecordPool.cpp.

00049 {
00050     // Get new samRecord.
00051     SamRecord* returnSam = NULL;
00052     if(!myFreeSamRecords.empty())
00053     {
00054         // have free already allocated records, so get one of those.
00055         returnSam = myFreeSamRecords.front();
00056         myFreeSamRecords.pop();
00057     }
00058     else if((myMaxAllowedRecs == -1) || (myAllocatedRecs < myMaxAllowedRecs))
00059     {
00060         // There were no free records, but either there is no max or
00061         // there is still room to allocate more.
00062         returnSam = new SamRecord();
00063         ++myAllocatedRecs;
00064         if(returnSam == NULL)
00065         {
00066             // Failed allocation.
00067             throw(std::runtime_error("Failed to allocate SamRecord"));
00068         }
00069     }
00070     else
00071     {
00072         // There are no more free ones and we have already hit the
00073         // max number allowed to be allocated, so return NULL.
00074         // The user will have to release some or update the max.
00075         returnSam = NULL;
00076     }
00077     return(returnSam);
00078 }

void SamRecordPool::releaseRecord ( SamRecord record  ) 

If record is not NULL, adds it back to the free list.

If record is NULL, nothing is done.

Parameters:
record pointer to a record that is no longer being used and is available for reuse.

Definition at line 81 of file SamRecordPool.cpp.

Referenced by SamCoordOutput::flush().

00082 {
00083     if(record == NULL)
00084     {
00085         // Nothing to release, so just return.
00086         return;
00087     }
00088 
00089     // Release the samRecord to be reused.
00090     myFreeSamRecords.push(record);
00091 }

void SamRecordPool::setMaxAllocatedRecs ( int  maxNumRecs  ) 

Set the maximum number of records allowed to be allocated.

If more than the new value have already been allocated, it does not deallocate any, and will continue to reuse the already allocated records, but it will not allocate any additional records.

Definition at line 94 of file SamRecordPool.cpp.

00095 {
00096     myMaxAllowedRecs = maxNumRecs;
00097 }


The documentation for this class was generated from the following files:
Generated on Mon Feb 11 13:45:28 2013 for libStatGen Software by  doxygen 1.6.3