]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/ipcbase.cpp | |
3 | // Purpose: IPC base classes | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // Copyright: (c) Julian Smart | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #endif | |
20 | ||
21 | #include "wx/ipcbase.h" | |
22 | ||
23 | wxIMPLEMENT_ABSTRACT_CLASS(wxServerBase, wxObject) | |
24 | wxIMPLEMENT_ABSTRACT_CLASS(wxClientBase, wxObject) | |
25 | wxIMPLEMENT_ABSTRACT_CLASS(wxConnectionBase, wxObject) | |
26 | ||
27 | wxConnectionBase::wxConnectionBase(void *buffer, size_t bytes) | |
28 | : m_buffer((char *)buffer), | |
29 | m_buffersize(bytes), | |
30 | m_deletebufferwhendone(false), | |
31 | m_connected(true) | |
32 | { | |
33 | if ( buffer == NULL ) | |
34 | { // behave like next constructor | |
35 | m_buffersize = 0; | |
36 | m_deletebufferwhendone = true; | |
37 | } | |
38 | } | |
39 | ||
40 | wxConnectionBase::wxConnectionBase() | |
41 | : m_buffer(NULL), | |
42 | m_buffersize(0), | |
43 | m_deletebufferwhendone(true), | |
44 | m_connected(true) | |
45 | { | |
46 | } | |
47 | ||
48 | wxConnectionBase::wxConnectionBase(const wxConnectionBase& copy) | |
49 | : wxObject(), | |
50 | m_buffer(copy.m_buffer), | |
51 | m_buffersize(copy.m_buffersize), | |
52 | m_deletebufferwhendone(false), | |
53 | m_connected(copy.m_connected) | |
54 | ||
55 | { | |
56 | // copy constructor would require ref-counted pointer to buffer | |
57 | wxFAIL_MSG( wxT("Copy constructor of wxConnectionBase not implemented") ); | |
58 | } | |
59 | ||
60 | ||
61 | wxConnectionBase::~wxConnectionBase() | |
62 | { | |
63 | if ( m_deletebufferwhendone ) | |
64 | delete [] m_buffer; | |
65 | } | |
66 | ||
67 | /* static */ | |
68 | wxString wxConnectionBase::GetTextFromData(const void* data, | |
69 | size_t size, | |
70 | wxIPCFormat fmt) | |
71 | { | |
72 | wxString s; | |
73 | switch ( fmt ) | |
74 | { | |
75 | case wxIPC_TEXT: | |
76 | // normally the string should be NUL-terminated and size should | |
77 | // include the total size of the buffer, including NUL -- but don't | |
78 | // crash (by trying to access (size_t)-1 bytes) if it doesn't | |
79 | if ( size ) | |
80 | size--; | |
81 | ||
82 | s = wxString(static_cast<const char *>(data), size); | |
83 | break; | |
84 | ||
85 | #if wxUSE_UNICODE | |
86 | // TODO: we should handle both wxIPC_UTF16TEXT and wxIPC_UTF32TEXT here | |
87 | // for inter-platform IPC | |
88 | case wxIPC_UNICODETEXT: | |
89 | wxASSERT_MSG( !(size % sizeof(wchar_t)), "invalid buffer size" ); | |
90 | if ( size ) | |
91 | { | |
92 | size /= sizeof(wchar_t); | |
93 | size--; | |
94 | } | |
95 | ||
96 | s = wxString(static_cast<const wchar_t *>(data), size); | |
97 | break; | |
98 | ||
99 | case wxIPC_UTF8TEXT: | |
100 | if ( size ) | |
101 | size--; | |
102 | ||
103 | s = wxString::FromUTF8(static_cast<const char *>(data), size); | |
104 | break; | |
105 | #endif // wxUSE_UNICODE | |
106 | ||
107 | default: | |
108 | wxFAIL_MSG( "non-string IPC format in GetTextFromData()" ); | |
109 | } | |
110 | ||
111 | return s; | |
112 | } | |
113 | ||
114 | void *wxConnectionBase::GetBufferAtLeast( size_t bytes ) | |
115 | { | |
116 | if ( m_buffersize >= bytes ) | |
117 | return m_buffer; | |
118 | else | |
119 | { // need to resize buffer | |
120 | if ( m_deletebufferwhendone ) | |
121 | { // we're in charge of buffer, increase it | |
122 | delete [] m_buffer; | |
123 | m_buffer = new char[bytes]; | |
124 | m_buffersize = bytes; | |
125 | return m_buffer; | |
126 | } // user-supplied buffer, fail | |
127 | else | |
128 | return NULL; | |
129 | } | |
130 | } |