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