]>
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 // ----------------------------------------------------------------------------
23 // wxObjectOutputStream
24 // ----------------------------------------------------------------------------
26 wxObjectOutputStream::wxObjectOutputStream(wxOutputStream
& s
)
27 : wxFilterOutputStream(s
)
32 wxString
wxObjectOutputStream::GetObjectName(wxObject
*obj
)
36 name
.Printf("%x", (unsigned long)obj
);
40 void wxObjectOutputStream::WriteObjectDef(wxObjectStreamInfo
& info
)
42 wxDataOutputStream
data_s(*this);
44 Write(WXOBJ_BEGIN
, WXOBJ_BEG_LEN
);
45 data_s
.WriteString(info
.object
->GetClassInfo()->GetClassName());
46 data_s
.WriteString(GetObjectName(info
.object
));
47 // I assume an object will not have millions of children
48 data_s
.Write8(info
.children
.Number());
51 void wxObjectOutputStream::AddChild(wxObject
*obj
)
53 wxObjectStreamInfo
*info
;
58 info
= new wxObjectStreamInfo
;
61 info
->parent
= m_current_info
->object
; // Not useful here.
62 m_current_info
->n_children
++;
63 m_current_info
->children
.Append(info
);
66 void wxObjectOutputStream::ProcessObjectDef(wxObjectStreamInfo
*info
)
70 m_current_info
= info
;
71 // First stage: get children of obj
72 info
->object
->StoreObject(*this);
74 // Prepare and write the sub-entry about the child obj.
75 WriteObjectDef(*info
);
77 node
= info
->children
.First();
80 ProcessObjectDef((wxObjectStreamInfo
*)node
->Data());
85 void wxObjectOutputStream::ProcessObjectData(wxObjectStreamInfo
*info
)
87 wxNode
*node
= info
->children
.First();
89 m_current_info
= info
;
91 info
->object
->StoreObject(*this);
94 ProcessObjectData((wxObjectStreamInfo
*)node
->Data());
99 bool wxObjectOutputStream::SaveObject(wxObject
& obj
)
101 wxObjectStreamInfo info
;
112 ProcessObjectDef(&info
);
115 ProcessObjectData(&info
);
117 info
.children
.Clear();
124 // ----------------------------------------------------------------------------
125 // wxObjectInputStream
126 // ----------------------------------------------------------------------------
128 wxObjectInputStream::wxObjectInputStream(wxInputStream
& s
)
129 : wxFilterInputStream(s
)
133 wxObject
*wxObjectInputStream::SolveName(const wxString
& name
) const
135 wxNode
*node
= m_solver
.First();
136 wxObjectStreamInfo
*info
;
139 info
= (wxObjectStreamInfo
*)node
->Data();
140 if (info
->object_name
== name
)
148 wxObject
*wxObjectInputStream::GetChild(int no
) const
150 return m_current_info
->children
.Nth(no
);
153 bool wxObjectInputStream::ReadObjectDef(wxObjectStreamInfo
*info
)
155 wxDataInputStream
data_s(*this);
156 char sig
[WXOBJ_BEG_LEN
+1];
158 Read(sig
, WXOBJ_BEG_LEN
);
159 sig
[WXOBJ_BEG_LEN
] = 0;
160 if (wxString(sig
) != WXOBJ_BEGIN
)
162 info
->object
= wxCreateDynamicObject( WXSTRINGCAST data_s
.ReadString());
163 info
->object_name
= data_s
.ReadString();
164 info
->n_children
= data_s
.Read8();
165 info
->children
= wxList();
170 wxObjectStreamInfo
*wxObjectInputStream::ProcessObjectDef(wxObjectStreamInfo
*parent
)
172 wxObjectStreamInfo
*info
, *c_info
;
175 info
= new wxObjectStreamInfo
;
176 info
->parent
= parent
;
177 info
->children
.DeleteContents(TRUE
);
179 m_solver
.Append(info
);
181 if (!ReadObjectDef(info
))
184 for (c
=0;c
<info
->n_children
;c
++) {
185 c_info
= ProcessObjectDef(info
);
188 info
->children
.Append(c_info
);
194 void wxObjectInputStream::ProcessObjectData(wxObjectStreamInfo
*info
)
196 wxNode
*node
= info
->children
.First();
197 wxObjectStreamInfo
*c_info
;
199 m_current_info
= info
;
201 info
->object
->LoadObject(*this);
203 c_info
= (wxObjectStreamInfo
*)node
->Data();
204 c_info
->object
->LoadObject(*this);
209 wxObject
*wxObjectInputStream::LoadObject()
211 wxObjectStreamInfo
*info
;
214 info
= ProcessObjectDef(NULL
);
217 ProcessObjectData(info
);
219 object
= info
->object
;
221 delete info
; // It's magic ! The whole tree is destroyed.