]> git.saurik.com Git - wxWidgets.git/blame - src/common/ipcbase.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[wxWidgets.git] / src / common / ipcbase.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/ipcbase.cpp
c801d85f
KB
3// Purpose: IPC base classes
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
55d99c7a 7// Copyright: (c) Julian Smart
7beb59f3 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
c801d85f
KB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
8898456d 15 #pragma hdrstop
c801d85f
KB
16#endif
17
18#ifndef WX_PRECOMP
c801d85f
KB
19#endif
20
21#include "wx/ipcbase.h"
22
e765d7ee
SC
23wxIMPLEMENT_ABSTRACT_CLASS(wxServerBase, wxObject)
24wxIMPLEMENT_ABSTRACT_CLASS(wxClientBase, wxObject)
25wxIMPLEMENT_ABSTRACT_CLASS(wxConnectionBase, wxObject)
c801d85f 26
50c549b9
VZ
27wxConnectionBase::wxConnectionBase(void *buffer, size_t bytes)
28 : m_buffer((char *)buffer),
9d860992 29 m_buffersize(bytes),
50c549b9
VZ
30 m_deletebufferwhendone(false),
31 m_connected(true)
44379ce6 32{
50c549b9 33 if ( buffer == NULL )
44379ce6
JS
34 { // behave like next constructor
35 m_buffersize = 0;
7beb59f3 36 m_deletebufferwhendone = true;
44379ce6
JS
37 }
38}
39
40wxConnectionBase::wxConnectionBase()
50c549b9 41 : m_buffer(NULL),
bbcd408a 42 m_buffersize(0),
50c549b9
VZ
43 m_deletebufferwhendone(true),
44 m_connected(true)
44379ce6
JS
45{
46}
47
fbfb8bcc 48wxConnectionBase::wxConnectionBase(const wxConnectionBase& copy)
60431236 49 : wxObject(),
bbcd408a
JS
50 m_buffer(copy.m_buffer),
51 m_buffersize(copy.m_buffersize),
50c549b9
VZ
52 m_deletebufferwhendone(false),
53 m_connected(copy.m_connected)
bbcd408a 54
44379ce6
JS
55{
56 // copy constructor would require ref-counted pointer to buffer
9a83f860 57 wxFAIL_MSG( wxT("Copy constructor of wxConnectionBase not implemented") );
44379ce6
JS
58}
59
60
022a8a5a 61wxConnectionBase::~wxConnectionBase()
44379ce6 62{
022a8a5a 63 if ( m_deletebufferwhendone )
45633789 64 delete [] m_buffer;
44379ce6
JS
65}
66
022a8a5a
VZ
67/* static */
68wxString 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
5c33522f 82 s = wxString(static_cast<const char *>(data), size);
022a8a5a
VZ
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
5c33522f 96 s = wxString(static_cast<const wchar_t *>(data), size);
022a8a5a
VZ
97 break;
98
99 case wxIPC_UTF8TEXT:
100 if ( size )
101 size--;
102
5c33522f 103 s = wxString::FromUTF8(static_cast<const char *>(data), size);
022a8a5a
VZ
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
50c549b9 114void *wxConnectionBase::GetBufferAtLeast( size_t bytes )
44379ce6
JS
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
45633789 122 delete [] m_buffer;
50c549b9 123 m_buffer = new char[bytes];
44379ce6
JS
124 m_buffersize = bytes;
125 return m_buffer;
126 } // user-supplied buffer, fail
127 else
128 return NULL;
129 }
130}