gpt4 book ai didi

visual-c++ - 用于 c++ 联合/结构的 Natvis 可视化工具

转载 作者:行者123 更新时间:2023-12-04 04:32:30 25 4
gpt4 key购买 nike

我正在尝试使用 msvc natvis 可视化工具实现个人可视化工具。问题是我不知道如何处理工会。
一个简单的例子,结构( value )包含两个结构( string1string2 )的并集:

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;

我可以在 natvis 中使用以下代码为 string1 和 string2 创建两种类型:
<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>

但是当我有一个“值”变量(联合)时,如何自动预览这些类型。
我坚持这一点:(假设变量类型等于 1 表示 string1,等于 2 表示 string2)。我已经做好了 :
 <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>

所以我希望根据类型值,调试将显示正确的可视化工具。
你能解释一下如何处理与 natvis 的 union 吗?或者有什么地方有一个例子? (官方 msvc 文档不考虑工会..)
显然这个例子没有意义,但它只是为了理解,因为我有一个更复杂的联合。

最佳答案

以下应该工作:

<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 移除联合 View 并使用 string1 和 resp。 string2 扩展代替,取决于类型的值。

我没有尝试使用我在此处发布的 XML,因此可能存在一些语法错误,但您应该能够通过细微的调整(如果有)使其正常工作。

关于visual-c++ - 用于 c++ 联合/结构的 Natvis 可视化工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com