/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #ifndef ARRAY_HPP #define ARRAY_HPP #include "ArrayPool.hpp" #include #include /** * Template class used for implementing an * array of object retreived from a pool */ template class Array { public: Array(ArrayPool & thePool); /** * Allocate an n objects from pool * These can now be addressed with 0 <= ptr.i < n */ bool seize(Uint32 i); /** * Release all object from array */ void release(); /** * Return current size of array */ Uint32 getSize() const; /** * empty */ inline bool empty() const { return sz == 0;} /** * Update i & p value according to i */ void getPtr(Ptr &, Uint32 i) const; /** * Update p value for ptr according to i value */ void getPtr(Ptr &) const ; /** * Get pointer for i value */ T * getPtr(Uint32 i) const; private: Uint32 base, sz; ArrayPool & thePool; }; template inline Array::Array(ArrayPool & _pool) : thePool(_pool) { sz = 0; base = RNIL; } template inline bool Array::seize(Uint32 n){ if(base == RNIL && n > 0){ base = thePool.seizeN(n); if(base != RNIL){ sz = n; return true; } return false; } ErrorReporter::handleAssert("Array::seize failed", __FILE__, __LINE__); return false; } template inline void Array::release(){ if(base != RNIL){ thePool.releaseN(base, sz); sz = 0; base = RNIL; return; } } template inline Uint32 Array::getSize() const { return sz; } template inline void Array::getPtr(Ptr & p, Uint32 i) const { p.i = i; #ifdef ARRAY_GUARD if(i < sz && base != RNIL){ p.p = thePool.getPtr(i + base); return; } else { ErrorReporter::handleAssert("Array::getPtr failed", __FILE__, __LINE__); } #endif p.p = thePool.getPtr(i + base); } template inline void Array::getPtr(Ptr & ptr) const { #ifdef ARRAY_GUARD if(ptr.i < sz && base != RNIL){ ptr.p = thePool.getPtr(ptr.i + base); return; } else { ErrorReporter::handleAssert("Array::getPtr failed", __FILE__, __LINE__); } #endif ptr.p = thePool.getPtr(ptr.i + base); } template inline T * Array::getPtr(Uint32 i) const { #ifdef ARRAY_GUARD if(i < sz && base != RNIL){ return thePool.getPtr(i + base); } else { ErrorReporter::handleAssert("Array::getPtr failed", __FILE__, __LINE__); } #endif return thePool.getPtr(i + base); } #endif