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