Breealzibub
Breealzibub

Reputation: 8095

Build custom string in natvis file out of separate chars

I have a type which represents "Battleship" coordinates:

struct BattleshipCoordinates
{
    int row; // zero-based row offset
    int col; // zero-based column offset
}

Note that the coordinates are stored natively as zero-based index offsets. I would like to display these in the debugger in a more 'natural' view for Battleship coordinates (i.e. when the structure contains {0, 0} I would like the debugger to display "A1" for the upper left corner). I would like to accomplish this custom formatting with a .natvis file.

I am able to translate the values to their respective chars ('A' and '1'), but the debugger displays them in an offputting format with extra formatting:

<Type Name="BattleshipCoordinates">
  <DisplayString>{(char)(row + 'A')}{(char)(col + '1')}</DisplayString>
</Type>

There are a number of issues with this approach; the current result for {0,0} is displayed in the debugger as 65'A'49'1'. I would like to remove the extra formatting (numbers and quotation marks) and just display simply "A1". Additionally, that syntax would break as soon as the column reaches double-digit values.

What secret sauce am I missing? Is there some method through which I can stream together multiple values?

If I could access stringstreams, I could just use: ostr << static_cast<char>(row + 'A') << (col + 1). If I could call one of the available to_string functions in my code, that would also work; but to my knowledge, none of that is available in natvis syntax...

Upvotes: 3

Views: 339

Answers (0)

Related Questions