'Understanding TYPE element in natvis file for class template

At first, I would like to share some dummy code. I have a class template which is used as a type for different variable in my project. I would like to display 2 member variable of that class template

  • The generic structure of the class_template is given in case1.h
template <class T> class Foo<T,1> : public Parent
{
    public:
        uint32_t ptr_size;
        T foo_first_ptr; // Want to display the memory values pointed by this pointer
};
  • A member value in the following class has the type of class Foo<T,1> (previous class) which is shown in case2.h
template <class T> class Foo<T,2> : public Parent
{
    public:
        Foo<T,1>* foo_second_ptr_;
};
  • I am able to display the content of ptr_size and foo_first_ptr using foo_second_ptr_.

My approach:

  • I have tried to write the natvis file is such way:
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="Foo&lt;*&gt;">
        <!-- <DisplayString>{{ptr_size = {ptr_size}}}</DisplayString> --> <!-- This one does not work. Unable to create the variable error -->
        <DisplayString>Natvis_Foo</DisplayString>
        <Expand>
            <Item Name="[ptr_size]">foo_second_ptr_.ptr_size</Item>
            <ArrayItems>
                <Size>foo_second_ptr_.ptr_size</Size>
                <ValuePointer>foo_second_ptr_.foo_first_ptr_</ValuePointer>
            </ArrayItems>
        </Expand>
    </Type>
</AutoVisualizer>

This visualization is working. Type is given Foo&lt;*&gt; which I have understood it can take Foo<T,1> & Foo<T,2> and any who is like as Foo<***>. But in my case it should take Foo<T,1> and then foo_second_ptr can fetch Foo<T,1> templates's member variable ptr_size and foo_first_ptr.

Where I have stucked

  • Foo<T,1> again is used for other variable type like the following, case3.h.
namespace XYZ{
template <class T, int some_value> class Bar_First<T,2> : public Another_Parent
{
    private:
        TYPE_A* type_a_ptr;
    public:
        typedef Foo<T, some_value> TYPE_A;
}
}

Here, also wish to print the all value stored in memory which is pointed by the type_a_ptr. What will be the Type here to display the content?

  • I have tried with <Type Name="Foo&lt;*&gt;"> but failed. My thought was that type_a_ptr type is Foo<T, Dim> and Type attribute is taking <Type Name="Foo&lt;*&gt;"> which is in general as like as Foo<T, Dim>.
  • Later I have tried with <Type Name="XYZ::Bar_First&lt;*&gt;"> and it has worked. Used natvis file is given below:
<Type Name="XYZ::Bar_First&lt;*&gt;">
    <DisplayString>Testing_tape_a_ptr_value</DisplayString>
    <Expand>
        <Item Name="[size_]">type_a_ptr.ptr_size</Item>
        <ArrayItems>
            <Size>type_a_ptr.ptr_size</Size>
            <ValuePointer>type_a_ptr.foo_first_ptr</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>

I am not understanding what is actually the real meaning of Type in natvis. I have tons of variable whose type Foo<***> and my desire is to fetch all just using a single entry. Would be glad if found the direction to approach correctly.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source