简体   繁体   English

使用运行时字符串在 natvis 中指定 object 类型

[英]Using a runtime string to specify object type in natvis

Say I have a struct说我有一个结构

struct Foo
{
    void* bar;
    const char* barTypeName;
}

bar is some type erased thing and barTypeName is a proper C++ type identifier that identifies the actual type of bar . bar是一些类型擦除的东西, barTypeName是一个正确的 C++ 类型标识符,用于标识bar的实际类型。

I want to visualize this in the Visual Studio debugger, particularly in the Watch window. There's no template involved that can give me the proper type.我想在 Visual Studio 调试器中对此进行可视化,尤其是在 Watch window 中。没有涉及的模板可以为我提供正确的类型。 The type itself is frequently POD and the debugger isn't able to figure out the type automatically.类型本身通常是 POD,调试器无法自动找出类型。

Question: Is there any way in natvis to tell the debugger the type of bar so it displays properly in the Watch window?问题:在 natvis 中有什么方法可以告诉调试器bar的类型,以便它在 Watch window 中正确显示?

I stumbled on <MostDerivedType> in the natvis schema , but it's not documented as far as I can tell and I can't tell if it does what I'm after or not.我在natvis schema中偶然发现了<MostDerivedType> ,但据我所知没有记录,我也不知道它是否做了我想要的。

I'm happy enough to use <CustomVisualizer> and implement this in C++ if it provides a way to handle this and natvis does not.我很高兴使用<CustomVisualizer>并在 C++ 中实现它,如果它提供了一种处理方法而 natvis 没有。

That is quite simple if you are willing to add a DisplayString for each wrapped POD.如果您愿意为每个包装的 POD 添加一个DisplayString ,那将非常简单。 If you want a generic solution, that might not be possible.如果你想要一个通用的解决方案,那可能是不可能的。

<Type Name="Foo">
  <DisplayString Condition='strcmp(barTypeName,"char")'>{(char)bar}</DisplayString>
  <DisplayString Condition='strcmp(barTypeName,"int")'>{(int)bar}</DisplayString>
</Type>

Code for testing:测试代码:

char c{};
int i{};
Foo fooc{ &c, "char" };
Foo fooi{ &i, "int" };

And this is the result in the VS 2019 (16.11.11) debugger:这是 VS 2019 (16.11.11) 调试器中的结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM