Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

DynamicBuffer.cxx

00001 /*
00002  * Copyright (C) 1991-9 Eric M. Hopper <hopper@omnifarious.mn.org>
00003  * 
00004  *     This program is free software; you can redistribute it and/or modify it
00005  *     under the terms of the GNU Lesser General Public License as published
00006  *     by the Free Software Foundation; either version 2 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, but
00010  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *     Lesser General Public License for more details.
00013  * 
00014  *     You should have received a copy of the GNU Lesser General Public
00015  *     License along with this program; if not, write to the Free Software
00016  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 /* $Header: /home/hopper/src/cvs/C++/StrMod/DynamicBuffer.cxx,v 1.6 2002/08/29 00:58:04 hopper Exp $ */
00020 
00021 // For a log, see ./ChangeLog
00022 
00023 #ifdef __GNUG__
00024 #  pragma implementation "DynamicBuffer.h"
00025 #endif
00026 
00027 #include "StrMod/DynamicBuffer.h"
00028 #include <cstdlib>
00029 #include <cstring>  // memcpy
00030 #include <new>
00031 #include <cassert>
00032 
00033 namespace strmod {
00034 namespace strmod {
00035 
00036 const STR_ClassIdent DynamicBuffer::identifier(39UL);
00037 
00038 DynamicBuffer::DynamicBuffer(unsigned int len) throw(std::bad_alloc)
00039 {
00040    resize(len);
00041 }
00042 
00043 DynamicBuffer::DynamicBuffer(const void *data,
00044                              unsigned int len) throw(std::bad_alloc)
00045 {
00046    resize(len);
00047    memcpy(buf_, data, len);
00048 }
00049 
00050 DynamicBuffer::~DynamicBuffer()
00051 {
00052    if (buf_)
00053    {
00054       assert(buflen_ > 0);
00055       free(buf_);
00056       buf_ = 0;
00057       buflen_ = 0;
00058    }
00059 }
00060 
00061 void DynamicBuffer::resize(unsigned int newsize) throw(std::bad_alloc)
00062 {
00063    #ifndef NDEBUG
00064    if (buf_ != 0)
00065    {
00066       assert((buf_ != 0) && (buflen_ > 0));
00067    }
00068    else
00069    {
00070       assert((buf_ == 0) && (buflen_ == 0));
00071    }
00072    #endif
00073 
00074    if (newsize != buflen_)
00075    {
00076       if (newsize == 0)
00077       {
00078          free(buf_);
00079          buf_ = 0;
00080          buflen_ = newsize;
00081       }
00082       else
00083       {
00084          void *newbuf = realloc(buf_, newsize);
00085          if ((newbuf == NULL) && (newsize != 0))
00086          {
00087             throw std::bad_alloc();
00088          }
00089          else
00090          {
00091             buf_ = static_cast<lcore::U1Byte *>(newbuf);
00092             buflen_ = newsize;
00093          }
00094       }
00095    }
00096 }
00097 
00098 };  // End namespace strmod
00099 };  // End namespace strmod

Generated on Wed Jan 29 00:32:43 2003 for libNet by doxygen1.3-rc1