debug visualizer of C++ custom types for Visual Studio - c++

I am working with a library with some awkward types. When debugging in Visual Studio, I would like to display them in a readable form. I found some really useful articles about how to edit autoexp.dat file.
http://www.idigitalhouse.com/Blog/?p=83
or
http://mariusbancila.ro/blog/2007/04/06/tweaking-autoexpdat-for-custom-types-in-vs2005/
Suppose I have a String class:
class String {
//...
private:
char *_cbuf;
}
then I can add the visualizer easily because _cbuf is a member variable. I just write
String{
preview (
[$c._cbuf]
)
}
at the beginning of [Visualizer] section in autoexp.dat file and it works.
But suppose that I want to display a more complex type which does not have any useful member variables but it has very useful methods. E.g.:
class Date {
//...
String asString() const;
private:
long _someReallyStrangeAndUnusefulDateRepresentation;
}
And I want to display the string rather than the unuseful long. How to do that? Writing
Date{
preview (
[$c.asString()]
)
}
in autoexp.dat does not work.

OK, after some research it seems it is generally possible but not directly by just editing of autoexp.dat.
First solution is to use EEAddIn.dll as described here: http://msdn.microsoft.com/en-us/library/8fwk67y3%28v=VS.90%29.aspx
another solution might be using inlined function as in Lucien Murray-Pitts' comment down on this page: http://www.virtualdub.org/blog/pivot/entry.php?id=120 which is inspired by boost debug visualizers.
However I have not yet tried any of these.

Related

What does the code does and how can i use it in my custom projects?

