]> git.saurik.com Git - wxWidgets.git/blame - src/common/objstrm.cpp
Added #include to get FIONBIO on Solaris 2.6
[wxWidgets.git] / src / common / objstrm.cpp
CommitLineData
6d44bf31
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: objstrm.cpp
3// Purpose: wxObjectStream classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 16/07/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11#ifdef __GNUG__
12#pragma implementation "objstrm.h"
13#endif
14
fcc6dddd
JS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
23#include "wx/defs.h"
24#include "wx/setup.h"
25#endif
26
27#if USE_SERIAL
28
6d44bf31
GL
29#include "wx/object.h"
30#include "wx/objstrm.h"
31#include "wx/datstrm.h"
32
33#define WXOBJ_BEGIN "OBEGIN"
34#define WXOBJ_BEG_LEN 6
35
7a4b9130 36#define TAG_EMPTY_OBJECT "NULL"
8d43638d 37#define TAG_DUPLICATE_OBJECT "DUPLIC"
7a4b9130 38
6d44bf31
GL
39// ----------------------------------------------------------------------------
40// wxObjectOutputStream
41// ----------------------------------------------------------------------------
42
43wxObjectOutputStream::wxObjectOutputStream(wxOutputStream& s)
44 : wxFilterOutputStream(s)
45{
46 m_saving = FALSE;
47}
48
49wxString wxObjectOutputStream::GetObjectName(wxObject *obj)
50{
51 wxString name;
52
53 name.Printf("%x", (unsigned long)obj);
54 return name;
55}
56
57void wxObjectOutputStream::WriteObjectDef(wxObjectStreamInfo& info)
58{
59 wxDataOutputStream data_s(*this);
60
61 Write(WXOBJ_BEGIN, WXOBJ_BEG_LEN);
7a4b9130 62
8d43638d
GL
63 if (info.duplicate) {
64 data_s.WriteString(TAG_DUPLICATE_OBJECT);
65 data_s.WriteString(GetObjectName(info.object));
f4a8c29f 66 printf("info.object (dup %s)\n", info.object->GetClassInfo()->GetClassName());
8d43638d
GL
67 return;
68 }
69
7a4b9130
GL
70 if (info.object) {
71 data_s.WriteString(info.object->GetClassInfo()->GetClassName());
f4a8c29f 72 printf("info.object (%s)\n", info.object->GetClassInfo()->GetClassName());
7a4b9130
GL
73 } else {
74 data_s.WriteString(TAG_EMPTY_OBJECT);
f4a8c29f 75 printf("info.object (NULL)\n");
1d44aaf8 76 return;
7a4b9130
GL
77 }
78
6d44bf31 79 data_s.WriteString(GetObjectName(info.object));
7a4b9130 80
6d44bf31 81 // I assume an object will not have millions of children
8d43638d
GL
82 // Hmmm ... it could have (for example wxGrid)
83 data_s.Write32(info.children.Number());
6d44bf31
GL
84}
85
1eac776c 86void wxObjectOutputStream::AddChild(wxObject *obj)
6d44bf31
GL
87{
88 wxObjectStreamInfo *info;
89
90 if (!FirstStage())
91 return;
92
93 info = new wxObjectStreamInfo;
8d43638d
GL
94
95 if (m_saved_objs.Member(obj) != NULL) {
96 info->duplicate = TRUE;
97 } else {
98 info->duplicate = FALSE;
99 m_saved_objs.Append(obj);
100 }
1d44aaf8
GL
101 if (!obj)
102 info->duplicate = FALSE;
103
6d44bf31
GL
104 info->n_children = 0;
105 info->object = obj;
7a4b9130 106 info->parent = m_current_info; // Not useful here.
6d44bf31
GL
107 m_current_info->n_children++;
108 m_current_info->children.Append(info);
109}
110
111void wxObjectOutputStream::ProcessObjectDef(wxObjectStreamInfo *info)
112{
113 wxNode *node;
114
115 m_current_info = info;
116 // First stage: get children of obj
8d43638d 117 if (info->object && !info->duplicate)
7a4b9130 118 info->object->StoreObject(*this);
6d44bf31
GL
119
120 // Prepare and write the sub-entry about the child obj.
121 WriteObjectDef(*info);
122
123 node = info->children.First();
124
125 while (node) {
126 ProcessObjectDef((wxObjectStreamInfo *)node->Data());
127 node = node->Next();
128 }
129}
130
131void wxObjectOutputStream::ProcessObjectData(wxObjectStreamInfo *info)
132{
133 wxNode *node = info->children.First();
134
135 m_current_info = info;
136
8d43638d 137 if (info->object && !info->duplicate)
7a4b9130 138 info->object->StoreObject(*this);
6d44bf31
GL
139
140 while (node) {
141 ProcessObjectData((wxObjectStreamInfo *)node->Data());
142 node = node->Next();
143 }
144}
145
146bool wxObjectOutputStream::SaveObject(wxObject& obj)
147{
148 wxObjectStreamInfo info;
149
150 if (m_saving)
151 return FALSE;
152
153 m_saving = TRUE;
154
155 // First stage
156 m_stage = 0;
157 info.object = &obj;
158 info.n_children = 0;
1d44aaf8 159 info.duplicate = FALSE;
6d44bf31
GL
160 ProcessObjectDef(&info);
161
162 m_stage = 1;
163 ProcessObjectData(&info);
164
165 info.children.Clear();
8d43638d 166 m_saved_objs.Clear();
6d44bf31
GL
167
168 m_saving = FALSE;
169
170 return TRUE;
171}
172
173// ----------------------------------------------------------------------------
174// wxObjectInputStream
175// ----------------------------------------------------------------------------
176
177wxObjectInputStream::wxObjectInputStream(wxInputStream& s)
178 : wxFilterInputStream(s)
179{
180}
181
182wxObject *wxObjectInputStream::SolveName(const wxString& name) const
183{
184 wxNode *node = m_solver.First();
185 wxObjectStreamInfo *info;
186
187 while (node) {
188 info = (wxObjectStreamInfo *)node->Data();
189 if (info->object_name == name)
190 return info->object;
191
192 node = node->Next();
193 }
c67daf87 194 return (wxObject *) NULL;
6d44bf31
GL
195}
196
7a4b9130
GL
197wxObject *wxObjectInputStream::GetParent() const
198{
199 if (!m_current_info->parent)
c67daf87 200 return (wxObject *) NULL;
7a4b9130
GL
201
202 return m_current_info->parent->object;
203}
204
1d44aaf8
GL
205wxObject *wxObjectInputStream::GetChild()
206{
207 wxObject *obj = GetChild(0);
208
209 m_current_info->children_removed++;
210
211 return obj;
212}
213
6d44bf31
GL
214wxObject *wxObjectInputStream::GetChild(int no) const
215{
1d44aaf8 216 wxNode *node;
7a4b9130
GL
217 wxObjectStreamInfo *info;
218
1d44aaf8 219 if (m_current_info->children_removed >= m_current_info->n_children)
5104949d 220 return (wxObject *) NULL;
1d44aaf8
GL
221
222 node = m_current_info->children.Nth(m_current_info->children_removed+no);
223
7a4b9130 224 if (!node)
c67daf87 225 return (wxObject *) NULL;
7a4b9130
GL
226
227 info = (wxObjectStreamInfo *)node->Data();
228
229 return info->object;
230}
231
232void wxObjectInputStream::RemoveChildren(int nb)
233{
234 m_current_info->children_removed += nb;
6d44bf31
GL
235}
236
237bool wxObjectInputStream::ReadObjectDef(wxObjectStreamInfo *info)
238{
239 wxDataInputStream data_s(*this);
240 char sig[WXOBJ_BEG_LEN+1];
7a4b9130 241 wxString class_name;
6d44bf31
GL
242
243 Read(sig, WXOBJ_BEG_LEN);
244 sig[WXOBJ_BEG_LEN] = 0;
245 if (wxString(sig) != WXOBJ_BEGIN)
246 return FALSE;
7a4b9130
GL
247
248 class_name = data_s.ReadString();
7a4b9130 249 info->children_removed = 0;
f4a8c29f 250 info->n_children = 0;
6d44bf31 251
8d43638d 252 if (class_name == TAG_EMPTY_OBJECT)
c67daf87 253 info->object = (wxObject *) NULL;
8d43638d 254 else if (class_name == TAG_DUPLICATE_OBJECT) {
1d44aaf8 255 info->object_name = data_s.ReadString();
8d43638d 256 info->object = SolveName(info->object_name);
8d43638d 257 } else {
1d44aaf8 258 info->object_name = data_s.ReadString();
8d43638d 259 info->object = wxCreateDynamicObject( WXSTRINGCAST class_name);
1d44aaf8 260 info->n_children = data_s.Read32();
8d43638d 261 }
6d44bf31
GL
262 return TRUE;
263}
264
265wxObjectStreamInfo *wxObjectInputStream::ProcessObjectDef(wxObjectStreamInfo *parent)
266{
267 wxObjectStreamInfo *info, *c_info;
268 int c;
269
270 info = new wxObjectStreamInfo;
271 info->parent = parent;
272 info->children.DeleteContents(TRUE);
273
274 m_solver.Append(info);
275
276 if (!ReadObjectDef(info))
c67daf87 277 return (wxObjectStreamInfo *) NULL;
6d44bf31
GL
278
279 for (c=0;c<info->n_children;c++) {
280 c_info = ProcessObjectDef(info);
281 if (!c_info)
c67daf87 282 return (wxObjectStreamInfo *) NULL;
6d44bf31
GL
283 info->children.Append(c_info);
284 }
285
286 return info;
287}
288
289void wxObjectInputStream::ProcessObjectData(wxObjectStreamInfo *info)
290{
291 wxNode *node = info->children.First();
6d44bf31
GL
292
293 m_current_info = info;
294
7a4b9130
GL
295 if (info->object)
296 info->object->LoadObject(*this);
6d44bf31 297 while (node) {
7a4b9130 298 ProcessObjectData((wxObjectStreamInfo *)node->Data());
6d44bf31
GL
299 node = node->Next();
300 }
1d44aaf8
GL
301
302 m_current_info = info;
303
304 if (info->recall) {
305 m_secondcall = TRUE;
306 info->object->LoadObject(*this);
307 m_secondcall = FALSE;
308 }
6d44bf31
GL
309}
310
311wxObject *wxObjectInputStream::LoadObject()
312{
313 wxObjectStreamInfo *info;
314 wxObject *object;
315
c67daf87 316 info = ProcessObjectDef((wxObjectStreamInfo *) NULL);
6d44bf31 317 if (!info)
c67daf87 318 return (wxObject *) NULL;
6d44bf31
GL
319 ProcessObjectData(info);
320
321 object = info->object;
322
323 delete info; // It's magic ! The whole tree is destroyed.
324
325 return object;
326}
fcc6dddd
JS
327
328#endif
329