1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "pf_sample.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
28 #include "pf_sample.h"
30 IMPLEMENT_DYNAMIC_CLASS( classA
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS( classB
, wxObject
)
33 IMPLEMENT_SERIALIZER_CLASS( classA
,
35 classASerializer::Serialize
,
38 IMPLEMENT_SERIALIZER_CLASS( classB
,
40 classBSerializer::Serialize
,
43 // somehow original wxASSERT(0) statements get not compiled in...
46 #define wxASSERT(x) if ( !(x) ) throw;
48 extern notStorableClass gNotStorable
;
50 typedef notStorableClass
* BackRefType
;
52 void test_storing( const char* fname
, wxObjectStorage
& store
)
56 classA
* pA
= new classA();
59 classB
* pB
= new classB();
63 // put cross-references
69 // put back-references to not-storable obj
71 pA
->mpBackRef
= &gNotStorable
;
72 pB
->mpBackRef
= &gNotStorable
;
74 // create stream object for output
76 wxIOStreamWrapper outFile
;
78 bool success
= outFile
.Create( fname
, FALSE
);
82 store
.SetDataStream( outFile
);
84 // store everything starting from "pA" object
86 store
.XchgObjPtr( (wxObject
**) &pA
);
92 void test_loading( const char* fname
, wxObjectStorage
& store
)
96 // create stream-object for input
98 wxIOStreamWrapper inFile
;
100 bool success
= inFile
.Create( fname
, TRUE
);
104 store
.SetDataStream( inFile
);
108 store
.XchgObjPtr( (wxObject
**) &pA
);
110 // calls initializing procedures for serializer
111 // which provide them
117 classB
* pB
= pA
->mpBObj
;
119 // assertain correctness of class members
121 wxASSERT( pA
->x
== 1 );
122 wxASSERT( pB
->y
== 2 );
124 // assertain correctness of cross-references
126 wxASSERT( pA
->mpBObj
== pB
);
127 wxASSERT( pB
->mpAObj
== pA
);
129 // asssertain correctness of inital references
131 wxASSERT( pA
->mpBackRef
== &gNotStorable
);
132 wxASSERT( pB
->mpBackRef
== &gNotStorable
);
135 void setup_inital_refs( wxObjectStorage
& store
)
137 store
.AddInitialRef( (wxObject
*) &gNotStorable
);
140 // global instance of the object, which we do not want to store/load for
141 // some reason, even though other stored/loaded objects have refernces to it
143 notStorableClass gNotStorable
;
145 void test_storing_of_list( const char* fname
, wxObjectStorage
& store
);
146 void test_loading_of_list( const char* fname
, wxObjectStorage
& store
);
148 /*---------------------------*/
149 /* Main testing function */
150 /*---------------------------*/
152 void test_obj_storage()
154 // NOTE:: for brevity, the heap clean-ups are omitted in the tests
156 wxObjectStorage store
;
158 setup_inital_refs( store
);
160 test_storing( "testdata.dat", store
);
161 test_loading( "testdata.dat", store
);
163 test_storing_of_list( "testdata.dat", store
);
164 test_loading_of_list( "testdata.dat", store
);
167 void test_storing_of_list( const char* fname
, wxObjectStorage
& store
)
171 classA
* pA
= new classA();
174 classB
* pB
= new classB();
178 // put cross-references
184 // create list object
186 wxList
* pLst
= new wxList
;
188 // put objects to the list
190 wxNode
* pNode
= pLst
->Append( pA
);
192 pA
->mpBackRef
= (BackRefType
)pNode
;
194 pNode
= pLst
->Append( pB
);
196 pB
->mpBackRef
= (BackRefType
)pNode
;
198 // create stream object for output
200 wxIOStreamWrapper outFile
;
202 bool success
= outFile
.Create( fname
, FALSE
);
206 store
.SetDataStream( outFile
);
208 // store everything starting from "pLst" object
210 store
.XchgObjPtr( (wxObject
**) &pLst
);
216 void test_loading_of_list( const char* fname
, wxObjectStorage
& store
)
218 // create stream-object for input
220 wxIOStreamWrapper inFile
;
222 bool success
= inFile
.Create( fname
, TRUE
);
226 store
.SetDataStream( inFile
);
232 // (NOTE:: serializers for wxList/wxNode is file objstore.cpp)
234 store
.XchgObjPtr( (wxObject
**) &pLst
);
236 // assertain correctness of list and it's contents
238 wxASSERT( pLst
->Number() == 2 );
241 wxNode
* pNode
= pLst
->First();
247 classA
* pA
= (classA
*)pNode
->Data();
249 // assertain correctness of class members
251 wxASSERT( pA
->x
== 1 );
253 // assertain correctness of cross-references
255 wxASSERT( pA
->mpBObj
== (classB
*)pNode
->Next()->Data() );
257 // asssertain correctness of inital references
259 wxASSERT( (wxNode
*)pA
->mpBackRef
== pNode
)
263 classB
* pB
= (classB
*)pNode
->Data();
265 // assertain correctness of class members
267 wxASSERT( pB
->y
== 2 );
269 // assertain correctness of cross-references
271 wxASSERT( pB
->mpAObj
== (classA
*)pNode
->Previous()->Data() );
273 // asssertain correctness of inital references
275 wxASSERT( (wxNode
*)pB
->mpBackRef
== pNode
)
278 pNode
= pNode
->Next();
282 // calls initializing procedures for serializer
283 // which provide them