]> git.saurik.com Git - wxWidgets.git/blame - include/wx/buffer.h
Added GSocket/wxSocket alias to socket.h to prevent us from using GSocket
[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
853d7d3d 17#include "wx/wxchar.h"
f93d01be 18#include <string.h> // strdup
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...
d87c0ac7 46 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
f93d01be
OK
47 }
48 wxCharBuffer& operator=(const wxCharBuffer& src)
49 {
50 m_str = src.m_str;
51 // no reference count yet...
d87c0ac7 52 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
f93d01be
OK
53 return *this;
54 }
55
14971e5b 56 operator const char *() const { return m_str; }
b1fa8b4e 57 char operator[](size_t n) const { return m_str[n]; }
14971e5b
VZ
58
59private:
60 char *m_str;
61};
62
7a3e402c 63#if wxUSE_WCHAR_T
14971e5b
VZ
64class wxWCharBuffer
65{
66public:
67 wxWCharBuffer(const wchar_t *wcs)
68 {
f93d01be 69 wxASSERT_MSG( wcs, _T("NULL string in wxWCharBuffer") );
14971e5b 70
853d7d3d
OK
71 if (wcs) {
72 size_t siz = (wcslen(wcs)+1)*sizeof(wchar_t);
73 m_wcs = (wchar_t *)malloc(siz);
74 memcpy(m_wcs, wcs, siz);
75 }
76 else m_wcs = (wchar_t *)NULL;
f93d01be
OK
77 }
78 wxWCharBuffer(size_t len)
79 {
80 m_wcs = (wchar_t *)malloc((len+1)*sizeof(wchar_t));
81 m_wcs[len] = L'\0';
14971e5b
VZ
82 }
83
84 // no need to check for NULL, free() does it
85 ~wxWCharBuffer() { free(m_wcs); }
86
f93d01be
OK
87 wxWCharBuffer(const wxWCharBuffer& src)
88 {
89 m_wcs = src.m_wcs;
90 // no reference count yet...
20456250 91 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
f93d01be
OK
92 }
93 wxWCharBuffer& operator=(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 return *this;
99 }
100
14971e5b 101 operator const wchar_t *() const { return m_wcs; }
b1fa8b4e
OK
102 wchar_t operator[](size_t n) const { return m_wcs[n]; }
103
14971e5b
VZ
104private:
105 wchar_t *m_wcs;
106};
7a3e402c 107#endif
14971e5b 108
f93d01be
OK
109#if wxUSE_UNICODE
110#define wxMB2WXbuf wxWCharBuffer
111#define wxWX2MBbuf wxCharBuffer
112#define wxWC2WXbuf wxChar*
113#define wxWX2WCbuf wxChar*
114#else
115#define wxMB2WXbuf wxChar*
116#define wxWX2MBbuf wxChar*
117#define wxWC2WXbuf wxCharBuffer
118#define wxWX2WCbuf wxWCharBuffer
119#endif
120
14971e5b
VZ
121// ----------------------------------------------------------------------------
122// template class for any kind of data
123// ----------------------------------------------------------------------------
124
125// TODO
126
127#endif // _WX_BUFFER_H