【问题标题】:Natvis visualizer for c++ union/structure用于 c++ 联合/结构的 Natvis 可视化工具
【发布时间】:2016-05-25 18:06:13
【问题描述】:

我正在尝试使用 msvc natvis 可视化器实现个人可视化器。问题是我不知道如何在工会方面做到这一点。 一个包含两个结构(string1string2)联合的结构(value)的简单示例:

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 的联合吗?或者有什么例子吗? (官方的 msvc 文档不考虑工会..) 显然这个例子没有意义,但它只是为了理解,因为我有一个更复杂的联合。

【问题讨论】:

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


    【解决方案1】:

    以下应该有效:

    <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 移除并集的视图并分别使用 string1string2 扩展代替,取决于 type 的值。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2011-02-21
      • 2010-12-31
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多