libsidplayfp  1.1.0
Buffer.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef BUFFER_H
22 #define BUFFER_H
23 
24 #include <assert.h>
25 #include <stdint.h>
26 
27 template <class T> class Buffer_sidtt
28 {
29  public:
30  Buffer_sidtt() : 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() const { return buf; }
54  uint_least32_t len() const { return bufLen; }
55 
56  T* xferPtr()
57  {
58  T* tmpBuf = buf;
59  buf = 0;
60  return tmpBuf;
61  }
62 
63  uint_least32_t xferLen()
64  {
65  const uint_least32_t tmpBufLen = bufLen;
66  bufLen = 0;
67  return tmpBufLen;
68  }
69 
70  T& operator[](uint_least32_t index)
71  {
72  return (index < bufLen) ? buf[index] : dummy;
73  }
74 
75  bool isEmpty() const { return (buf==0); }
76 
77  void erase()
78  {
79  if (buf!=0 && bufLen!=0)
80  {
81 #ifndef SID_HAVE_BAD_COMPILER
82  delete[] buf;
83 #else
84  delete[] (void *) buf;
85 #endif
86  }
87  kill();
88  }
89 
90  ~Buffer_sidtt()
91  {
92  erase();
93  }
94 
95  private:
96  T* buf;
97  uint_least32_t bufLen;
98  T dummy;
99 
100  void kill()
101  {
102  buf = 0;
103  bufLen = 0;
104  }
105 
106  private: // prevent copying
107  // SAW - Need function body so code can be fully instatiated
108  // for exporting from dll. Use asserts in debug mode as these
109  // should not be used.
110  Buffer_sidtt(const Buffer_sidtt&) : dummy (0) { assert(0); }
111  Buffer_sidtt& operator=(Buffer_sidtt& b)
112  {
113  assert(0);
114  return b;
115  }
116 };
117 
118 #endif /* BUFFER_H */