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