RTTI utility functions

The rttiutils unit is a unit providing simplified access to the RTTI information from published properties using the class. This access can be used when saving or restoring form properties at runtime, or for persisting other objects whose RTTI is available: the class can be used for this. The implementation is based on the apputils unit from RXLib by AO ROSNO and Master-Bank

Exception support and formatting TPersistent, collections and lists definitions Low-level RTTI access String routines Class for keeping property information TPropInfoList is a class which can be used to maintain a list with information about published properties of a class (or an instance). It is used internally by Create a new instance of TPropInfoList Create allocates and initializes a new instance of TPropInfoList on the heap. It retrieves a list of published properties from AObject: if Filter is empty, then all properties are retrieved. If it is not empty, then only properties of the kind specified in the set are retrieved. Instance should not be Nil Destroy Object whose properties should be listed Filter on the kind of properties to retrieve Remove the TPropInfoList instance from memory Destroy cleans up the internal structures maintained by TPropInfoList and then calls the inherited Destroy. Create Check whether a certain property is included Contains checks whether P is included in the list of properties, and returns True if it does. If P cannot be found, False is returned. Find Intersect True if P is in the list of properties Property info to check for Retrieve property information based on name Find returns a pointer to the type information of the property AName. If no such information is available, the function returns Nil. The search is performed case insensitive. Intersect Contains Pointer to property type information for AName, or Nil Name of property Delete property information from the list Delete deletes the property information at position Index from the list. It's mainly of use in the Intersect call. No checking on the validity of Index is performed. Intersect Index of item to delete. Intersect 2 property lists Intersect reduces the list of properties to the ones also contained in List, i.e. all properties which are not also present in List are removed. Delete Contains List to intersect with Number of items in the list Count is the number of property type pointers in the list. Items Indexed access to the property type pointers Items provides access to the property type pointers stored in the list. Index runs from 0 to Count-1. Count Index of property to retrieve Callback to read strings from storage TReadStrEvent is used by to read strings from a storage mechanism, in a .ini file like fashion: The call should read the string in ASection with key Item, and if it does not exist, Default should be returned. Value of Item in section ASection Section to read value from Key name for value Default return value if none was found Write a string to storage TWriteStrEvent is used by to write strings to a storage mechanism, in a .ini file like fashion: The call should write the string Value in ASection with key Item. The section and key should be created if they didn't exist yet. Section to write in Key name for value String value to write Clear a storage section TEraseSectEvent is used by to clear a storage section, in a .ini file like fashion: The call should remove all keys in the section ASection, and remove the section from storage. Section to erase Property storage mechanism

TPropsStorage provides a mechanism to store properties from any class which has published properties (usually a TPersistent descendent) in a storage mechanism.

TPropsStorage does not handle the storage by itself, instead, the storage is handled through a series of callbacks to read and/or write strings. Conversion of property types to string is handled by TPropsStorage itself: all that needs to be done is set the 3 handlers. The storage mechanism is assumed to have the structure of an .ini file : sections with key/value pairs. The three callbacks should take this into account, but they do not need to create an actual .ini file.

Store a property value StoreAnyProperty stores the property with information specified in PropInfo in the storage mechanism. The property value is retrieved from the object instance specified in the AObject property of TPropsStorage. If the property pointer is invalid or AObject is invalid, an exception will be raised. AObject LoadAnyProperty LoadProperties StoreProperties Information about property to store Load a property value LoadAnyProperty loads the property with information specified in PropInfo from the storage mechanism. The value is then applied to the object instance specified in the AObject property of TPropsStorage. If the property pointer is invalid or AObject is invalid, an exception will be raised. AObject StoreAnyProperty LoadProperties StoreProperties Information about property to load Store a list of properties StoreProperties stores the values of all properties in PropList in the storage mechanism. The list should contain names of published properties of the AObject object. If an invalid property name is specified, an exception will be raised. AObject StoreAnyProperty LoadProperties LoadAnyProperty List with names of properties to store Load a list of properties LoadProperties loads the values of all properties in PropList from the storage mechanism. The list should contain names of published properties of the AObject object. If an invalid property name is specified, an exception will be raised. AObject StoreAnyProperty StoreProperties LoadAnyProperty List with names of properties whose value should be loaded Load a list of component properties

LoadObjectsProps loads a list of component properties, relative to AComponent: the names of the component properties to load are specified as follows:

ComponentName1.PropertyName
ComponentName2.Subcomponent1.PropertyName

