Configuring NetBeans Code Templates

So, I’m sitting there making a C++ class, minding my own business and…

#ifndef FOO_H
#define FOO_H

class foo {
public:
virtual ~foo();
foo();
foo(const foo& orig);
private:
};

#endif /* FOO_H */

What?! NO! Where are my 500 nested namespaces?! Why is this default constructor public?! Where is my operator=?! Why would they do this?! No wonder the cool kids use Eclipse…

Luckily for us, this is fixable.

First, launch NetBeans.

Click Tools->Templates

Click Settings. First, we need to create some custom properties. I like to use a universal top-level namespace, and a subject-specific namespace for my personal projects. Enter:

BASE_NAMESPACE=clt
SUB_NAMESPACE=/*PLACEHOLDER*/

Save and close the User.properties tab. Click Tools->Templates. Expand C++, and click class.h. Click Open in Editor. You’ll see some suspiciously familiar text. Let’s go ahead and best practices it up:

#ifndef %<%GUARD_NAME%>%
#define %<%GUARD_NAME%>%

namespace %<%BASE_NAMESPACE%>%
{
namespace %<%SUB_NAMESPACE%>%
{
class %<%CLASSNAME%>%
{
public:
virtual ~%<%CLASSNAME%>%();
private:
%<%CLASSNAME%>%();
%<%CLASSNAME%>%(const %<%CLASSNAME%>%& orig);
%<%CLASSNAME%>%& operator=(const %<%CLASSNAME%>%& orig);
};
}
}
#endif /* %<%GUARD_NAME%>% */

We’ve declared operator=, and we’ve moved the default constructor, copy constructor, and operator= into the private section. We’ve also added our namespaces. Save and close the class.h tab. While we’re at it, why don’t we update the Class.cpp file. Click Tools->Templates. Expand C++, and click C++ Class. Click Open in Editor. Let’s use our namespaces, and add our operator=:

#include %<%QUOTES%>%%<%NAME%>%.%<%DEFAULT_HEADER_EXT%>%%<%QUOTES%>%

using namespace %<%BASE_NAMESPACE%>%::%<%SUB_NAMESPACE%>%;

%<%CLASSNAME%>%::%<%CLASSNAME%>%() {
}

%<%CLASSNAME%>%::%<%CLASSNAME%>%(const %<%CLASSNAME%>%& orig) {
}

%<%CLASSNAME%>%::~%<%CLASSNAME%>%() {
}

%<%CLASSNAME%>%& %<%CLASSNAME%>%::operator=(const %<%CLASSNAME%>%& orig){

}

Save and close the C++ Class tab. Now, let’s try this again. Right click your project, and select New->C++ Class… You should now end up with a .cpp file and .h file that look like this:

#ifndef FOO_H
#define FOO_H

namespace clt {
namespace /*PLACEHOLDER*/ {

class foo {
public:
virtual ~foo();
private:
foo();
foo(const foo& orig);
foo& operator=(const foo& orig);
};
}
}
#endif /* FOO_H */

…and…

#include "foo.h"

using namespace clt::/*PLACEHOLDER*/;

foo::foo() {
}

foo::foo(const foo& orig) {
}

foo::~foo() {
}

foo& foo::operator=(const foo& orig) {

}

You’ll need to change /*PLACEHOLDER*/ to an actual name before you can use these files, but I like the red jaggies; makes it hard to forget to fix them. Regardelss, we now have a class template that’d make Scott Meyers proud!

2 responses to “Configuring NetBeans Code Templates”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: