Fix several warnings that appear in gcc 4.3.2.
[wvstreams.git] / include / wvbuf.h
blob24a987f6f39177239a893d0ecffa5eb41e1e54b8
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Specializations of the generic buffering API and a few new buffers.
6 */
7 #ifndef __WVBUFFER_H
8 #define __WVBUFFER_H
10 #include "wvstring.h"
11 #include "wvbufbase.h"
13 /***** Specialization for 'unsigned char' buffers *****/
15 /**
16 * Specialization of WvBufBase for unsigned char type
17 * buffers intended for use with raw memory buffers.
18 * Refines the interface to add support for untyped pointers.
19 * Adds some useful string operations.
21 template <>
22 class WvBufBase<unsigned char> :
23 public WvBufBaseCommonImpl<unsigned char>
25 public:
26 explicit WvBufBase(WvBufStore *store) :
27 WvBufBaseCommonImpl<unsigned char>(store) { }
29 /**
30 * Copies a WvString into the buffer, excluding the null-terminator.
31 * "str" is the string
33 void putstr(WvStringParm str);
34 void putstr(WVSTRING_FORMAT_DECL)
35 { putstr(WvString(WVSTRING_FORMAT_CALL)); }
37 /**
38 * Returns the entire buffer as a null-terminated WvString.
40 * If the buffer contains null characters, they will seem to
41 * prematurely terminate the string.
43 * After this operation, ungettable() >= length of the string.
45 * Returns: the buffer contents as a string
47 WvString getstr();
49 /**
50 * Returns the first len characters in the buffer.
52 * This is equivalent to doing a get(len), but returns it as a WvString
53 * instead of as an unsigned char *.
55 WvString getstr(size_t len);
57 /*** Get/put characters as integer values ***/
59 /**
60 * Returns a single character from the buffer as an int.
62 * The same constraints apply as for get(1).
64 * Returns: the character
66 int getch()
67 { return int(get()); }
69 /**
70 * Puts a single character into the buffer as an int.
72 * The same constraints apply as for alloc(1).
74 * "ch" is the character
76 void putch(int ch)
77 { put((unsigned char)ch); }
79 /**
80 * Peeks a single character from the buffer as an int.
82 * The same constraints apply as for peek(offset, 1).
84 * "offset" is the offset
85 * Returns: the character
87 int peekch(int offset = 0)
88 { return int(peek(offset)); }
90 /**
91 * Returns the number of characters that would have to be read
92 * to find the first instance of the character.
93 * "ch" is the character
94 * Returns: the number of bytes, or zero if the character is not
95 * in the buffer
97 size_t strchr(int ch);
99 /**
100 * Returns the number of leading buffer elements that match
101 * any of those in the list.
102 * "bytelist" is the list bytes to search for
103 * "numbytes" is the number of bytes in the list
104 * Returns: the number of leading buffer elements that match
106 size_t match(const void *bytelist, size_t numbytes)
107 { return _match(bytelist, numbytes, false); }
110 * Returns the number of leading buffer elements that match
111 * any of those in the list.
112 * "chlist" is a string of characters to search for
113 * Returns: the number of leading buffer elements that match
115 size_t match(const char *chlist)
116 { return match(chlist, strlen(chlist)); }
119 * Returns the number of leading buffer elements that do not
120 * match any of those in the list.
121 * "bytelist" is the list bytes to search for
122 * "numbytes" is the number of bytes in the list
123 * Returns: the number of leading buffer elements that don't match
125 size_t notmatch(const void *bytelist, size_t numbytes)
126 { return _match(bytelist, numbytes, true); }
129 * Returns the number of leading buffer elements that do not
130 * match any of those in the list.
131 * "chlist" is a string of characters to search for
132 * Returns: the number of leading buffer elements that don't match
134 size_t notmatch(const char *chlist)
135 { return notmatch(chlist, strlen(chlist)); }
137 /*** Overload put() and move() to accept void pointers ***/
139 void put(unsigned char value)
140 { WvBufBaseCommonImpl<unsigned char>::put(value); }
141 void put(const void *data, size_t count)
142 { WvBufBaseCommonImpl<unsigned char>::put(
143 (const unsigned char*)data, count); }
144 void move(void *data, size_t count)
145 { WvBufBaseCommonImpl<unsigned char>::move(
146 (unsigned char*)data, count); }
147 void poke(void *data, int offset, size_t count)
148 { WvBufBaseCommonImpl<unsigned char>::poke(
149 (unsigned char*)data, offset, count); }
151 private:
152 // moved here to avoid ambiguities between the match variants
153 size_t _match(const void *bytelist, size_t numbytes, bool reverse);
158 /***** Declarations for some commonly used memory buffers *****/
161 * The in place raw memory buffer type.
162 * Refines the interface to add support for untyped pointers.
164 class WvInPlaceBuf : public WvInPlaceBufBase<unsigned char>
166 public:
167 WvInPlaceBuf(void *_data, size_t _avail, size_t _size,
168 bool _autofree = false) :
169 WvInPlaceBufBase<unsigned char>((unsigned char*)_data,
170 _avail, _size, _autofree) { }
171 explicit WvInPlaceBuf(size_t _size) :
172 WvInPlaceBufBase<unsigned char>(_size) { }
173 WvInPlaceBuf() :
174 WvInPlaceBufBase<unsigned char>() { }
175 void reset(void *_data, size_t _avail, size_t _size,
176 bool _autofree = false)
178 WvInPlaceBufBase<unsigned char>::reset(
179 (unsigned char*)_data, _avail, _size, _autofree);
184 * The const in place raw memory buffer type.
185 * Refines the interface to add support for untyped pointers.
187 class WvConstInPlaceBuf : public WvConstInPlaceBufBase<unsigned char>
189 public:
190 WvConstInPlaceBuf(const void *_data, size_t _avail) :
191 WvConstInPlaceBufBase<unsigned char>(
192 (const unsigned char*)_data, _avail) { }
193 WvConstInPlaceBuf() :
194 WvConstInPlaceBufBase<unsigned char>() { }
195 void reset(const void *_data, size_t _avail)
197 WvConstInPlaceBufBase<unsigned char>::reset(
198 (const unsigned char*)_data, _avail);
203 * The circular in place raw memory buffer type.
204 * Refines the interface to add support for untyped pointers.
206 class WvCircularBuf : public WvCircularBufBase<unsigned char>
208 public:
209 WvCircularBuf(void *_data, size_t _avail, size_t _size,
210 bool _autofree = false) :
211 WvCircularBufBase<unsigned char>((unsigned char*)_data,
212 _avail, _size, _autofree) { }
213 explicit WvCircularBuf(size_t _size) :
214 WvCircularBufBase<unsigned char>(_size) { }
215 WvCircularBuf() :
216 WvCircularBufBase<unsigned char>() { }
217 void reset(void *_data, size_t _avail, size_t _size,
218 bool _autofree = false)
220 WvCircularBufBase<unsigned char>::reset(
221 (unsigned char*)_data, _avail, _size, _autofree);
225 /** The base raw memory buffer type. */
226 typedef WvBufBase<unsigned char> WvBuf;
228 /** The dynamically resizing raw memory buffer type. */
229 typedef WvDynBufBase<unsigned char> WvDynBuf;
231 /** The empty raw memory buffer type. */
232 typedef WvNullBufBase<unsigned char> WvNullBuf;
234 /** The raw memory buffer cursor type. */
235 typedef WvBufCursorBase<unsigned char> WvBufCursor;
237 /** The raw memory buffer view type. */
238 typedef WvBufViewBase<unsigned char> WvBufView;
240 /** A raw memory read-only buffer backed by a constant WvString */
241 class WvConstStringBuffer : public WvConstInPlaceBuf
243 WvString xstr;
245 public:
247 * Creates a new buffer backed by a constant string.
249 * "_str" is the string
251 explicit WvConstStringBuffer(WvStringParm _str);
253 /** Creates a new empty buffer backed by a null string. */
254 WvConstStringBuffer();
257 * Resets the buffer contents to a new string.
259 * "_str" is the new string
261 void reset(WvStringParm _str);
264 * Returns the string that backs the array
266 * Returns: the string
268 WvString str()
269 { return xstr; }
272 #endif // __WVBUFFER_H