1

In C++, I have an array of integers which I want to visualize, which elements are like this:

[0] <range 1 start, e.g. 1253>
[1] <range 1 end, e.g. 1320>
[2] <range 2 start, e.g. 1852>
[3] <range 2 end, e.g. 2528>
...
[n] 0

So, I'd like to visualize it to have custom representation, where each visualized item would be like [0] <1253-1320>; [1] <1852-2528> etc.

Currently, I have this (not giving me the desired result):

<CustomListItems>
  <Variable Name='pCurRange' InitialValue='m_pWhichRanges'/>
  <Variable Name='i' InitialValue='0'/>
  <Loop Condition='*pCurRange'>
    <Item Name='[{i,d}] begin'>*pCurRange</Item>
    <Item Name='[{i,d}] end'>*(pCurRange+1)</Item>
    <Exec>pCurRange+=2</Exec>
    <Exec>++i</Exec>
  </Loop>
</CustomListItems>

But I'd like each item to be like this (in DisplayString syntax):

<Item>{*pCurRange} - {*(pCurRange+1)}</Item>

As the contents of the item element cannot be this, I seem to be unable to do that. Also, I don't see a way to define a string (or char array) variable in the visualizer, and construct it before the Item element - above all, there are no string modifier intrinsics available to visualisers. And doing it in the item's Name attribute isn't an option, because I need that in Value column.

Can that be done in some way?

Edit: this is the request for what is required to implement my desired view.

Mike Kaganski
  • 701
  • 6
  • 18

1 Answers1

2

I think there is no direct way in natvis to do this. But why don't you change your code to better fit the data structure? First declare a

struct Range {
    int begin;
    int end;
}

and then let m_pWhichRanges point to a Range and not an int (or even better make it a std::vector or a std::array or a gsl::span). If this is not possible, then make sure that your struct Range is used anywhere in the code so that the debugger sees the type. For example add

Range* as_ranges() { return reinterpret_cast<Range*>(m_pWhichRanges); }

to your struct/class.

Now that you have the Range type you can display m_pWhichRanges as being a custom list of Ranges and let each Range display itself magically.

<CustomListItems>
  <Variable Name='pCurRange' InitialValue='(Range*)m_pWhichRanges'/>
  <Variable Name='i' InitialValue='0'/>
  <Loop Condition='pCurRange->begin'>
    <Item Name='[{i,d}]'>pCurRange,na</Item>
    <Exec>++pCurRange</Exec>
    <Exec>++i</Exec>
  </Loop>
</CustomListItems>

Please note that I added ,na to the pCurRange item so that the address is not displayed.

There is no need to do it, but if you want to you can now fine tune the display of range to for example.

<Type Name="Range">
  <DisplayString>&lt;{begin,d}, {end,d}&gt;</DisplayString>
</Type>
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • 2
    > But why don't you change *your* code to better fit the data structure? 1. Because not all code is *mine*. 2. Because not every structure fits every need ;-) – Mike Kaganski Nov 12 '18 at 18:18
  • @Werner can you please take a look here https://stackoverflow.com/questions/72359355/extend-display-range-of-arrayitems-using-natvis ? – user10634362 May 24 '22 at 12:14