File: buffer.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (400 lines) | stat: -rw-r--r-- 11,833 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/////////////////////////////////////////////////////////////////////////////
// Name:        buffer.h
// Purpose:     interface of wxMemoryBuffer
// Author:      wxWidgets team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////


/**
    wxScopedCharTypeBuffer<T> is a template class for storing characters.

    Data are stored in reference-counted buffer. In other words, making a copy
    of wxScopedCharTypeBuffer<T> will @em not make another copy of the stored
    string data, it will still point to the same location in memory.

    wxScopedCharTypeBuffer<T> supports two storage modes: owned and non-owned.
    "Owned" data buffer (created with CreateOwned() or wxCharTypeBuffer<T>
    derived class) owns the data and frees them when the last buffer pointing
    to them is destroyed.

    "Non-owned" buffer (created with CreateNonOwned()), on the other hand,
    references data owned by somebody else -- typical use is by
    wxString::mb_str() or wxString::wc_str(), which may return non-owned buffer
    pointing to wxString's internal store.

    Because of this, the validity of data stored in wxScopedCharTypeBuffer<T>
    is limited by the lifetime of the "parent" object that created the
    buffer (e.g. the wxString on which mb_str() was called).

    If you need to preserve the data for longer, assign it to
    wxCharTypeBuffer<T> instead of wxScopedCharTypeBuffer<T>. On the other
    hand, use wxScopedCharTypeBuffer<T> if the buffer is to be destroyed before
    the "parent" object -- typical use would be creating it on the stack and
    destroying when it goes out of scope (hence the class' name).

    @tparam T
        The type of the characters stored in this class.

    @since 2.9.0

    @nolibrary
    @category{data}
*/
template <typename T>
class wxScopedCharTypeBuffer
{
public:
    /// Stored characters type.
    typedef T CharType;

    /// Default constructor, creates NULL buffer.
    wxScopedCharTypeBuffer();

    /**
        Creates non-owned buffer from string data @a str.

        The buffer's destructor will not destroy @a str. The returned buffer's
        data is valid only as long as @a str is valid.

        @param str String data.
        @param len If specified, length of the string, otherwise the string
                   is considered to be NUL-terminated.
     */
    static const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str, size_t len = wxNO_LEN);

    /**
        Creates owned buffer from @a str and takes ownership of it.

        The buffer's destructor will free @a str when its reference count
        reaches zero (initial count is 1).

        @param str String data.
        @param len If specified, length of the string, otherwise the string
                   is considered to be NUL-terminated.
     */
    static const wxScopedCharTypeBuffer CreateOwned(CharType *str, size_t len = wxNO_LEN);

    /**
        Copy constructor.

        Increases reference count on the data, does @em not make wxStrdup()
        copy of the data.
     */
    wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer& src);

    /// Assignment operator behaves in the same way as the copy constructor.
    wxScopedCharTypeBuffer& operator=(const wxScopedCharTypeBuffer& src);

    /**
        Destructor. Frees stored data if it is in "owned" mode and data's
        reference count reaches zero.
     */
    ~wxScopedCharTypeBuffer();

    /// Resets the buffer to NULL, freeing the data if necessary.
    void reset();

    /// Returns pointer to the stored data.
    CharType *data();

    /// Returns const pointer to the stored data.
    const CharType *data() const;

    /// Returns length of the string stored.
    size_t length() const;

    /// Implicit conversion to C string.
    operator const CharType *() const;

    /// Random access to the stored C string.
    CharType operator[](size_t n) const;
};

/// Scoped char buffer.
typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;

/// Scoped wchar_t buffer.
typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;

/**
    wxCharTypeBuffer<T> is a template class for storing characters.

    The difference from wxScopedCharTypeBuffer<T> is that this class
    doesn't have non-owned mode and the data stored in it are valid for
    as long as the buffer instance exists. Other than that, this class'
    behaviour is the same as wxScopedCharTypeBuffer<T>'s -- in particular,
    the data are reference-counted and copying the buffer is cheap.

    wxScopedCharTypeBuffer<T> buffers can be converted into wxCharTypeBuffer<T>.

    @tparam T
        The type of the characters stored in this class.

    @since 2.9.0

    @nolibrary
    @category{data}
*/
template <typename T>
class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
{
public:
    /**
        Creates (owned) buffer from @a str and takes ownership of it.

        @param str String data.
        @param len If specified, length of the string, otherwise the string
                   is considered to be NUL-terminated.

        @see wxScopedCharTypeBuffer<T>::CreateOwned()
     */
    wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN);


    /**
        Creates (owned) buffer of size @a len.

        @see wxScopedCharTypeBuffer<T>::CreateOwned()
     */
    wxCharTypeBuffer(size_t len);

    /**
        Copy constructor.

        Increases reference count on the data, does @em not make wxStrdup()
        copy of the data.
     */
    wxCharTypeBuffer(const wxCharTypeBuffer& src);

    /**
        Makes a copy of scoped buffer @a src.

        If @a src is a non-owned buffer, a copy of its data is made using
        wxStrdup(). If @a src is an owned buffer, this constructor behaves
        in the usual way (reference count on buffer data is incremented).
     */
    wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src);

    /**
        Assigns @a str to this buffer and takes ownership of it (i.e.\ the
        buffer becomes "owned").
     */
    wxCharTypeBuffer& operator=(const CharType *str);

    /// Assignment operator behaves in the same way as the copy constructor.
    wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src);

    /**
        Assigns a scoped buffer to this buffer.

        If @a src is a non-owned buffer, a copy of its data is made using
        wxStrdup(). If @a src is an owned buffer, the assignment behaves
        in the usual way (reference count on buffer data is incremented).
     */
    wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src);

    /**
        Extends the buffer to have size @a len.

        Can only be called on buffers that don't share data with another
        buffer (i.e. reference count of the data is 1).

        @see shrink()
     */
    bool extend(size_t len);

    /**
        Shrinks the buffer to have size @a len and NUL-terminates the string
        at this length.

        Can only be called on buffers that don't share data with another
        buffer (i.e. reference count of the data is 1).

        @param len Length to shrink to. Must not be larger than current length.

        @note The string is not reallocated to take less memory.

        @since 2.9.0

        @see extend()
     */
    bool shrink(size_t len);
};

/**
    This is a specialization of wxCharTypeBuffer<T> for @c char type.

    @nolibrary
    @category{data}
*/
class wxCharBuffer : public wxCharTypeBuffer<char>
{
public:
    typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
    typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;

    wxCharBuffer(const wxCharTypeBufferBase& buf);
    wxCharBuffer(const wxScopedCharTypeBufferBase& buf);
    wxCharBuffer(const CharType *str = NULL);
    wxCharBuffer(size_t len);
    wxCharBuffer(const wxCStrData& cstr);
};

/**
    This is a specialization of wxCharTypeBuffer<T> for @c wchar_t type.

    @nolibrary
    @category{data}
*/
class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
{
public:
    typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
    typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;

    wxWCharBuffer(const wxCharTypeBufferBase& buf);
    wxWCharBuffer(const wxScopedCharTypeBufferBase& buf);
    wxWCharBuffer(const CharType *str = NULL);
    wxWCharBuffer(size_t len);
    wxWCharBuffer(const wxCStrData& cstr);
};

/**
    @class wxMemoryBuffer

    A @b wxMemoryBuffer is a useful data structure for storing arbitrary sized
    blocks of memory. wxMemoryBuffer guarantees deletion of the memory block when
    the object is destroyed.

    @library{wxbase}
    @category{data}
*/
class wxMemoryBuffer
{
public:
    /**
        Copy constructor, refcounting is used for performance, but wxMemoryBuffer
        is not a copy-on-write structure so changes made to one buffer effect all
        copies made from it.

        @see @ref overview_refcount
    */
    wxMemoryBuffer(const wxMemoryBuffer& src);

    /**
        Create a new buffer.

        @param size
            size of the new buffer, 1KiB by default.
    */
    wxMemoryBuffer(size_t size = 1024);

    /**
        Append a single byte to the buffer.

        @param data
            New byte to append to the buffer.
    */
    void AppendByte(char data);

    /**
        Single call to append a data block to the buffer.

        @param data
            Pointer to block to append to the buffer.
        @param len
            Length of data to append.
    */
    void AppendData(const void *data, size_t len);

    /**
        Clear the buffer contents.

        The buffer won't contain any data after this method is called.

        @see IsEmpty()

        @since 2.9.4
     */
    void Clear();

    /**
        Ensure that the buffer is big enough and return a pointer to the start
        of the empty space in the buffer. This pointer can be used to directly
        write data into the buffer, this new data will be appended to the
        existing data.

        @param sizeNeeded
            Amount of extra space required in the buffer for
            the append operation
    */
    void* GetAppendBuf(size_t sizeNeeded);

    /**
        Returns the size of the buffer.
    */
    size_t GetBufSize() const;

    /**
        Return a pointer to the data in the buffer.
    */
    void* GetData() const;

    /**
        Returns the length of the valid data in the buffer.
    */
    size_t GetDataLen() const;

    /**
        Ensure the buffer is big enough and return a pointer to the
        buffer which can be used to directly write into the buffer
        up to @a sizeNeeded bytes.
    */
    void* GetWriteBuf(size_t sizeNeeded);

    /**
        Returns true if the buffer contains no data.

        @see Clear()

        @since 2.9.4
     */
    bool IsEmpty() const;

    /**
        Ensures the buffer has at least @a size bytes available.
    */
    void SetBufSize(size_t size);

    /**
        Sets the length of the data stored in the buffer.
        Mainly useful for truncating existing data.

        @param size
            New length of the valid data in the buffer. This is
            distinct from the allocated size
    */
    void SetDataLen(size_t size);

    /**
        Update the length after completing a direct append, which
        you must have used GetAppendBuf() to initialise.

        @param sizeUsed
            This is the amount of new data that has been
            appended.
    */
    void UngetAppendBuf(size_t sizeUsed);

    /**
        Update the buffer after completing a direct write, which
        you must have used GetWriteBuf() to initialise.

        @param sizeUsed
            The amount of data written in to buffer
            by the direct write
    */
    void UngetWriteBuf(size_t sizeUsed);
};