Raymond
Raymond

Reputation: 957

NatVis display substring of enum

We often use prefix for our enums.

It is very verbose to display full name in NatVis.

Is it possible to remove prefix of enum name (AKA return substring of enum name) ?

enum FooFormat {
  FooFormat_Foo,
  FooFormat_Bar,
  FooFormat_Baz,
  FooFormat_COUNT
};

struct Bar {
  FooFormat format;
};
<AutoVisualizer>
  <Type Name="Bar">
    <DisplayString>fmt={format,How-to-get-substring-of-enum-name-?}</DisplayString>
  </Type>
</AutoVisualizer>

Upvotes: 4

Views: 1188

Answers (2)

Werner Henze
Werner Henze

Reputation: 16771

This does not work:

<Type Name="FooFormat">
  <DisplayString Condition="this==FooFormat::FooFormat_Foo">Foo</DisplayString>
  <DisplayString Condition="this==FooFormat::FooFormat_Bar">Bar</DisplayString>
  <DisplayString>"bla"</DisplayString>
</Type>

But fortunately this works. Of course that is only feasible if your format string does not depend on too many variables, otherwise you might end up with a ton of conditional DisplayStrings.

<Type Name="Bar">
  <DisplayString Condition="format==FooFormat::FooFormat_Foo">fmt=Foo</DisplayString>
  <DisplayString Condition="format==FooFormat::FooFormat_Bar">fmt=Bar</DisplayString>
  <DisplayString>fmt={format}</DisplayString>
</Type>

Another approach, if you are using C++11 or above, I would go with scoped enums (enum class FooFormat { Foo, Bar, Baz, COUNT };). These are a bit better than regular enums and instead of FooFormat_Foo you write FooFormat::Foo. So you still have the verbose code, but the enum values have a shorter identifier and natvis only displays Foo. Of course this only works for C++, not for C.

Upvotes: 3

mr NAE
mr NAE

Reputation: 3372

Use format specifier en for this:

<AutoVisualizer>
  <Type Name="Bar">
    <DisplayString>fmt={format,en}</DisplayString>
  </Type>
</AutoVisualizer>

For example:

Bar f;
f.format = FooFormat_Bar;
... // breakpoint here

enter image description here

Upvotes: 3

Related Questions