libStatGen Software  1
SamRecordPool.h
00001 /*
00002  *  Copyright (C) 2011  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __SAM_RECORD_POOL_H__
00019 #define __SAM_RECORD_POOL_H__
00020 
00021 #include <queue>
00022 #include "SamRecord.h"
00023 
00024 
00025 class SamRecordPool
00026 {
00027 public:
00028     /// Constructor that sets there to be no max number of allocated records.
00029     SamRecordPool();
00030 
00031     /// Constructor that sets the maximum number of allocated records
00032     /// \param maxNumRecs maximum number of allocated records (-1 means no max)
00033     SamRecordPool(int maxNumRecs);
00034 
00035     /// Destructor.  Any records that were allocated without calling "releaseRecord"
00036     /// will not get cleaned up and the user will need to delete them.
00037     ~SamRecordPool();
00038 
00039     /// Get a SamRecord.  If records are already allocated and free use those, if not
00040     /// and there are still more that are allowed to be allocated, allocate a new one.
00041     /// If no more records are allowed to be allocated, NULL is returned.
00042     /// NOTE: The user should call releaseRecord when done using the record.
00043     /// If the user deletes the record instead, it still counts as allocated when
00044     /// comparing against the maxNumRecs but cannot be reused.
00045     /// \return pointer to a SamRecord available for use, or NULL if no more records
00046     /// are allowed to be allocated.
00047     SamRecord* getRecord();
00048 
00049     /// If record is not NULL, adds it back to the free list.  
00050     /// If record is NULL, nothing is done.
00051     /// \param record pointer to a record that is no longer being used 
00052     /// and is available for reuse.
00053     void releaseRecord(SamRecord* record);
00054 
00055     /// Set the maximum number of records allowed to be allocated.
00056     /// If more than the new value have already been allocated,
00057     /// it does not deallocate any, and will continue to reuse
00058     /// the already allocated records, but it will not allocate
00059     /// any additional records.
00060     void setMaxAllocatedRecs(int maxNumRecs);
00061     
00062 
00063 private:
00064 
00065     std::queue<SamRecord*> myFreeSamRecords;
00066     int myMaxAllowedRecs;
00067     int myAllocatedRecs;
00068 };
00069 
00070 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends