]> git.saurik.com Git - wxWidgets.git/blame - include/wx/buffer.h
work around (harmless) warnings in VC7 release build
[wxWidgets.git] / include / wx / buffer.h
CommitLineData
14971e5b 1///////////////////////////////////////////////////////////////////////////////
c12ef8a2 2// Name: wx/buffer.h
14971e5b
VZ
3// Purpose: auto buffer classes: buffers which automatically free memory
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 12.04.99
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
14971e5b
VZ
10///////////////////////////////////////////////////////////////////////////////
11
14971e5b
VZ
12#ifndef _WX_BUFFER_H
13#define _WX_BUFFER_H
14
e713a90b 15#include "wx/chartype.h"
52de37c7 16#include "wx/wxcrtbase.h"
e90c1d2a 17
9b4da627 18#ifndef __WXPALMOS5__
e2473d4c 19#include <stdlib.h> // malloc() and free()
9b4da627 20#endif // ! __WXPALMOS5__
e2473d4c 21
b5dbe15d 22class WXDLLIMPEXP_FWD_BASE wxCStrData;
8db4a5d2 23
14971e5b
VZ
24// ----------------------------------------------------------------------------
25// Special classes for (wide) character strings: they use malloc/free instead
26// of new/delete
27// ----------------------------------------------------------------------------
28
4e79262f
VZ
29// helpers used by wxCharTypeBuffer
30namespace wxPrivate
31{
32
33struct UntypedBufferData
34{
35 enum Kind
36 {
37 Owned,
38 NonOwned
39 };
40
6df09f32
VS
41 UntypedBufferData(void *str, size_t len, Kind kind = Owned)
42 : m_str(str), m_length(len), m_ref(1), m_owned(kind == Owned) {}
4e79262f
VZ
43
44 ~UntypedBufferData()
45 {
46 if ( m_owned )
47 free(m_str);
48 }
49
50 void *m_str;
6df09f32 51 size_t m_length;
4e79262f 52
6df09f32 53 // "short" to have sizeof(Data)=12 on 32bit archs
4e79262f
VZ
54 unsigned short m_ref;
55
56 bool m_owned;
57};
58
59// this has to be defined inside the DLL (and not e.g. as a static variable
60// inside an inline function) as otherwise MSVC gives link errors when the
61// functions are effectively inlined (i.e. in non-debug build)
62//
63// NB: this is defined in string.cpp and not the (non-existent) buffer.cpp
64extern WXDLLIMPEXP_DATA_BASE(UntypedBufferData * const) untypedNullDataPtr;
65
66} // namespace wxPrivate
67
de4983f3
VS
68
69// Reference-counted character buffer for storing string data. The buffer
70// is only valid for as long as the "parent" object that provided the data
71// is valid; see wxCharTypeBuffer<T> for persistent variant.
d18c8d3d 72template <typename T>
de4983f3 73class wxScopedCharTypeBuffer
d18c8d3d
VS
74{
75public:
76 typedef T CharType;
77
de4983f3 78 wxScopedCharTypeBuffer()
d18c8d3d 79 {
de4983f3 80 m_data = GetNullData();
d18c8d3d
VS
81 }
82
de4983f3
VS
83 // Creates "non-owned" buffer, i.e. 'str' is not owned by the buffer
84 // and doesn't get freed by dtor. Used e.g. to point to wxString's internal
85 // storage.
6df09f32
VS
86 static
87 const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str,
88 size_t len = wxNO_LEN)
d18c8d3d 89 {
6df09f32
VS
90 if ( len == wxNO_LEN )
91 len = wxStrlen(str);
92
de4983f3
VS
93 wxScopedCharTypeBuffer buf;
94 if ( str )
6df09f32 95 buf.m_data = new Data(const_cast<CharType*>(str), len, Data::NonOwned);
de4983f3 96 return buf;
d18c8d3d
VS
97 }
98
de4983f3
VS
99 // Creates "owned" buffer, i.e. takes over ownership of 'str' and frees it
100 // in dtor (if ref.count reaches 0).
6df09f32
VS
101 static
102 const wxScopedCharTypeBuffer CreateOwned(const CharType *str,
103 size_t len = wxNO_LEN )
486a5941 104 {
6df09f32
VS
105 if ( len == wxNO_LEN )
106 len = wxStrlen(str);
107
de4983f3 108 wxScopedCharTypeBuffer buf;
5c1de526 109 if ( str )
6df09f32 110 buf.m_data = new Data(StrCopy(str, len), len);
486a5941
VS
111 return buf;
112 }
113
de4983f3
VS
114 wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer& src)
115 {
116 m_data = src.m_data;
117 IncRef();
118 }
119
120 wxScopedCharTypeBuffer& operator=(const wxScopedCharTypeBuffer& src)
121 {
122 if ( &src == this )
123 return *this;
124
125 DecRef();
126 m_data = src.m_data;
127 IncRef();
128
129 return *this;
130 }
131
132 ~wxScopedCharTypeBuffer()
486a5941 133 {
5c1de526 134 DecRef();
486a5941 135 }
d18c8d3d 136
03af437f
VS
137 // NB: this method is only const for backward compatibility. It used to
138 // be needed for auto_ptr-like semantics of the copy ctor, but now
139 // that ref-counting is used, it's not really needed.
140 CharType *release() const
5c1de526 141 {
6b583d40 142 if ( m_data == GetNullData() )
5c1de526 143 return NULL;
d18c8d3d 144
5c1de526
VS
145 wxASSERT_MSG( m_data->m_owned, _T("can't release non-owned buffer") );
146 wxASSERT_MSG( m_data->m_ref == 1, _T("can't release shared buffer") );
14971e5b 147
4e79262f 148 CharType * const p = m_data->Get();
03af437f 149
de4983f3 150 wxScopedCharTypeBuffer *self = const_cast<wxScopedCharTypeBuffer*>(this);
6df09f32 151 self->m_data->Set(NULL, 0);
03af437f
VS
152 self->DecRef();
153
5c1de526 154 return p;
d18c8d3d 155 }
c12ef8a2 156
d18c8d3d
VS
157 void reset()
158 {
5c1de526 159 DecRef();
d18c8d3d 160 }
c12ef8a2 161
4e79262f
VZ
162 CharType *data() { return m_data->Get(); }
163 const CharType *data() const { return m_data->Get(); }
5c1de526
VS
164 operator const CharType *() const { return data(); }
165 CharType operator[](size_t n) const { return data()[n]; }
486a5941 166
6df09f32
VS
167 size_t length() const { return m_data->m_length; }
168
de4983f3 169protected:
5c1de526 170 // reference-counted data
5c69ef61 171 struct Data : public wxPrivate::UntypedBufferData
486a5941 172 {
6df09f32
VS
173 Data(CharType *str, size_t len, Kind kind = Owned)
174 : wxPrivate::UntypedBufferData(str, len, kind)
5c1de526 175 {
5c1de526
VS
176 }
177
4e79262f 178 CharType *Get() const { return static_cast<CharType *>(m_str); }
6df09f32
VS
179 void Set(CharType *str, size_t len)
180 {
181 m_str = str;
182 m_length = len;
183 }
5c1de526
VS
184 };
185
186 // placeholder for NULL string, to simplify this code
6b583d40
VZ
187 static Data *GetNullData()
188 {
4e79262f 189 return static_cast<Data *>(wxPrivate::untypedNullDataPtr);
6b583d40 190 }
5c1de526
VS
191
192 void IncRef()
193 {
6b583d40 194 if ( m_data == GetNullData() ) // exception, not ref-counted
5c1de526
VS
195 return;
196 m_data->m_ref++;
486a5941
VS
197 }
198
5c1de526 199 void DecRef()
486a5941 200 {
6b583d40 201 if ( m_data == GetNullData() ) // exception, not ref-counted
5c1de526
VS
202 return;
203 if ( --m_data->m_ref == 0 )
204 delete m_data;
6b583d40 205 m_data = GetNullData();
486a5941
VS
206 }
207
de4983f3
VS
208 // sets this object to a be copy of 'other'; if 'src' is non-owned,
209 // a deep copy is made and 'this' will contain new instance of the data
210 void MakeOwnedCopyOf(const wxScopedCharTypeBuffer& src)
211 {
212 this->DecRef();
213
214 if ( src.m_data == this->GetNullData() )
215 {
216 this->m_data = this->GetNullData();
217 }
218 else if ( src.m_data->m_owned )
219 {
220 this->m_data = src.m_data;
221 this->IncRef();
222 }
223 else
224 {
225 // if the scoped buffer had non-owned data, we have to make
226 // a copy here, because src.m_data->m_str is valid only for as long
227 // as 'src' exists
6df09f32
VS
228 this->m_data = new Data
229 (
230 StrCopy(src.data(), src.length()),
231 src.length()
232 );
de4983f3
VS
233 }
234 }
235
6df09f32
VS
236 static CharType *StrCopy(const CharType *src, size_t len)
237 {
238 CharType *dst = (CharType*)malloc(sizeof(CharType) * (len + 1));
239 memcpy(dst, src, sizeof(CharType) * (len + 1));
240 return dst;
241 }
242
de4983f3 243protected:
5c1de526 244 Data *m_data;
d18c8d3d
VS
245};
246
de4983f3
VS
247typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;
248typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;
249
250
251// this buffer class always stores data in "owned" (persistent) manner
252template <typename T>
253class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
254{
255protected:
256 typedef typename wxScopedCharTypeBuffer<T>::Data Data;
257
258public:
259 typedef T CharType;
260
6df09f32 261 wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN)
de4983f3
VS
262 {
263 if ( str )
6df09f32
VS
264 {
265 if ( len == wxNO_LEN )
266 len = wxStrlen(str);
267 this->m_data = new Data(StrCopy(str, len), len);
268 }
de4983f3 269 else
6df09f32 270 {
de4983f3 271 this->m_data = this->GetNullData();
6df09f32 272 }
de4983f3
VS
273 }
274
275 wxCharTypeBuffer(size_t len)
276 {
6df09f32
VS
277 this->m_data =
278 new Data((CharType *)malloc((len + 1)*sizeof(CharType)), len);
de4983f3
VS
279 this->m_data->Get()[len] = (CharType)0;
280 }
281
282 wxCharTypeBuffer(const wxCharTypeBuffer& src)
3339414a 283 : wxScopedCharTypeBuffer<T>(src) {}
de4983f3
VS
284
285 wxCharTypeBuffer& operator=(const CharType *str)
286 {
287 this->DecRef();
288
289 if ( str )
6df09f32 290 this->m_data = new Data(wxStrdup(str), wxStrlen(str));
de4983f3
VS
291 return *this;
292 }
293
294 wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src)
295 {
3339414a 296 wxScopedCharTypeBuffer<T>::operator=(src);
de4983f3
VS
297 return *this;
298 }
299
300 wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src)
301 {
302 MakeOwnedCopyOf(src);
303 }
304
305 wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src)
306 {
307 MakeOwnedCopyOf(src);
308 return *this;
309 }
310
311 bool extend(size_t len)
312 {
313 wxASSERT_MSG( this->m_data->m_owned, "cannot extend non-owned buffer" );
314 wxASSERT_MSG( this->m_data->m_ref == 1, "can't extend shared buffer" );
315
316 CharType *str =
317 (CharType *)realloc(this->data(), (len + 1) * sizeof(CharType));
318 if ( !str )
319 return false;
320
321 if ( this->m_data == this->GetNullData() )
322 {
6df09f32 323 this->m_data = new Data(str, len);
de4983f3
VS
324 }
325 else
326 {
6df09f32 327 this->m_data->Set(str, len);
de4983f3
VS
328 this->m_data->m_owned = true;
329 }
330
331 return true;
332 }
a6bb7a28
VS
333
334 void shrink(size_t len)
335 {
336 wxASSERT_MSG( this->m_data->m_owned, "cannot shrink non-owned buffer" );
337 wxASSERT_MSG( this->m_data->m_ref == 1, "can't shrink shared buffer" );
338
339 wxASSERT( len <= this->length() );
340
341 this->m_data->m_length = len;
342 this->data()[len] = 0;
343 }
de4983f3
VS
344};
345
fb5ae4f6 346WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer<char> )
7c77f334
VZ
347WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
348
6b583d40 349class wxCharBuffer : public wxCharTypeBuffer<char>
d18c8d3d
VS
350{
351public:
ccd4deab 352 typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
de4983f3 353 typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;
ccd4deab 354
681e4412
VS
355 wxCharBuffer(const wxCharTypeBufferBase& buf)
356 : wxCharTypeBufferBase(buf) {}
de4983f3
VS
357 wxCharBuffer(const wxScopedCharTypeBufferBase& buf)
358 : wxCharTypeBufferBase(buf) {}
681e4412 359
ccd4deab
VZ
360 wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
361 wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
362
ccd4deab 363 wxCharBuffer(const wxCStrData& cstr);
d18c8d3d
VS
364};
365
366#if wxUSE_WCHAR_T
fb5ae4f6 367WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer<wchar_t> )
7c77f334
VZ
368WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<wchar_t> )
369
6b583d40 370class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
d18c8d3d
VS
371{
372public:
ccd4deab 373 typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
de4983f3 374 typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;
ccd4deab 375
681e4412
VS
376 wxWCharBuffer(const wxCharTypeBufferBase& buf)
377 : wxCharTypeBufferBase(buf) {}
de4983f3
VS
378 wxWCharBuffer(const wxScopedCharTypeBufferBase& buf)
379 : wxCharTypeBufferBase(buf) {}
681e4412 380
ccd4deab
VZ
381 wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
382 wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
383
ccd4deab 384 wxWCharBuffer(const wxCStrData& cstr);
d18c8d3d
VS
385};
386#endif // wxUSE_WCHAR_T
2b5f62a0 387
ef0f1387
VS
388// wxCharTypeBuffer<T> implicitly convertible to T*
389template <typename T>
665e6a87 390class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
ef0f1387
VS
391{
392public:
de4983f3 393 typedef typename wxScopedCharTypeBuffer<T>::CharType CharType;
ef0f1387 394
de4983f3 395 wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src)
ef0f1387
VS
396 : wxCharTypeBuffer<T>(src) {}
397 // FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
398 // always return a buffer
de4983f3
VS
399 // + we should derive this class from wxScopedCharTypeBuffer
400 // then
ef0f1387
VS
401 wxWritableCharTypeBuffer(const CharType *str = NULL)
402 : wxCharTypeBuffer<T>(str) {}
403
404 operator CharType*() { return this->data(); }
405};
406
407typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer;
408typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
409
410
f93d01be 411#if wxUSE_UNICODE
ccd4deab 412 #define wxWxCharBuffer wxWCharBuffer
2f4f6de7 413
e90c1d2a
VZ
414 #define wxMB2WXbuf wxWCharBuffer
415 #define wxWX2MBbuf wxCharBuffer
81727065
VS
416 #if wxUSE_UNICODE_WCHAR
417 #define wxWC2WXbuf wxChar*
418 #define wxWX2WCbuf wxChar*
419 #elif wxUSE_UNICODE_UTF8
420 #define wxWC2WXbuf wxWCharBuffer
421 #define wxWX2WCbuf wxWCharBuffer
422 #endif
e90c1d2a 423#else // ANSI
ccd4deab 424 #define wxWxCharBuffer wxCharBuffer
2f4f6de7 425
e90c1d2a
VZ
426 #define wxMB2WXbuf wxChar*
427 #define wxWX2MBbuf wxChar*
428 #define wxWC2WXbuf wxCharBuffer
429 #define wxWX2WCbuf wxWCharBuffer
430#endif // Unicode/ANSI
f93d01be 431
b9fdb397
RD
432// ----------------------------------------------------------------------------
433// A class for holding growable data buffers (not necessarily strings)
434// ----------------------------------------------------------------------------
435
2b5f62a0
VZ
436// This class manages the actual data buffer pointer and is ref-counted.
437class wxMemoryBufferData
b9fdb397
RD
438{
439public:
2b5f62a0 440 // the initial size and also the size added by ResizeIfNeeded()
ae0ca755 441 enum { DefBufSize = 1024 };
2b5f62a0
VZ
442
443 friend class wxMemoryBuffer;
444
445 // everyting is private as it can only be used by wxMemoryBuffer
446private:
ae0ca755 447 wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
2b5f62a0 448 : m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
b9fdb397 449 {
b9fdb397 450 }
2b5f62a0 451 ~wxMemoryBufferData() { free(m_data); }
b9fdb397 452
b9fdb397 453
2b5f62a0 454 void ResizeIfNeeded(size_t newSize)
b9fdb397 455 {
2b5f62a0
VZ
456 if (newSize > m_size)
457 {
458 void *dataOld = m_data;
ae0ca755 459 m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
2b5f62a0
VZ
460 if ( !m_data )
461 {
462 free(dataOld);
463 }
464
ae0ca755 465 m_size = newSize + wxMemoryBufferData::DefBufSize;
2b5f62a0 466 }
b9fdb397
RD
467 }
468
2b5f62a0
VZ
469 void IncRef() { m_ref += 1; }
470 void DecRef()
b9fdb397 471 {
2b5f62a0
VZ
472 m_ref -= 1;
473 if (m_ref == 0) // are there no more references?
474 delete this;
b9fdb397 475 }
b9fdb397 476
2b5f62a0
VZ
477
478 // the buffer containing the data
479 void *m_data;
480
481 // the size of the buffer
482 size_t m_size;
483
484 // the amount of data currently in the buffer
485 size_t m_len;
486
487 // the reference count
488 size_t m_ref;
22f3361e 489
c0c133e1 490 wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData);
2b5f62a0
VZ
491};
492
493
6b583d40 494class wxMemoryBuffer
2b5f62a0
VZ
495{
496public:
497 // ctor and dtor
ae0ca755 498 wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
b9fdb397 499 {
2b5f62a0
VZ
500 m_bufdata = new wxMemoryBufferData(size);
501 m_bufdata->IncRef();
b9fdb397 502 }
b9fdb397 503
2b5f62a0
VZ
504 ~wxMemoryBuffer() { m_bufdata->DecRef(); }
505
506
507 // copy and assignment
508 wxMemoryBuffer(const wxMemoryBuffer& src)
509 : m_bufdata(src.m_bufdata)
510 {
511 m_bufdata->IncRef();
b9fdb397 512 }
2b5f62a0
VZ
513
514 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
b9fdb397 515 {
162e998c
PC
516 if (&src != this)
517 {
518 m_bufdata->DecRef();
519 m_bufdata = src.m_bufdata;
520 m_bufdata->IncRef();
521 }
2b5f62a0 522 return *this;
b9fdb397
RD
523 }
524
b9fdb397 525
2b5f62a0
VZ
526 // Accessors
527 void *GetData() const { return m_bufdata->m_data; }
528 size_t GetBufSize() const { return m_bufdata->m_size; }
529 size_t GetDataLen() const { return m_bufdata->m_len; }
ef1cae87 530
2b5f62a0
VZ
531 void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
532 void SetDataLen(size_t len)
ef1cae87 533 {
2b5f62a0
VZ
534 wxASSERT(len <= m_bufdata->m_size);
535 m_bufdata->m_len = len;
ef1cae87
RD
536 }
537
2b5f62a0
VZ
538 // Ensure the buffer is big enough and return a pointer to it
539 void *GetWriteBuf(size_t sizeNeeded)
ef1cae87 540 {
2b5f62a0
VZ
541 m_bufdata->ResizeIfNeeded(sizeNeeded);
542 return m_bufdata->m_data;
543 }
c12ef8a2 544
2b5f62a0
VZ
545 // Update the length after the write
546 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
ef1cae87 547
2b5f62a0
VZ
548 // Like the above, but appends to the buffer
549 void *GetAppendBuf(size_t sizeNeeded)
550 {
551 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
552 return (char*)m_bufdata->m_data + m_bufdata->m_len;
553 }
ef1cae87 554
2b5f62a0
VZ
555 // Update the length after the append
556 void UngetAppendBuf(size_t sizeUsed)
557 {
558 SetDataLen(m_bufdata->m_len + sizeUsed);
559 }
ef1cae87 560
2b5f62a0
VZ
561 // Other ways to append to the buffer
562 void AppendByte(char data)
b9fdb397 563 {
2b5f62a0
VZ
564 wxCHECK_RET( m_bufdata->m_data, _T("invalid wxMemoryBuffer") );
565
566 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
567 *(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
568 m_bufdata->m_len += 1;
569 }
570
68bc51a9 571 void AppendData(const void *data, size_t len)
2b5f62a0
VZ
572 {
573 memcpy(GetAppendBuf(len), data, len);
574 UngetAppendBuf(len);
b9fdb397
RD
575 }
576
2b5f62a0
VZ
577 operator const char *() const { return (const char*)GetData(); }
578
b9fdb397 579private:
2b5f62a0 580 wxMemoryBufferData* m_bufdata;
b9fdb397
RD
581};
582
14971e5b
VZ
583// ----------------------------------------------------------------------------
584// template class for any kind of data
585// ----------------------------------------------------------------------------
586
587// TODO
588
589#endif // _WX_BUFFER_H