Saturday, November 15, 2014

Program Code – Standard Vector

The Program Code class holds the code of the program in a vector.  The vector consists of program words.  This class was derived from the QVector, which exposes all of base functions.  This not the best object-oriented design practice, and was redesigned to instead contain a standard vector member.  The program words are defined by the Program Word class, which consists of various access functions to access the code, sub-code and operand components of program word.

Several functions are needed to access the member vector including the begin iterator, end iterator, empty status, size, clear vector, element access bracket operator ([]) and the emplace back functions.  These functions simply pass through to the code vector member.  The main program code insert line, remove line, and replace line access functions were modified to use standard vector functions (insert and erase or using the direct standard copy) to manipulate the code vector instead of the raw memory move function previously used.

Using the vector functions simplified the code, but since there is no function to replace part of the vector with a different size part, the replace line still contains various checks and operations depending on size of the old and new lines.  For an empty new line, the remove line function is called as before.  For a same size or larger new line, the part of the new line that will fit in the space of old line is copied.  For a larger new line, the rest of the new line is then inserted.  For a smaller new line, the new line is copied, and the remainder of the old line is erased.

The debug text, dereference and decode functions of the program model previously obtained a pointer to the first word of the program line by adding its offset to the the raw data access function.  These were changed to obtain a vector iterator to the line by adding the line offset to the begin iterator of the program code vector.  The C++11 auto type was used to define these iterators, which is simpler then using std::vector<ProgramWord>::const_iterator or the ProgramCode::const_iterator alias added to the program code class.

The encode function of the program model previously created a sized vector for the new line (using a constructor taking a size argument), and each word was set to the instruction or operand information from the RPN list.  This was replaced by starting with an empty vector and using the emplace back template function for each instruction and operand word.  The set instruction and set operand functions of the program word class were replaced with corresponding constructors needed for the emplace back function.  The size constructor was removed.

The Program Word and Program Code classes were moved to a new header file since the program model header file.  The  insert line, remove line, and replace line functions were kept as in-line functions in the class definition.  Each of these functions are only called once, so in-lining is appropriate.  This header file did not show up in the project file list in QtCreator (the project built fine) because there was no associated source file.  A new headers variable was added to the CMake build file with this new header file and the main program target was made dependent on this variable so that QtCreator knows about this new header file.

[branch misc-cpp-stl commit c7080421cb]

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)