libsidplayfp  0.3.5
Buffer.h
1 /*
2  * /home/ms/files/source/libsidtune/RCS/Buffer.h,v
3  *
4  * Copyright (C) Michael Schwendt <mschwendt@yahoo.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #ifndef BUFFER_H
22 #define BUFFER_H
23 
24 #include <assert.h>
25 #include "sidtypes.h"
26 
27 template <class T> class Buffer_sidtt
28 {
29  public:
30  Buffer_sidtt(void) : dummy(0)
31  {
32  kill();
33  }
34 
35  Buffer_sidtt(T* inBuf, uint_least32_t inLen) : dummy(0)
36  {
37  kill();
38  if (inBuf!=0 && inLen!=0)
39  {
40  buf = inBuf;
41  bufLen = inLen;
42  }
43  }
44 
45  bool assign(T* newBuf, uint_least32_t newLen)
46  {
47  erase();
48  buf = newBuf;
49  bufLen = newLen;
50  return (buf!=0);
51  }
52 
53  T* get(void) const { return buf; }
54  uint_least32_t len(void) const { return bufLen; }
55 
56  T* xferPtr(void)
57  {
58  T* tmpBuf = buf;
59  buf = 0;
60  return tmpBuf;
61  }
62 
63  uint_least32_t xferLen(void)
64  {
65  uint_least32_t tmpBufLen = bufLen;
66  bufLen = 0;
67  return tmpBufLen;
68  }
69 
70  T& operator[](uint_least32_t index)
71  {
72  if (index < bufLen)
73  return buf[index];
74  else
75  return dummy;
76  }
77 
78  bool isEmpty(void) const { return (buf==0); }
79 
80  void erase(void)
81  {
82  if (buf!=0 && bufLen!=0)
83  {
84 #ifndef SID_HAVE_BAD_COMPILER
85  delete[] buf;
86 #else
87  delete[] (void *) buf;
88 #endif
89  }
90  kill();
91  }
92 
93  ~Buffer_sidtt(void)
94  {
95  erase();
96  }
97 
98  private:
99  T* buf;
100  uint_least32_t bufLen;
101  T dummy;
102 
103  void kill(void)
104  {
105  buf = 0;
106  bufLen = 0;
107  }
108 
109  private: // prevent copying
110  // SAW - Need function body so code can be fully instatiated
111  // for exporting from dll. Use asserts in debug mode as these
112  // should not be used.
113  Buffer_sidtt(const Buffer_sidtt&) : dummy (0) { assert(0); }
114  Buffer_sidtt& operator=(Buffer_sidtt& b)
115  {
116  assert(0);
117  return b;
118  }
119 };
120 
121 #endif /* BUFFER_H */