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

标签 visual-c++ visual-studio-debugging natvis

我正在尝试使用 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/

相关文章:

c++ - 当我在顶点着色器中有 "out"时,OpenGL 不绘制

c# - 如何打包和部署带有符号和源代码的 NuGet 包,以便调试器可以使用该源代码?

visual-studio - -1。#IND000在Visual Studio调试窗口中是什么意思?

c++ - 如何让 std::pair<char *, char *> 在 Visual Studio 的调试器中显示为正确的字符串段?

visual-studio-debugging - 访问 Visual Studio ImageWatch 插件的 .natvis 文件中的矢量基础数据

c++ - 从 boost::archive::text_oarchive_impl 和 boost::archive::text_iarchive_impl 派生自定义存档类

c++ - const decltype(*std::begin(container))& val 不会使 val const?

c++ - 简单的 std::sort 不工作

c++ - 程序没有输出?

c++ - Natvis TreeItems 定义不适用于 map<int,int>