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