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