The component instances will be located relative to AComponent, and must therefore be names of components owned by AComponent, followed by a valid property of these components. If the componentname is missing, the property name will be assumed to be a property of AComponent itself.

The Objects property of the stringlist should be filled with the instances of the components the property references refer to: they can be filled with the call.

For example, to load the checked state of a checkbox named 'CBCheckMe' and the caption of a button named 'BPressMe', both owned by a form, the following strings should be passed:

CBCheckMe.Checked
BPressMe.Caption

and the ACompontent should be the form component that owns the button and checkbox.

Note that this call removes the value of the AObject property.

If an invalid component is specified, an exception will be raised. StoreObjectsProps LoadProperties LoadAnyProperty
Owner component of the components. List of components and their properties to load Store a list of component properties

StoreObjectsProps stores a list of component properties, relative to AComponent: the names of the component properties to store are specified as follows:

ComponentName1.PropertyName
ComponentName2.Subcomponent1.PropertyName

The component instances will be located relative to AComponent, and must therefore be names of components owned by AComponent, followed by a valid property of these components. If the componentname is missing, the property name will be assumed to be a property of AComponent itself.

The Objects property of the stringlist should be filled with the instances of the components the property references refer to: they can be filled with the call.

For example, to store the checked state of a checkbox named 'CBCheckMe' and the caption of a button named 'BPressMe', both owned by a form, the following strings should be passed:

CBCheckMe.Checked
BPressMe.Caption

and the ACompontent should be the form component that owns the button and checkbox.

Note that this call removes the value of the AObject property.

LoadObjectsProps LoadProperties LoadAnyProperty
Owner component of the components List of components and their properties to load Object to load or store properties from AObject is the object instance whose properties will be loaded or stored with any of the methods in the TPropsStorage class. Note that a call to StoreObjectProps or LoadObjectProps will destroy any value that this property might have. LoadProperties LoadAnyProperty StoreProperties StoreAnyProperty StoreObjectProps LoadObjectProps Prefix to use in storage Prefix is prepended to all property names to form the key name when writing a property to storage, or when reading a value from storage. This is useful when storing properties of multiple forms in a single section. Section name for storage Section is used as the section name when writing values to storage. Note that when writing properties of subcomponents, their names will be appended to the value specified here. Read a string value from storage OnReadString is the event handler called whenever TPropsStorage needs to read a string from storage. It should be set whenever properties need to be loaded, or an exception will be raised. OnWriteString OnEraseSection Write a string value to storage OnWriteString is the event handler called whenever TPropsStorage needs to write a string to storage. It should be set whenever properties need to be stored, or an exception will be raised. OnReadString OnEraseSection Erase a section in storage OnEraseSection is the event handler called whenever TPropsStorage needs to clear a complete storage section. It should be set whenever stringlist properties need to be stored, or an exception will be raised. OnReadString OnWriteString Update a stringlist with object references

UpdateStoredList will parse the strings in AStoredList using and will replace the Objects properties with the instance of the object whose name each property path in the list refers to. If FromForm is True, then all instances are searched relative to AComponent, i.e. they must be owned by AComponent. If FromForm is False the instances are searched in the global list of streamed components. (the callback must be set for the search to work correctly in this case)

If a component cannot be found, the reference string to the property is removed from the stringlist.

If AComponent is Nil, an exception may be raised.
Root component List of property references to resolve Start search from form or search globally Concatenates component and property name

CreateStoredItem concatenates CompName and PropName if they are both empty. The names are separated by a dot (.) character. If either of the names is empty, an empty string is returned.

This function can be used to create items for the list of properties such as used in , or .

Full property path Component name Property name Split a property reference to component reference and property name ParseStoredItem parses the property reference Item and splits it in a reference to a component (returned in CompName) and a name of a property (returned in PropName). This function basically does the opposite of . Note that both names should be non-empty, i.e., at least 1 dot character must appear in Item. If an error occurred during parsing, False is returned. True if Item contains a valid property reference Property reference to parse Component part of property reference. Property name part of property reference. Separator used when constructing section/key names Event callback to locate a component instance TFindComponentEvent should return the component instance for the component with name path Name. The name path should be relative to the global list of loaded components. Component instance Name path of component to search for Callback used when component references must be resolved. FindGlobalComponentCallBack is called by whenever it needs to resolve component references. It should be set to a routine that locates a loaded component in the global list of loaded components.