]>
git.saurik.com Git - wxWidgets.git/blob - src/common/objstrm.cpp
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"
24 // ----------------------------------------------------------------------------
25 // wxObjectOutputStream
26 // ----------------------------------------------------------------------------
28 wxObjectOutputStream::wxObjectOutputStream(wxOutputStream
& s
)
29 : wxFilterOutputStream(s
)
34 wxString
wxObjectOutputStream::GetObjectName(wxObject
*obj
)
38 name
.Printf("%x", (unsigned long)obj
);
42 void wxObjectOutputStream::WriteObjectDef(wxObjectStreamInfo
& info
)
44 wxDataOutputStream
data_s(*this);
46 Write(WXOBJ_BEGIN
, WXOBJ_BEG_LEN
);
49 data_s
.WriteString(info
.object
->GetClassInfo()->GetClassName());
51 data_s
.WriteString(TAG_EMPTY_OBJECT
);
54 data_s
.WriteString(GetObjectName(info
.object
));
56 // I assume an object will not have millions of children
57 data_s
.Write8(info
.children
.Number());
60 void wxObjectOutputStream::AddChild(wxObject
*obj
)
62 wxObjectStreamInfo
*info
;
67 info
= new wxObjectStreamInfo
;
70 info
->parent
= m_current_info
; // Not useful here.
71 m_current_info
->n_children
++;
72 m_current_info
->children
.Append(info
);
75 void wxObjectOutputStream::ProcessObjectDef(wxObjectStreamInfo
*info
)
79 m_current_info
= info
;
80 // First stage: get children of obj
82 info
->object
->StoreObject(*this);
84 // Prepare and write the sub-entry about the child obj.
85 WriteObjectDef(*info
);
87 node
= info
->children
.First();
90 ProcessObjectDef((wxObjectStreamInfo
*)node
->Data());
95 void wxObjectOutputStream::ProcessObjectData(wxObjectStreamInfo
*info
)
97 wxNode
*node
= info
->children
.First();
99 m_current_info
= info
;
102 info
->object
->StoreObject(*this);
105 ProcessObjectData((wxObjectStreamInfo
*)node
->Data());
110 bool wxObjectOutputStream::SaveObject(wxObject
& obj
)
112 wxObjectStreamInfo info
;
123 ProcessObjectDef(&info
);
126 ProcessObjectData(&info
);
128 info
.children
.Clear();
135 // ----------------------------------------------------------------------------
136 // wxObjectInputStream
137 // ----------------------------------------------------------------------------
139 wxObjectInputStream::wxObjectInputStream(wxInputStream
& s
)
140 : wxFilterInputStream(s
)
144 wxObject
*wxObjectInputStream::SolveName(const wxString
& name
) const
146 wxNode
*node
= m_solver
.First();
147 wxObjectStreamInfo
*info
;
150 info
= (wxObjectStreamInfo
*)node
->Data();
151 if (info
->object_name
== name
)
159 wxObject
*wxObjectInputStream::GetParent() const
161 if (!m_current_info
->parent
)
164 return m_current_info
->parent
->object
;
167 wxObject
*wxObjectInputStream::GetChild(int no
) const
169 wxNode
*node
= m_current_info
->children
.Nth(m_current_info
->children_removed
+no
);
170 wxObjectStreamInfo
*info
;
175 info
= (wxObjectStreamInfo
*)node
->Data();
180 void wxObjectInputStream::RemoveChildren(int nb
)
182 m_current_info
->children_removed
+= nb
;
185 bool wxObjectInputStream::ReadObjectDef(wxObjectStreamInfo
*info
)
187 wxDataInputStream
data_s(*this);
188 char sig
[WXOBJ_BEG_LEN
+1];
191 Read(sig
, WXOBJ_BEG_LEN
);
192 sig
[WXOBJ_BEG_LEN
] = 0;
193 if (wxString(sig
) != WXOBJ_BEGIN
)
196 class_name
= data_s
.ReadString();
197 printf("class_name = %s\n", WXSTRINGCAST class_name
);
198 if (class_name
== TAG_EMPTY_OBJECT
)
201 info
->object
= wxCreateDynamicObject( WXSTRINGCAST class_name
);
202 info
->object_name
= data_s
.ReadString();
203 printf("object_name = %s\n", WXSTRINGCAST info
->object_name
);
204 info
->n_children
= data_s
.Read8();
205 info
->children_removed
= 0;
206 printf("n_children = %d\n", info
->n_children
);
211 wxObjectStreamInfo
*wxObjectInputStream::ProcessObjectDef(wxObjectStreamInfo
*parent
)
213 wxObjectStreamInfo
*info
, *c_info
;
216 info
= new wxObjectStreamInfo
;
217 info
->parent
= parent
;
218 info
->children
.DeleteContents(TRUE
);
220 m_solver
.Append(info
);
222 if (!ReadObjectDef(info
))
225 for (c
=0;c
<info
->n_children
;c
++) {
226 c_info
= ProcessObjectDef(info
);
229 info
->children
.Append(c_info
);
235 void wxObjectInputStream::ProcessObjectData(wxObjectStreamInfo
*info
)
237 wxNode
*node
= info
->children
.First();
238 wxObjectStreamInfo
*c_info
;
240 m_current_info
= info
;
243 info
->object
->LoadObject(*this);
245 ProcessObjectData((wxObjectStreamInfo
*)node
->Data());
250 wxObject
*wxObjectInputStream::LoadObject()
252 wxObjectStreamInfo
*info
;
255 info
= ProcessObjectDef(NULL
);
258 ProcessObjectData(info
);
260 object
= info
->object
;
262 delete info
; // It's magic ! The whole tree is destroyed.