libStatGen Software  1
SamRecordPool Class Reference

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:
maxNumRecsmaximum number of allocated records (-1 means no max)

Definition at line 29 of file SamRecordPool.cpp.

    : myFreeSamRecords(),
      myMaxAllowedRecs(maxNumRecs),
      myAllocatedRecs(0)
{
}

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.

{
    // Loop through the stack deleting the free records.
    while (!myFreeSamRecords.empty())
    {
        delete(myFreeSamRecords.front());
        myFreeSamRecords.pop();
    }
}

Member Function Documentation

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.

{
    // Get new samRecord.
    SamRecord* returnSam = NULL;
    if(!myFreeSamRecords.empty())
    {
        // have free already allocated records, so get one of those.
        returnSam = myFreeSamRecords.front();
        myFreeSamRecords.pop();
    }
    else if((myMaxAllowedRecs == -1) || (myAllocatedRecs < myMaxAllowedRecs))
    {
        // There were no free records, but either there is no max or
        // there is still room to allocate more.
        returnSam = new SamRecord();
        ++myAllocatedRecs;
        if(returnSam == NULL)
        {
            // Failed allocation.
            throw(std::runtime_error("Failed to allocate SamRecord"));
        }
    }
    else
    {
        // There are no more free ones and we have already hit the
        // max number allowed to be allocated, so return NULL.
        // The user will have to release some or update the max.
        returnSam = NULL;
    }
    return(returnSam);
}

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

If record is NULL, nothing is done.

Parameters:
recordpointer 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().

{
    if(record == NULL)
    {
        // Nothing to release, so just return.
        return;
    }

    // Release the samRecord to be reused.
    myFreeSamRecords.push(record);
}
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.

{
    myMaxAllowedRecs = maxNumRecs;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends