Thursday, October 30, 2014

Parser – Unique Pointers

The parser routines create a token held in a shared pointer upon return.  The main function operator routine returns this shared pointer.  The token is not actually being shared, just moved until it reaches the caller.  There is no reason to use a shared pointer in the parser as a standard unique pointer is sufficient.  The parser routines were changed to return a unique pointer.  Another alias was added for a unique token pointer:
using TokenUniquePtr = std::unique_ptr<Token>;
Unfortunately, there is no equivalent function for std::unique_ptr like the std::make_shared() function for std::shared_ptr (though one has been added for C++14) so unique pointers must be initialized using the new operator with the unique token pointer alias constructor:
return TokenUniquePtr{new Token {pos, len, type, dataType, m_input}};
The callers of the parser operator function did not needed to be modified since there is a shared pointer constructor that takes unique pointer as an argument (the shared pointer takes ownership of unique pointer).  The table new token function was also modified to return a unique token pointer.

One other small unrelated change was made to the get identifier routine with the creation of the REM command token.  This code was simplified as it was not necessary to copy the comment string from the input into a temporary string before creating the token.  The string can be passed directly when the token is created.  The new position can simply be set to the length of the input string.  This was already done for the remark operator in the get operator routine.

[branch parser commit 336ad07bf8]