Very handy file data structure base classes


I’ve made some classes that have made my application development much easier. They basically give you a particular way to structure your ‘FileData’ (i.e. the bit that is edited when a user uses your program, and gets saved when the user clicks save)

Data source archive

The classes have detailed explanations as to how they work and what they do, but i’ll just describe what they do here too…

Data is a base for each piece of Data, and is access-controlled. If you use a DataConnection<MyDataType> to access an object of your Data subclass, then you can use a simple myData.[i]exists/i check before using it as a pointer - this prevents you from accidentally accessing the data after it has been deleted. A DataConnection works like a pointer, but with the added security just mentioned, by opening a connection to the data which the data can close at any time (automatically on deletion).

Data is also a ChangeBroadcaster, and your DataConnection can be told to register any object as a listener, which happens automatically when a connection is opened. If you have a widget that uses data, you’ll have a DataConnection member, which you can tell to [i]setChangeListenerForFutureConnections /i in your constructor. Just make sure that your data subclass calls dataChanged () in all its ‘setValue()’ type functions.

The other subclass is FileDataElement, which is a subclass of Data (so personally i never subclass Data, only this). This is enhanced in that it knows it is part of a FileData structure, and it knows it will eventually get written to a File (so you don’t have to work too hard to get it ‘out’ of your program, or back in). Consider how the data will be written out as an Xml file, and design a structure accordingly, using a FileDataElement subclass for each tag.

In the initialiser for this in your subclass, you can set a tag name to represent your element. This is automatically given to the Xml tag generated by the [i]xml/i function, and is also automatically checked for a match when reading a tag with the [i]processXml/i function. The data reading/writing is done in the [i]readDataFromXmlElement/i and [i]addDataToXmlElement/i functions. As the tag name is already taken care of by the time these functions get called internally, you just need to write your member data to/read it from the given XmlElement. This will also involve calling xml() / processXml() for any contained elements.

Your root FileDataElement can subclass FileDataRootElement, which also has additional functions to automatically save/load the data using a given File object.

It has two functions you can override to store and retrieve your data to/from an XmlElement. These are called internally, and as a result provide you with a valid tag to use.

Some of the FileDataElement stuff may not be totally clear here - i will write a more useful guide for it when i have time.