From: Christian P. <cp...@us...> - 2005-06-03 09:47:53
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23915/include/pclasses Modified Files: Makefile.am Added Files: Buffer.h Log Message: - Added P::Buffer<> Index: Makefile.am =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Makefile.am,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- Makefile.am 6 May 2005 15:28:23 -0000 1.14 +++ Makefile.am 3 Jun 2005 09:47:42 -0000 1.15 @@ -18,7 +18,7 @@ CircularQueue.h List.h NonCopyable.h Phoenix.h CoreMutex.h Factory.h \ SharingContext.h Time.h Date.h DateTime.h TimeSpan.h Callback.h Signal.h \ CallbackN1.h CallbackN2.h CallbackN3.h CallbackN4.h \ - Trace.h PropertyMap.h Export.h pclasses-config.h + Trace.h PropertyMap.h Export.h Buffer.h pclasses-config.h all: all_callbacks --- NEW FILE: Buffer.h --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * 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 Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef P_Buffer_h #define P_Buffer_h #include <pclasses/Algorithm.h> #include <pclasses/Exception.h> namespace P { template <class Type> class Buffer { public: Buffer(size_t size = 1024, bool autoResize = true) : _autoResize(autoResize), _buffer((Type*)new char[sizeof(Type) * size]), _size(size), _count(0) { } Buffer(const Buffer& b) : _autoResize(b._autoResize), _buffer((Type*)new char[sizeof(Type) * b._size]), _size(b._size), _count(b._count) { copy_construct(_buffer, b._buffer, _count); } ~Buffer() throw() { clear(); delete[] (char*)_buffer; } void swap(Buffer& b) throw() { bool autoResize = b._autoResize; Type* buffer = b._buffer; size_t size = b._size; size_t count = b._count; b._autoResize = _autoResize; b._buffer = _buffer; b._size = _size; b._count = _count; _autoResize = autoResize; _buffer = buffer; _size = size; _count = count; } void clear() throw() { destruct(_buffer, _count); _count = 0; } inline size_t size() const throw() { return _count; } void resize(size_t sz) throw(OverrunError) { if(_count > sz) throw OverrunError("Buffer overrun", P_SOURCEINFO); Type* newBuffer = (Type*)new char[sizeof(Type) * sz]; copy_construct(newBuffer, _buffer, _count); destruct(_buffer, _count); delete[] (char*)_buffer; _buffer = newBuffer; _size = sz; } inline size_t capacity() const throw() { return _size; } inline bool empty() const throw() { return _count == 0; } inline Type* data() throw() { return _buffer; } void pop(size_t count) throw(UnderrunError) { if(_count >= count) { destruct(_buffer, count); copy(_buffer, _buffer + count, _count - count); _count -= count; return; } throw UnderrunError("Buffer underrun", P_SOURCEINFO); } void push(const Type* buff, size_t count) throw(OverrunError) { if(_size < _count + count) { if(!_autoResize) throw OverrunError("Buffer overrun", P_SOURCEINFO); resize(_count + count); } copy_construct(_buffer + _count, buff, count); _count += count; } inline Buffer& operator=(const Buffer& b) { Buffer(b).swap(*this); return *this; } private: bool _autoResize; Type* _buffer; size_t _size; size_t _count; }; template <class Type> inline void swap(Buffer<Type>& a, Buffer<Type>& b) throw() { a.swap(b); } } // !namespace P #endif |