]> git.saurik.com Git - wxWidgets.git/blob - include/wx/buffer.h
a1547764c71c1f3264ad9782ba97f990fcab09ad
[wxWidgets.git] / include / wx / buffer.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // these classes are for private use only for now, they're not documented
13
14 #ifndef _WX_BUFFER_H
15 #define _WX_BUFFER_H
16
17 #include "wx/wxchar.h"
18 #include <string.h> // strdup
19
20 // ----------------------------------------------------------------------------
21 // Special classes for (wide) character strings: they use malloc/free instead
22 // of new/delete
23 // ----------------------------------------------------------------------------
24
25 class wxCharBuffer
26 {
27 public:
28 wxCharBuffer(const char *str)
29 {
30 wxASSERT_MSG( str, _T("NULL string in wxCharBuffer") );
31
32 m_str = str ? strdup(str) : (char *)NULL;
33 }
34 wxCharBuffer(size_t len)
35 {
36 m_str = (char *)malloc(len+1);
37 m_str[len] = '\0';
38 }
39 // no need to check for NULL, free() does it
40 ~wxCharBuffer() { free(m_str); }
41
42 wxCharBuffer(const wxCharBuffer& src)
43 {
44 m_str = src.m_str;
45 // no reference count yet...
46 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
47 }
48 wxCharBuffer& operator=(const wxCharBuffer& src)
49 {
50 m_str = src.m_str;
51 // no reference count yet...
52 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
53 return *this;
54 }
55
56 operator const char *() const { return m_str; }
57
58 private:
59 char *m_str;
60 };
61
62 class wxWCharBuffer
63 {
64 public:
65 wxWCharBuffer(const wchar_t *wcs)
66 {
67 wxASSERT_MSG( wcs, _T("NULL string in wxWCharBuffer") );
68
69 if (wcs) {
70 size_t siz = (wcslen(wcs)+1)*sizeof(wchar_t);
71 m_wcs = (wchar_t *)malloc(siz);
72 memcpy(m_wcs, wcs, siz);
73 }
74 else m_wcs = (wchar_t *)NULL;
75 }
76 wxWCharBuffer(size_t len)
77 {
78 m_wcs = (wchar_t *)malloc((len+1)*sizeof(wchar_t));
79 m_wcs[len] = L'\0';
80 }
81
82 // no need to check for NULL, free() does it
83 ~wxWCharBuffer() { free(m_wcs); }
84
85 wxWCharBuffer(const wxWCharBuffer& src)
86 {
87 m_wcs = src.m_wcs;
88 // no reference count yet...
89 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
90 }
91 wxWCharBuffer& operator=(const wxWCharBuffer& src)
92 {
93 m_wcs = src.m_wcs;
94 // no reference count yet...
95 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
96 return *this;
97 }
98
99 operator const wchar_t *() const { return m_wcs; }
100
101 private:
102 wchar_t *m_wcs;
103 };
104
105 #if wxUSE_UNICODE
106 #define wxMB2WXbuf wxWCharBuffer
107 #define wxWX2MBbuf wxCharBuffer
108 #define wxWC2WXbuf wxChar*
109 #define wxWX2WCbuf wxChar*
110 #else
111 #define wxMB2WXbuf wxChar*
112 #define wxWX2MBbuf wxChar*
113 #define wxWC2WXbuf wxCharBuffer
114 #define wxWX2WCbuf wxWCharBuffer
115 #endif
116
117 // ----------------------------------------------------------------------------
118 // template class for any kind of data
119 // ----------------------------------------------------------------------------
120
121 // TODO
122
123 #endif // _WX_BUFFER_H