libsidplayfp  1.0.3
Buffer.h
00001 /*
00002  * This file is part of libsidplayfp, a SID player engine.
00003  *
00004  * Copyright (C) Michael Schwendt <mschwendt@yahoo.com>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 #ifndef BUFFER_H
00022 #define BUFFER_H
00023 
00024 #include <assert.h>
00025 #include <stdint.h>
00026 
00027 template <class T> class Buffer_sidtt
00028 {
00029  public:
00030         Buffer_sidtt() : dummy(0)
00031         {
00032                 kill();
00033         }
00034 
00035         Buffer_sidtt(T* inBuf, uint_least32_t inLen) : dummy(0)
00036         {
00037                 kill();
00038                 if (inBuf!=0 && inLen!=0)
00039                 {
00040                         buf = inBuf;
00041                         bufLen = inLen;
00042                 }
00043         }
00044 
00045         bool assign(T* newBuf, uint_least32_t newLen)
00046         {
00047                 erase();
00048                 buf = newBuf;
00049                 bufLen = newLen;
00050                 return (buf!=0);
00051         }
00052 
00053         T* get() const  { return buf; }
00054         uint_least32_t len() const  { return bufLen; }
00055 
00056         T* xferPtr()
00057         {
00058                 T* tmpBuf = buf;
00059                 buf = 0;
00060                 return tmpBuf;
00061         }
00062 
00063         uint_least32_t xferLen()
00064         {
00065                 const uint_least32_t tmpBufLen = bufLen;
00066                 bufLen = 0;
00067                 return tmpBufLen;
00068         }
00069 
00070         T& operator[](uint_least32_t index)
00071         {
00072                 return (index < bufLen) ? buf[index] : dummy;
00073         }
00074 
00075         bool isEmpty() const  { return (buf==0); }
00076 
00077         void erase()
00078         {
00079                 if (buf!=0 && bufLen!=0)
00080                 {
00081 #ifndef SID_HAVE_BAD_COMPILER
00082                         delete[] buf;
00083 #else
00084                         delete[] (void *) buf;
00085 #endif
00086                 }
00087                 kill();
00088         }
00089 
00090         ~Buffer_sidtt()
00091         {
00092                 erase();
00093         }
00094 
00095  private:
00096         T* buf;
00097         uint_least32_t bufLen;
00098         T dummy;
00099 
00100         void kill()
00101         {
00102                 buf = 0;
00103                 bufLen = 0;
00104         }
00105 
00106  private:       // prevent copying
00107         // SAW - Need function body so code can be fully instatiated
00108         // for exporting from dll.  Use asserts in debug mode as these
00109         // should not be used.
00110         Buffer_sidtt(const Buffer_sidtt&) : dummy (0) { assert(0); }
00111         Buffer_sidtt& operator=(Buffer_sidtt& b)
00112         {
00113                 assert(0);
00114                 return b;
00115         }
00116 };
00117 
00118 #endif  /* BUFFER_H */