]> git.saurik.com Git - wxWidgets.git/blame - include/wx/buffer.h
Unicode. You know the drill.
[wxWidgets.git] / include / wx / buffer.h
CommitLineData
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
f93d01be
OK
17#include <string.h> // strdup
18#include <wchar.h> // wchar_t
14971e5b
VZ
19
20// ----------------------------------------------------------------------------
21// Special classes for (wide) character strings: they use malloc/free instead
22// of new/delete
23// ----------------------------------------------------------------------------
24
25class wxCharBuffer
26{
27public:
28 wxCharBuffer(const char *str)
29 {
f93d01be 30 wxASSERT_MSG( str, _T("NULL string in wxCharBuffer") );
14971e5b
VZ
31
32 m_str = str ? strdup(str) : (char *)NULL;
33 }
f93d01be
OK
34 wxCharBuffer(size_t len)
35 {
36 m_str = (char *)malloc(len+1);
37 m_str[len] = '\0';
38 }
14971e5b
VZ
39 // no need to check for NULL, free() does it
40 ~wxCharBuffer() { free(m_str); }
41
f93d01be
OK
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
14971e5b
VZ
56 operator const char *() const { return m_str; }
57
58private:
59 char *m_str;
60};
61
62class wxWCharBuffer
63{
64public:
65 wxWCharBuffer(const wchar_t *wcs)
66 {
f93d01be 67 wxASSERT_MSG( wcs, _T("NULL string in wxWCharBuffer") );
14971e5b 68
f93d01be
OK
69 m_wcs = wcs ? (wchar_t *)malloc((wcslen(wcs)+1)*sizeof(wchar_t))
70 : (wchar_t *)NULL;
71 if (m_wcs) wcscpy(m_wcs, wcs);
72 }
73 wxWCharBuffer(size_t len)
74 {
75 m_wcs = (wchar_t *)malloc((len+1)*sizeof(wchar_t));
76 m_wcs[len] = L'\0';
14971e5b
VZ
77 }
78
79 // no need to check for NULL, free() does it
80 ~wxWCharBuffer() { free(m_wcs); }
81
f93d01be
OK
82 wxWCharBuffer(const wxWCharBuffer& src)
83 {
84 m_wcs = src.m_wcs;
85 // no reference count yet...
86 (wxWCharBuffer)src.m_wcs = (wchar_t *)NULL;
87 }
88 wxWCharBuffer& operator=(const wxWCharBuffer& src)
89 {
90 m_wcs = src.m_wcs;
91 // no reference count yet...
92 (wxWCharBuffer)src.m_wcs = (wchar_t *)NULL;
93 return *this;
94 }
95
14971e5b
VZ
96 operator const wchar_t *() const { return m_wcs; }
97
98private:
99 wchar_t *m_wcs;
100};
101
f93d01be
OK
102#if wxUSE_UNICODE
103#define wxMB2WXbuf wxWCharBuffer
104#define wxWX2MBbuf wxCharBuffer
105#define wxWC2WXbuf wxChar*
106#define wxWX2WCbuf wxChar*
107#else
108#define wxMB2WXbuf wxChar*
109#define wxWX2MBbuf wxChar*
110#define wxWC2WXbuf wxCharBuffer
111#define wxWX2WCbuf wxWCharBuffer
112#endif
113
14971e5b
VZ
114// ----------------------------------------------------------------------------
115// template class for any kind of data
116// ----------------------------------------------------------------------------
117
118// TODO
119
120#endif // _WX_BUFFER_H