Login  Register

Re: STBInFiler and events

Posted by Ian Bartholomew-19 on Apr 16, 2004; 7:38am
URL: https://forum.world.st/STBInFiler-and-events-tp3370347p3370368.html

Musulainen Vladimir wrote:

> I restore object of class A (entry point to my model) standart
> STBOutFiler, after that, I restore myself  objects from collectionB
> and collectionC.
>
> It's correct?

No.  When you store your class A instance then anything referenced by it's
instance variables (and, recursively, anything referenced by their instance
variables) are also stored in the binary stream.  When you restore the saved
bytes then _all_ the objects are restored.

A demonstration.  Copy the code below into a workspace, select it all and
then "File It In" from the Workspace menu.  You will have 3 new classes in
the image, ClassA, ClassB and ClassC.

Save the image.

Evaluate the following in an workspace.

a := ClassA new.
    10 timesRepeat: [
        b := ClassB new.
        11 timesRepeat: [
            c := ClassC new.
            12 timesRepeat: [
                c add: Time microsecondClockValue -> Time now].
            b add: c].
    a add: b].

fs := FileStream write: 'test.bin' text: false.
[a binaryStoreOn: fs] ensure: [fs close].

That will create a nested collection of instances of the new classes and
save them into a file.

Shut down and restart Dolphin.  The 3 classes added above will still be in
the image so you can open up a new workspace and evaluate.

fs := FileStream read: 'test.bin' text: false.
[a1 := Object binaryReadFrom: fs] ensure: [fs close].
a1 inspect

You should see that the entire structure that you created in the first
workspace has been reconstructed.  Note that although the objects are
recreated their original values are restored - the Times stored in the
ClassC instances are the ones from when the instance was first created.

Note that the classes for the objects stored in the file must be present in
the image.  If you had started again with a clean image in which ClassA,
ClassB and ClassC did not exist, then restoring the file will fail.

=~=~=~=~=~=
Object subclass: #ClassA

instanceVariableNames: 'collectionA'

classVariableNames: ''

poolDictionaries: ''

classInstanceVariableNames: ''!

ClassA guid: (GUID fromString: '{8358D41E-838B-4835-8B57-13338C55DC42}')!

ClassA comment: ''!

!ClassA categoriesForClass!Kernel-Objects! !

!ClassA methodsFor!

add: anObject

collectionA ifNil: [collectionA := OrderedCollection new].

collectionA add: anObject! !

!ClassA categoriesFor: #add:!public! !

Object subclass: #ClassB

instanceVariableNames: 'collectionB'

classVariableNames: ''

poolDictionaries: ''

classInstanceVariableNames: ''!

ClassB guid: (GUID fromString: '{9861C63B-DD02-4870-B94E-D0E5477421E7}')!

ClassB comment: ''!

!ClassB categoriesForClass!Kernel-Objects! !

!ClassB methodsFor!

add: anObject

collectionB ifNil: [collectionB := Set new].

collectionB add: anObject! !

!ClassB categoriesFor: #add:!public! !

Object subclass: #ClassC

instanceVariableNames: 'collectionC'

classVariableNames: ''

poolDictionaries: ''

classInstanceVariableNames: ''!

ClassC guid: (GUID fromString: '{5871B861-2105-4949-A2A6-9A720C8C87ED}')!

ClassC comment: ''!

!ClassC categoriesForClass!Kernel-Objects! !

!ClassC methodsFor!

add: anObject

collectionC ifNil: [collectionC := Bag new].

collectionC add: anObject! !

!ClassC categoriesFor: #add:!public! !

=~=~=~=~=~=

I hope that helps.  If not please ask again.

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.