Reputation: 6732
I'm trying to implement a personal visualizer using msvc natvis visualizer. The problem is that I don't know how to do it regarding union.
A simple example with a structure ( value
) containing a union of two structure (string1
and string2
):
typedef struct value
{ int type; /* variable type */
union
{
string1 sval;
string2 sval2;
} t;
}
typedef struct string1
{
int size;
char *data;
} aString1;
typedef struct string2
{
int size;
char *data;
} aString2;
I can create two type for string1 and string2 with the following code in natvis :
<Type Name="string1"> /// (A) preview
<DisplayString>{{ string 1 }}</DisplayString>
<Expand>
<Item Name="text">data</Item>
</Expand>
</Type>
<Type Name="string2"> /// (B) preview
<DisplayString>{{ string 2 }}</DisplayString>
<Expand>
<Item Name="text">data</Item>
</Expand>
</Type>
But how can I automatically preview these type when I have a "value" variable (the union). I’m stuck to this point : (assuming the variable type equals 1 to indicate string1, and 2 for string2 ). I have done :
<Type Name="value">
<DisplayString>{{Value}}</DisplayString>
<Expand>
<Synthetic Name="String 1 Name" Condition="type==1"> // assume type of string1 = 1
/// here i want to call preview I have created for string1 in (A)
</Synthetic>
<Synthetic Name="String 2 Name" Condition="type==2"> // assume type of string2 = 2
/// here i want to call preview I have created for string2 in (B)
</Synthetic>
</Expand>
</Type>
So I would like that depending of the type value, the debug will show the correct visualizer. Can you explain me how to deal with union with natvis ? or is there somewhere an example ? (the official msvc documentation do not consider unions..) Obvioulsy this example makes no sense but it is just to understand because I have a far more complex union.
Upvotes: 4
Views: 1420
Reputation: 36
The following should work:
<Type Name="value">
<DisplayString Condition="type == 1">{t.sval}</DisplayString>
<DisplayString Condition="type == 2">{t.sval2}</DisplayString>
<Expand>
<ExpandedItem Condition="type == 1">t.sval</ExpandedItem>
<ExpandedItem Condition="type == 2">t.sval2</ExpandedItem>
</Expand>
</Type>
ExpandedItem removes the view of the union and uses string1 resp. string2 expansion instead, depending on the value of type.
I haven't tried to use the XML I posted here, so there could be some syntax errors, but you should be able to get it to work with minor tweaks (if any).
Upvotes: 2