| Basics of the construction and functionality of HSOK: |
|
| 20 June 2007 | |||||||||||||||||||||
Page 6 of 9 "Weak" and "strong" references"Weak" references correspond to regular pointers to objects and are represented by THSRefField class. The THSObject class using GetHSStrongRefField and SetHSStrongRefField methods supports also "strong" reference fields, represented by THSStrongRefField class. When "strong" reference field is set to point an object then it is also "registered" in this object. The object stores the list of references pointing to it and when it is released it clears automatically these references. When a reference is cleared or switched to another object it first "unregisters" itself from the referenced object and then eventually registers in the new one. This mechanism is compliant with the project pattern "Observer" and is very useful, because it simplifies significantly the code needed to manage relations between objects. It also protects the programmer from using references to not existing objects. The mechanism of strong references management is provided with AddRefFrom, ClearRefFrom, IsRefFrom and ClearReference methods implemented in THSBaseObject class. Class THSStrongRefField overrides only the method ClearRefTo, which implements automatic reference clearing by the object being released. These methods are automatically called by HSOK but it is good to know about their presence. HSOK provides serializing of "weak" and "strong" references in external files. In case of XML files, nodes to which references exist are signed with an additional attribute "ID" which is used then by stored references. What's interesting HSOK serializes even very complex structures - also these containing circular references. Only nodes that are referenced are signed with "ID" attribute. To compare, in .NET to write an object containing circular references you have to enable a special save mode (preserveObjectReferences), which has some restrictions (maxItemsInObjectGraph). In this case all nodes in the XML file are signed, which unnecessarilly increases the file size. In normal save mode when you try to write cycles .NET raises an InvalidOperationException exception "A circular reference was detected while serializing an object of type xxx". You can read more about it here. |
|||||||||||||||||||||





