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