]> git.saurik.com Git - wxWidgets.git/blob - include/wx/buffer.h
Fix UTF-8 string storage in constant: use unsigned char.
[wxWidgets.git] / include / wx / buffer.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/buffer.h
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>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_BUFFER_H
13 #define _WX_BUFFER_H
14
15 #include "wx/chartype.h"
16 #include "wx/wxcrtbase.h"
17
18 #ifndef __WXPALMOS5__
19 #include <stdlib.h> // malloc() and free()
20 #endif // ! __WXPALMOS5__
21
22 class WXDLLIMPEXP_FWD_BASE wxCStrData;
23
24 // ----------------------------------------------------------------------------
25 // Special classes for (wide) character strings: they use malloc/free instead
26 // of new/delete
27 // ----------------------------------------------------------------------------
28
29 // helpers used by wxCharTypeBuffer
30 namespace wxPrivate
31 {
32
33 struct UntypedBufferData
34 {
35 enum Kind
36 {
37 Owned,
38 NonOwned
39 };
40
41 UntypedBufferData(void *str, size_t len, Kind kind = Owned)
42 : m_str(str), m_length(len), m_ref(1), m_owned(kind == Owned) {}
43
44 ~UntypedBufferData()
45 {
46 if ( m_owned )
47 free(m_str);
48 }
49
50 void *m_str;
51 size_t m_length;
52
53 // "short" to have sizeof(Data)=12 on 32bit archs
54 unsigned short m_ref;
55
56 bool m_owned;
57 };
58
59 // NB: this is defined in string.cpp and not the (non-existent) buffer.cpp
60 WXDLLIMPEXP_BASE UntypedBufferData * GetUntypedNullData();
61
62 } // namespace wxPrivate
63
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.
68 template <typename T>
69 class wxScopedCharTypeBuffer
70 {
71 public:
72 typedef T CharType;
73
74 wxScopedCharTypeBuffer()
75 {
76 m_data = GetNullData();
77 }
78
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.
82 static
83 const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str,
84 size_t len = wxNO_LEN)
85 {
86 if ( len == wxNO_LEN )
87 len = wxStrlen(str);
88
89 wxScopedCharTypeBuffer buf;
90 if ( str )
91 buf.m_data = new Data(const_cast<CharType*>(str), len, Data::NonOwned);
92 return buf;
93 }
94
95 // Creates "owned" buffer, i.e. takes over ownership of 'str' and frees it
96 // in dtor (if ref.count reaches 0).
97 static
98 const wxScopedCharTypeBuffer CreateOwned(const CharType *str,
99 size_t len = wxNO_LEN )
100 {
101 if ( len == wxNO_LEN )
102 len = wxStrlen(str);
103
104 wxScopedCharTypeBuffer buf;
105 if ( str )
106 buf.m_data = new Data(StrCopy(str, len), len);
107 return buf;
108 }
109
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()
129 {
130 DecRef();
131 }
132
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
137 {
138 if ( m_data == GetNullData() )
139 return NULL;
140
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") );
143
144 CharType * const p = m_data->Get();
145
146 wxScopedCharTypeBuffer *self = const_cast<wxScopedCharTypeBuffer*>(this);
147 self->m_data->Set(NULL, 0);
148 self->DecRef();
149
150 return p;
151 }
152
153 void reset()
154 {
155 DecRef();
156 }
157
158 CharType *data() { return m_data->Get(); }
159 const CharType *data() const { return m_data->Get(); }
160 operator const CharType *() const { return data(); }
161 CharType operator[](size_t n) const { return data()[n]; }
162
163 size_t length() const { return m_data->m_length; }
164
165 protected:
166 // reference-counted data
167 struct Data : public wxPrivate::UntypedBufferData
168 {
169 Data(CharType *str, size_t len, Kind kind = Owned)
170 : wxPrivate::UntypedBufferData(str, len, kind)
171 {
172 }
173
174 CharType *Get() const { return static_cast<CharType *>(m_str); }
175 void Set(CharType *str, size_t len)
176 {
177 m_str = str;
178 m_length = len;
179 }
180 };
181
182 // placeholder for NULL string, to simplify this code
183 static Data *GetNullData()
184 {
185 return static_cast<Data *>(wxPrivate::GetUntypedNullData());
186 }
187
188 void IncRef()
189 {
190 if ( m_data == GetNullData() ) // exception, not ref-counted
191 return;
192 m_data->m_ref++;
193 }
194
195 void DecRef()
196 {
197 if ( m_data == GetNullData() ) // exception, not ref-counted
198 return;
199 if ( --m_data->m_ref == 0 )
200 delete m_data;
201 m_data = GetNullData();
202 }
203
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
224 this->m_data = new Data
225 (
226 StrCopy(src.data(), src.length()),
227 src.length()
228 );
229 }
230 }
231
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
239 protected:
240 Data *m_data;
241 };
242
243 typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;
244 typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;
245
246
247 // this buffer class always stores data in "owned" (persistent) manner
248 template <typename T>
249 class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
250 {
251 protected:
252 typedef typename wxScopedCharTypeBuffer<T>::Data Data;
253
254 public:
255 typedef T CharType;
256
257 wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN)
258 {
259 if ( str )
260 {
261 if ( len == wxNO_LEN )
262 len = wxStrlen(str);
263 this->m_data = new Data(StrCopy(str, len), len);
264 }
265 else
266 {
267 this->m_data = this->GetNullData();
268 }
269 }
270
271 wxCharTypeBuffer(size_t len)
272 {
273 this->m_data =
274 new Data((CharType *)malloc((len + 1)*sizeof(CharType)), len);
275 this->m_data->Get()[len] = (CharType)0;
276 }
277
278 wxCharTypeBuffer(const wxCharTypeBuffer& src)
279 : wxScopedCharTypeBuffer<T>(src) {}
280
281 wxCharTypeBuffer& operator=(const CharType *str)
282 {
283 this->DecRef();
284
285 if ( str )
286 this->m_data = new Data(wxStrdup(str), wxStrlen(str));
287 return *this;
288 }
289
290 wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src)
291 {
292 wxScopedCharTypeBuffer<T>::operator=(src);
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 {
319 this->m_data = new Data(str, len);
320 }
321 else
322 {
323 this->m_data->Set(str, len);
324 this->m_data->m_owned = true;
325 }
326
327 return true;
328 }
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 }
340 };
341
342 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer<char> )
343 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
344
345 class wxCharBuffer : public wxCharTypeBuffer<char>
346 {
347 public:
348 typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
349 typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;
350
351 wxCharBuffer(const wxCharTypeBufferBase& buf)
352 : wxCharTypeBufferBase(buf) {}
353 wxCharBuffer(const wxScopedCharTypeBufferBase& buf)
354 : wxCharTypeBufferBase(buf) {}
355
356 wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
357 wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
358
359 wxCharBuffer(const wxCStrData& cstr);
360 };
361
362 #if wxUSE_WCHAR_T
363 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer<wchar_t> )
364 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<wchar_t> )
365
366 class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
367 {
368 public:
369 typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
370 typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;
371
372 wxWCharBuffer(const wxCharTypeBufferBase& buf)
373 : wxCharTypeBufferBase(buf) {}
374 wxWCharBuffer(const wxScopedCharTypeBufferBase& buf)
375 : wxCharTypeBufferBase(buf) {}
376
377 wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
378 wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
379
380 wxWCharBuffer(const wxCStrData& cstr);
381 };
382 #endif // wxUSE_WCHAR_T
383
384 // wxCharTypeBuffer<T> implicitly convertible to T*
385 template <typename T>
386 class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
387 {
388 public:
389 typedef typename wxScopedCharTypeBuffer<T>::CharType CharType;
390
391 wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src)
392 : wxCharTypeBuffer<T>(src) {}
393 // FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
394 // always return a buffer
395 // + we should derive this class from wxScopedCharTypeBuffer
396 // then
397 wxWritableCharTypeBuffer(const CharType *str = NULL)
398 : wxCharTypeBuffer<T>(str) {}
399
400 operator CharType*() { return this->data(); }
401 };
402
403 typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer;
404 typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
405
406
407 #if wxUSE_UNICODE
408 #define wxWxCharBuffer wxWCharBuffer
409
410 #define wxMB2WXbuf wxWCharBuffer
411 #define wxWX2MBbuf wxCharBuffer
412 #if wxUSE_UNICODE_WCHAR
413 #define wxWC2WXbuf wxChar*
414 #define wxWX2WCbuf wxChar*
415 #elif wxUSE_UNICODE_UTF8
416 #define wxWC2WXbuf wxWCharBuffer
417 #define wxWX2WCbuf wxWCharBuffer
418 #endif
419 #else // ANSI
420 #define wxWxCharBuffer wxCharBuffer
421
422 #define wxMB2WXbuf wxChar*
423 #define wxWX2MBbuf wxChar*
424 #define wxWC2WXbuf wxCharBuffer
425 #define wxWX2WCbuf wxWCharBuffer
426 #endif // Unicode/ANSI
427
428 // ----------------------------------------------------------------------------
429 // A class for holding growable data buffers (not necessarily strings)
430 // ----------------------------------------------------------------------------
431
432 // This class manages the actual data buffer pointer and is ref-counted.
433 class wxMemoryBufferData
434 {
435 public:
436 // the initial size and also the size added by ResizeIfNeeded()
437 enum { DefBufSize = 1024 };
438
439 friend class wxMemoryBuffer;
440
441 // everyting is private as it can only be used by wxMemoryBuffer
442 private:
443 wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
444 : m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
445 {
446 }
447 ~wxMemoryBufferData() { free(m_data); }
448
449
450 void ResizeIfNeeded(size_t newSize)
451 {
452 if (newSize > m_size)
453 {
454 void *dataOld = m_data;
455 m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
456 if ( !m_data )
457 {
458 free(dataOld);
459 }
460
461 m_size = newSize + wxMemoryBufferData::DefBufSize;
462 }
463 }
464
465 void IncRef() { m_ref += 1; }
466 void DecRef()
467 {
468 m_ref -= 1;
469 if (m_ref == 0) // are there no more references?
470 delete this;
471 }
472
473
474 // the buffer containing the data
475 void *m_data;
476
477 // the size of the buffer
478 size_t m_size;
479
480 // the amount of data currently in the buffer
481 size_t m_len;
482
483 // the reference count
484 size_t m_ref;
485
486 wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData);
487 };
488
489
490 class wxMemoryBuffer
491 {
492 public:
493 // ctor and dtor
494 wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
495 {
496 m_bufdata = new wxMemoryBufferData(size);
497 m_bufdata->IncRef();
498 }
499
500 ~wxMemoryBuffer() { m_bufdata->DecRef(); }
501
502
503 // copy and assignment
504 wxMemoryBuffer(const wxMemoryBuffer& src)
505 : m_bufdata(src.m_bufdata)
506 {
507 m_bufdata->IncRef();
508 }
509
510 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
511 {
512 if (&src != this)
513 {
514 m_bufdata->DecRef();
515 m_bufdata = src.m_bufdata;
516 m_bufdata->IncRef();
517 }
518 return *this;
519 }
520
521
522 // Accessors
523 void *GetData() const { return m_bufdata->m_data; }
524 size_t GetBufSize() const { return m_bufdata->m_size; }
525 size_t GetDataLen() const { return m_bufdata->m_len; }
526
527 void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
528 void SetDataLen(size_t len)
529 {
530 wxASSERT(len <= m_bufdata->m_size);
531 m_bufdata->m_len = len;
532 }
533
534 // Ensure the buffer is big enough and return a pointer to it
535 void *GetWriteBuf(size_t sizeNeeded)
536 {
537 m_bufdata->ResizeIfNeeded(sizeNeeded);
538 return m_bufdata->m_data;
539 }
540
541 // Update the length after the write
542 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
543
544 // Like the above, but appends to the buffer
545 void *GetAppendBuf(size_t sizeNeeded)
546 {
547 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
548 return (char*)m_bufdata->m_data + m_bufdata->m_len;
549 }
550
551 // Update the length after the append
552 void UngetAppendBuf(size_t sizeUsed)
553 {
554 SetDataLen(m_bufdata->m_len + sizeUsed);
555 }
556
557 // Other ways to append to the buffer
558 void AppendByte(char data)
559 {
560 wxCHECK_RET( m_bufdata->m_data, wxT("invalid wxMemoryBuffer") );
561
562 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
563 *(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
564 m_bufdata->m_len += 1;
565 }
566
567 void AppendData(const void *data, size_t len)
568 {
569 memcpy(GetAppendBuf(len), data, len);
570 UngetAppendBuf(len);
571 }
572
573 operator const char *() const { return (const char*)GetData(); }
574
575 private:
576 wxMemoryBufferData* m_bufdata;
577 };
578
579 // ----------------------------------------------------------------------------
580 // template class for any kind of data
581 // ----------------------------------------------------------------------------
582
583 // TODO
584
585 #endif // _WX_BUFFER_H