Custom Visualisers in Visual Studio

I have, for a while, known about Visual Studio's autoexp.dat file, which allows one to customise how various classes are previewed in the debugger.

In the [AutoExpand] section at the beginning, there is an easy syntax to generate custom abbreviations (the one that appears as a tooltip over variables, and also to the right of the un-expanded variable in watch windows).  E.g., a class MyClass that contains a multitude of fields might have only two really useful fields “FieldA” and “FieldB”.  One may cherry-pick these with:

	MyClass =A=<FieldA>, B=<FieldB>

This shows the class via the custom text “A=value, B=value”. However, there was always this other, undocumented, mess further down the file that did loads of clever shite with STL classes and the like: the [Visualizer] section.

Enter this blog at virtualdub.org.  He goes into some detail on the syntax of this enhanced mechanism, which not only allows one to specify the preview syntax (without the braces one otherwise gets), but also to customise the tree display of member fields and also how to specify a nicely formatted HTML or XML view of the class (nice, but seemingly useless, though :-) ).

I shan't bother to parrot what he's written (sorry to seem anonymous, but I couldn't find an author ID), go and read it yourself (although I've saved a copy so if the page does go away I can plonk a copy here).

He doesn't cover everything, presumably because some of the stuff is so esoteric he couldn't figure it out, so I will add an example of my own, which is cobbled from the #tree mechanism used to display, amongst other things, maps.  We have a class with a member “Children”.  This is a map of strings to smart pointers of objects.  Ordinarily, this means opening up “Children” in the watch tree, opening up the nth. value to get a map-value, opening that up to get a first-second pair, opening the “second” to get a smart-pointer entry, opening up that (yawn) to get the smart-pointer bits, then finally opening up the actual raw pointer to the target object.

Now, this is actually a tree of objects, so once we've got to that child, we go through the same again.  And again.  It gets quite tedious very quickly.

Not any more!  I can have a custom view of our object class, which cherry picks the most common members, also allows the digging into the full class if need be, and also lists the children directly, with no map or pointer rubbish to get in the way.  Sweet.

cf::CObject {
	preview
	(
		#(
			$c.Configuration.Name,
			" (", $c.Configuration.StaticState.Value, ")"
		)
	)
	children
	(
		#(
			[CObject]: [$c,!],
			Name: [$c.Configuration.Name],
			State: [$c.Configuration.StaticState.Value],
			Parent: [$c.Parent],
			#tree
			(
				head : $c.Children._Myhead->_Parent,
				skip : $c.Children._Myhead,
				size : $c.Children._Mysize,
				left : _Left,
				right : _Right
			) : $e._Myval.second.___pData
		)
	)
}

I'll reiterate his warning, though: if you stuff this up, you'll get an error if you're lucky!  Often, you'll just crash Visual Studio.  Mind you, that happens often enough to me that I can take it with a pinch of salt :-)

This is not all to say that I'm a big fan of Visual Studio (or Windows in general), far from it.  I do have to admit, though, that I've yet to see anything out there with as decent a debugger.  I just wish eclipse-CDT would play catch-up a little faster (says the man who's only just moved on from ‘ddd’).


Advertisement



About fnx

PSN: the_phoenix
This entry was posted in Software and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>