]>
git.saurik.com Git - wxWidgets.git/blob - src/common/objstrm.cpp
94200e50d8789feccdeeeceeb80b0574cce96f9e
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObjectStream classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "objstrm.h"
15 #include "wx/object.h"
16 #include "wx/objstrm.h"
17 #include "wx/datstrm.h"
19 #define WXOBJ_BEGIN "OBEGIN"
20 #define WXOBJ_BEG_LEN 6
22 #define TAG_EMPTY_OBJECT "NULL"
23 #define TAG_DUPLICATE_OBJECT "DUPLIC"
25 // ----------------------------------------------------------------------------
26 // wxObjectOutputStream
27 // ----------------------------------------------------------------------------
29 wxObjectOutputStream::wxObjectOutputStream(wxOutputStream
& s
)
30 : wxFilterOutputStream(s
)
35 wxString
wxObjectOutputStream::GetObjectName(wxObject
*obj
)
39 name
.Printf("%x", (unsigned long)obj
);
43 void wxObjectOutputStream::WriteObjectDef(wxObjectStreamInfo
& info
)
45 wxDataOutputStream
data_s(*this);
47 Write(WXOBJ_BEGIN
, WXOBJ_BEG_LEN
);
50 data_s
.WriteString(TAG_DUPLICATE_OBJECT
);
51 data_s
.WriteString(GetObjectName(info
.object
));
56 data_s
.WriteString(info
.object
->GetClassInfo()->GetClassName());
58 data_s
.WriteString(TAG_EMPTY_OBJECT
);
61 data_s
.WriteString(GetObjectName(info
.object
));
63 // I assume an object will not have millions of children
64 // Hmmm ... it could have (for example wxGrid)
65 data_s
.Write32(info
.children
.Number());
68 void wxObjectOutputStream::AddChild(wxObject
*obj
)
70 wxObjectStreamInfo
*info
;
75 info
= new wxObjectStreamInfo
;
77 if (m_saved_objs
.Member(obj
) != NULL
) {
78 info
->duplicate
= TRUE
;
80 info
->duplicate
= FALSE
;
81 m_saved_objs
.Append(obj
);
85 info
->parent
= m_current_info
; // Not useful here.
86 m_current_info
->n_children
++;
87 m_current_info
->children
.Append(info
);
90 void wxObjectOutputStream::ProcessObjectDef(wxObjectStreamInfo
*info
)
94 m_current_info
= info
;
95 // First stage: get children of obj
96 if (info
->object
&& !info
->duplicate
)
97 info
->object
->StoreObject(*this);
99 // Prepare and write the sub-entry about the child obj.
100 WriteObjectDef(*info
);
102 node
= info
->children
.First();
105 ProcessObjectDef((wxObjectStreamInfo
*)node
->Data());
110 void wxObjectOutputStream::ProcessObjectData(wxObjectStreamInfo
*info
)
112 wxNode
*node
= info
->children
.First();
114 m_current_info
= info
;
116 if (info
->object
&& !info
->duplicate
)
117 info
->object
->StoreObject(*this);
120 ProcessObjectData((wxObjectStreamInfo
*)node
->Data());
125 bool wxObjectOutputStream::SaveObject(wxObject
& obj
)
127 wxObjectStreamInfo info
;
138 ProcessObjectDef(&info
);
141 ProcessObjectData(&info
);
143 info
.children
.Clear();
144 m_saved_objs
.Clear();
151 // ----------------------------------------------------------------------------
152 // wxObjectInputStream
153 // ----------------------------------------------------------------------------
155 wxObjectInputStream::wxObjectInputStream(wxInputStream
& s
)
156 : wxFilterInputStream(s
)
160 wxObject
*wxObjectInputStream::SolveName(const wxString
& name
) const
162 wxNode
*node
= m_solver
.First();
163 wxObjectStreamInfo
*info
;
166 info
= (wxObjectStreamInfo
*)node
->Data();
167 if (info
->object_name
== name
)
172 return (wxObject
*) NULL
;
175 wxObject
*wxObjectInputStream::GetParent() const
177 if (!m_current_info
->parent
)
178 return (wxObject
*) NULL
;
180 return m_current_info
->parent
->object
;
183 wxObject
*wxObjectInputStream::GetChild(int no
) const
185 wxNode
*node
= m_current_info
->children
.Nth(m_current_info
->children_removed
+no
);
186 wxObjectStreamInfo
*info
;
189 return (wxObject
*) NULL
;
191 info
= (wxObjectStreamInfo
*)node
->Data();
196 void wxObjectInputStream::RemoveChildren(int nb
)
198 m_current_info
->children_removed
+= nb
;
201 bool wxObjectInputStream::ReadObjectDef(wxObjectStreamInfo
*info
)
203 wxDataInputStream
data_s(*this);
204 char sig
[WXOBJ_BEG_LEN
+1];
207 Read(sig
, WXOBJ_BEG_LEN
);
208 sig
[WXOBJ_BEG_LEN
] = 0;
209 if (wxString(sig
) != WXOBJ_BEGIN
)
212 class_name
= data_s
.ReadString();
213 info
->object_name
= data_s
.ReadString();
214 info
->children_removed
= 0;
216 if (class_name
== TAG_EMPTY_OBJECT
)
217 info
->object
= (wxObject
*) NULL
;
218 else if (class_name
== TAG_DUPLICATE_OBJECT
) {
219 info
->object
= SolveName(info
->object_name
);
220 info
->n_children
= 0;
222 info
->object
= wxCreateDynamicObject( WXSTRINGCAST class_name
);
223 info
->n_children
= data_s
.Read8();
228 wxObjectStreamInfo
*wxObjectInputStream::ProcessObjectDef(wxObjectStreamInfo
*parent
)
230 wxObjectStreamInfo
*info
, *c_info
;
233 info
= new wxObjectStreamInfo
;
234 info
->parent
= parent
;
235 info
->children
.DeleteContents(TRUE
);
237 m_solver
.Append(info
);
239 if (!ReadObjectDef(info
))
240 return (wxObjectStreamInfo
*) NULL
;
242 for (c
=0;c
<info
->n_children
;c
++) {
243 c_info
= ProcessObjectDef(info
);
245 return (wxObjectStreamInfo
*) NULL
;
246 info
->children
.Append(c_info
);
252 void wxObjectInputStream::ProcessObjectData(wxObjectStreamInfo
*info
)
254 wxNode
*node
= info
->children
.First();
256 m_current_info
= info
;
259 info
->object
->LoadObject(*this);
261 ProcessObjectData((wxObjectStreamInfo
*)node
->Data());
266 wxObject
*wxObjectInputStream::LoadObject()
268 wxObjectStreamInfo
*info
;
271 info
= ProcessObjectDef((wxObjectStreamInfo
*) NULL
);
273 return (wxObject
*) NULL
;
274 ProcessObjectData(info
);
276 object
= info
->object
;
278 delete info
; // It's magic ! The whole tree is destroyed.