I fount the below code in QT header file named "mywidget.h".
Could any one please clarify the below questions:
What the code does ?
Is it a good practice ?
Where can I find similar kind of code snippets to enhance my knowledge?
#ifndef %PRE_DEF%
#define %PRE_DEF%
#include <%BASECLASS%>
class %CLASS% : public %BASECLASS%
{
Q_OBJECT
public:
%CLASS%(QWidget *parent = 0);
~%CLASS%();
};
#endif // %PRE_DEF%
This code is just a code snippet used to create new class.
What it does?
It just inserts this code snippet into your text/source file and selects ( or marks ) all `%NAME% keywords which let's you change it to desired name.
Is it a good practice?
It depends. If you're tired of typing new classes and you know how to do this then answer is yes it is. It let's you create object definitions in less steps.
Where can I find similar kind of code snippets to enhance my knowledge?
This depends on IDE you're using. With Visual Studio you can add these in :
C:\Users\your_username\Documents\Visual Studio version\Code Snippets
There you'll find different folders named like Visual C++ in which you can add them.
EDIT:
Answering to the comment:
You dont have to use any kind of IDE, you can just copy the content by hand and modify it. But ( just for clarity ) it's easier to use IDE instead of hand copying and changing the content.
Complete explanation on how to use code snippets:
Qt Creator
Visual Studio

C++ - Using a variable without knowing what it is called

I have a program that uses plug-ins. As I'm in development, these plug-ins are currently just .h and .cpp files that I add or remove from my project before re-compiling, but eventually they will be libraries.
Each plug-in contains lists of data in vectors, and I need to dynamically load data from the plug-ins without knowing which plug-ins are present. For instance:
// plugin1.h
extern vector<int> plugin1Data;
// plugin2.h
extern vector<int> plugin2Data;
// main.cpp
vector<vector<int>> pluginDataList;
int CountPlugins () {
// Some function that counts how many plug-ins are present, got this bit covered ;)
}
int main() {
int numPlugins = CountPlugins();
for (int i = 0; i < numPlugins; i++) {
vector<int> newPluginData = /***WAY TO ADD PLUGIN DATA!!!***/;
pluginDataList.push_back(newPluginData);
}
}
I already access the names of each plugin present during my CountPlugins() function, and have a list of names, so my first gut feeling was to use the name from each plugin to create a variable name like:
vector<string> pluginNames = /*filled by CountPlugins*/;
string pluginDataName = pluginNames.at(i) + "Data";
// Use pluginDataName to locate plugin1Data or plugin2Data
That's something I've done before in c# when I used to mess around with unity, but I've read a few stackoverflow posts clearly stating that it's not possible in c++. It's also a fairly messy solution in C# anyway as far as I remember.
If each plugin was a class instead of just a group of vectors, I could access the specific data doing something like plugin2.data... but then I still need to be able to reference the object stored within each plugin, and that'll mean that when I get round to compiling the plugins as libraries, I'll always have to link to class declaration and definition, which isn't ideal (though not out of the question if it'll give a nicer solution over all).
I'm all out of ideas after that, any help you can offer will be most welcome!
Thanks! Pete
Why dont you save the data as JSON between the application and the plugins ? That way you will also allow other types of tech to plug-into your app, like javascript based plugins via an embedded version of v8 or c#/.net plugins via mono.'

NetBeans code-template expansion; string manipulation

I'm trying to use the Code Templates feature with PHP in NetBeans (7.3), however I'm finding it rather limited. Given the following desired output:
public function addFoo(Foo $foo) {
$this->fooCollection[] = $foo;
}
I'm trying to have every instance of "foo"/"Foo" be variable; so I used a variable:
public function add${name}(${name} $$${name}) {
$this->${name}Collection[] = $$${name};
}
Of course, when expanded there isn't any regard given to the desired capitalization rules, because I can't find a way to implement that; the result being (given I populate ${name} with "Foo"):
public function addFoo(Foo $Foo) { // note the uppercase "Foo" in the argument
$this->FooCollection[] = $Foo; // and collection property names...
} // not what I had in mind
Now, I've read that NetBeans supports FreeMarker in it's templates, but that seems to be only for file-templates and not snippet-templates like these.
As far as I can tell, the FreeMarker version would look something like the following; however, it doesn't work, and ${name?capitalize} is simply seen as another variable name.
public function add${name?capitalize}(${name?capitalize} $$${name}) {
$this->${name}Collection[] = $$${name};
}
Passing "foo", allowing capitalize to fix it for type-names, second-words, etc.
Is there any way to get FreeMarker support here, or an alternative?
I'm open to any suggestions really; third-party plugins included. I just don't want to have to abandon NetBeans.
Addendum
The example given is trivial; an obvious solution for it specifically would be:
public function add${upperName}(${upperName} $$${lowerName}) {
$this->${lowerName}Collection[] = $$${lowerName};
}
Where upper/lower would be "Foo"/"foo" respectively. However, it's just an example, and I'm looking for something more robust in general (such as FreeMarker support)

In the .cpp, is there a way to auto-implement all the functions from its .h?

I think this would increase the quality of life when devving, but google came up with nothing and I couldn't find anything specific inside inside Netbeans either.
What I want is to start with this header:
class bla
{
public:
static void gfg(somearg asd);
};
Then I open the blank bla.cpp and pressed 'autoimplement'. After that, it would look like this:
#include "bla.h"
static void bla::gfg(somearg asd)
{
//TODO: implement
throw unimplemented("void bla::gfg(somearg) is unimplemented");
}
Anyone know of a tool like this?
I found http://www.radwin.org/michael/projects/stubgen/
"stubgen is a C++ development tool that keeps code files in sync with their associated headers. When it finds a member function declaration in a header file that doesn't have a corresponding implementation, it creates an empty skeleton with descriptive comment headers."
This looks like it does exactly what you want it to do.
Some time has passed and in the meantime the requested feature seems to have been implemented in netbeans. Refer to https://netbeans.org/bugzilla/show_bug.cgi?id=213811 , which also gives a description on how to use it:
Note:
Implemented CTRL+SPACE.
IDE suggest implementing of class method if CTRL+SPACE was pressed:
- inside file that already has at least one method definition
- between method declarations

How do I write a very simple Visual Studio debugger visualizer?

I'm trying to write an 'autoexp.dat'-based visualizer for a string type. I've scaled-back my ambitions to attempting to write a visualizer for a really simple test type that contains a null-terminated string field:
namespace thizz { namespace izz {
class MyType {
const char* _ptr;
public:
MyType(const char* ptr) : _ptr(ptr) {}
};
}
}
This is my stab at a visualiser, but it has no effect on how Visual Studio (2010) displays an instance of this type:
thizz::izz::MyType
{
preview ([$e._ptr,s])
}
(That's going at the top of the [Visualizers] section in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packages\Debugger\autoexp.dat).
Watching an instance of this type:
thizz::izz::MyType t("testing testing");
Just displays
t | {_ptr=0x0f56a6fc "testing testing" } | thizz::izz::MyType
in the Watch window.
To get an even more versatile viewer try changing to use this:
thizz::izz::MyType {
preview ( #( [$e._ptr,s] ) )
stringview ( #( [$e._ptr,sb] ) )
}
this will also give the magnifying glass icon which will open a larger text view window in the case that you have a longer string. It'll also give you the option of rendering as HTML or XML.
Note that as well as the format of the file being sensitive to whitespace, I've also found that you can't use a colon in the string otherwise it generates parse errors.
The debugger visualisers are incredibly powerful, though the syntax can be quite bewildering. As general advice I would suggest creating some entries first in the [AutoExpand] section to summarise the data types that you are most interested in, and then if you have custom containers then copy and adapt the examples for vector, list, etc, which will give you the largest return for the investment in your time.
I can't give a categorical reason why my original 'code' in autoexp.dat was not working, but I found that the same code worked when all the whitespace was removed.
I then tried re-adding whitespace and found that keeping the initial open brace on the first line was necessary to keep the definition working.