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