I’m writing an application that is mapping out a directory structure that needs to hold as much information about each file as possible so the end user can them apply custom filters for manipulating the files and folders within. The Class that also does the file searching provides basic information back to the UI to give the end user an idea of how much data is involved. What I am trying to accomplish is recursion in to the sub objects of the class via properties to get the information that I need so that I only ever have to access the top level class to get requiered the information and not have to worry about the information in any number of sub classes.
type
TSomeClass = class(TObject)
private
FContainerForSubObjects: TObjectList<TSomeClass>;
FSomethingToBeCounted: Integer;
FHasTheDataChanged: Boolean;
private
function GetSomethingToBeCounted: Integer;
function GetHasTheDataChanged: Boolean;
public
property SomethingToBeCounted: Integer read GetSomethingToBeCounted;
property HasTheDataChanged: Boolean read GetHasTheDataChanged;
To make the class declaration make a little more sense. Look at it like this. The ContainerForSubObjects contains a folder so if you were to look at your hard drive the root class would be C:\ if there was a folder called data there would be a sub object that represented the C:\Data folder and the SomethingToBeCounted would be the number of files in each folder.
Now what I want to do in the GetSomethingToBeCounted function if as follows
- See if there are any Sub Objects (Which is easy the part)
- if there are Sub Objects, Query their
SomethingToBeCountedproperty which in turn should call theGetSomethingToBeCountedfunction to return the value of theFSomethingToBeCountedfield of the class BUT the function should act according to the condition of theHasTheDataChangedstate of the class This is where I’m hoping recursion will kick in- if the
HasTheDataChangedproperty is set to false for any contained Sub Objects is up to date then it should return it’s value and no more processing should be done - if the
HasTheDataChangedproperty is set to true data is not up to date and it should be recalculated and the new recalculated value returned. It should also set the appropriateHasTheDataChangedstate so it reduces further reprocessing.
- if the
I’m also assuming that this similar type of propagation would have to be done with the HasTheDataChanged property as well so that if a value changes somewhere in the middle of the tree all parent objects would be updated accordingly.
Hopefully those requirements make sense
Now to the meat of the question. First of all. is my thinking correct and that just by accessing the sub objects properties that the correct values will propagate their way up to root object so I don’t have to spend countless lines of code searching each sub object when there could be thousands of sub objects. Or is it a case that I’m trying to reinvent the wheel here and I’m just over looking a class that already exists and could be using instead of making my own. and Last but not least. would this be the most efficient way of doing things?
I haven’t come across an existing component that does what you want so writing your own seems to be sound.
Your method will work and is one that I have used myself many times. You have a recursive data structure in that
TSomeClasscontains a list of furtherTSomeClassobjects. Traversing this structure in any other way would be more complex.