Saturday, February 7, 2015

Program Model Access Refactoring

Upon reviewing the most recent changes made to the encode, operand text, and remove virtual table functions, I decided that some refactoring was in order to clean up how the program model is accessed from these functions and how the program line reader is utilized.

I observed that for each of the three locations that created a program line reader instance were identical.  The program line reader constructor contained three arguments, the beginning of the code vector, the offset of the line within the code vector and size of the line.  For code readability, the number of arguments in a function call should be as few as possible and three is a little too many.

In all three constructions, the offset and size of the line were obtained from a line info structure.  This could have been one way to reduce the number of arguments by one - to pass just a reference to a line info structure.  This would have meant that the program code header file where the program line reader class is located, would need access to the line info structure, which is an internal class to the program model.

The more header files included, the longer compilation takes, so this was not a desirable solution, plus there would still be two arguments.  This would be an improvement, but there was an alternative to eliminate the other argument - the begin code vector iterator.

Since the program model contains the code vector, the line info structure definition and includes the program code header file containing the program line reader class definition, a new create program line reader function was added to the program model class.  This function contains a single reference to a line info structure argument and creates a program line reader instance from this argument and the begin code vector iterator.

[branch table commit ba6e5ee46d]

Table – Remove Virtual Function

The remove function is called to remove a reference to a dictionary entry for the operand of a code, possibly removing the entry if there are no more references to it.  This function is used by the program model dereference function called when program lines are removed or replaced.  This function had to check if the code had an operand (using the is code with operand access function) before the virtual function could be called.

The program model dereference function was modified to use a program line reader instance.  The remove function function was modified to check if the remove function pointer is not null before calling the remove function of the table entry.  By default, for codes without operands, the remove function will do nothing.

The operand argument of the remove virtual functions was changed to a reference to a program line reader instance.  The remove functions were modified to use the program line reader instance to obtain the operand to pass to the desired dictionary to remove the reference to the item.

[branch table commit b980c95369]