Macro to Add New Classes to Visual C++ Projects

I use the following macro to add a class to my project. There are other implementations of this sort of macro. However, I believe that my version does a little bit more than most and even it adds some basic functions to the newly created class. As an example, this macro will not only create the class' definition for you, it also automatically generates such basic class functions as the constructor, destructor and copy constructor.

In addition, I always use a protected assign method; I call this method both from my operator= as well as my copy constructor. I put the assign function in the code too. Obviously not everyone has the same function requirements so feel free to edit away!

Function GetProjectPath(ByVal proj)
 dim szProjectPath
 dim nIndex
 dim szReturn

 szProjectPath = proj.FullName
 nIndex = InStrRev(szProjectPath, "\")
 
 if (nIndex <> 0) then
  szReturn = Left(szProjectPath, nIndex)
 end if

 GetProjectPath = szReturn
End Function

Sub ClassGenerator
'DESCRIPTION: Creates minimal source/header files for a 
' class, given its input name.
On Error Resume Next

 dim szClassName
 dim szClassRoot ' classname minus any MFC-style prefix
 dim szSourceFile
 dim szHeaderFile
 
 ' used to determine full paths for the added 
 ' source/header file
 dim szProjectPath 
 
 dim szTempPath
 dim szHeaderPreprocessor, szHeaderClassBegin
 dim szFirstChar, szSecondChar

 ' make sure the project is valid
 if ActiveProject.Type <> "Build" then
  MsgBox "This project is not valid. Ending macro."
  Exit Sub
 end if

 ' enter the class name
 szClassName = InputBox("Enter the class name:", 
                        "Class Name")
 if (len(szClassName) <= 0) then
  MsgBox "Invalid class name. Ending macro."
  Exit Sub
 end if

 ' generate file names based on the input 
 ' class name
 szSourceFile = szClassName + ".cpp"
 szHeaderFile = szClassName + ".h"

 ' test for MFC style class names [ie: CClassName's 
 ' file should be ClassName.h"] all we do is check the 
 ' first to characters; if they're both capitals, we 
 ' don't use the first one
 szFirstChar = Left(szClassName, 1)
 szSecondChar = Mid(szClassName, 2, 1)
 
 if ( ((Asc(szFirstChar) >= 65) and (Asc(szFirstChar) <= 90)) _
 and ((Asc(szSecondChar) >= 65) and (Asc(szSecondChar) <= 90)) ) _
 then
  szSourceFile = Mid(szSourceFile, 2)
  szHeaderFile = Mid(szHeaderFile, 2)
  szClassRoot = Mid(szClassName, 2)
 else
  szClassRoot = szClassName   
 end if

 ' add the files to the project -- if they can't be added, 
 ' Resume Next will see to it that we skip past this
 ActiveProject.AddFile szSourceFile
 ActiveProject.AddFile szHeaderFile

 ' get the project path
 szProjectPath = GetProjectPath(ActiveProject)

 ' now add the header file to the hard drive
 Documents.Add "Text"

 ActiveDocument.Selection.StartOfDocument

 ActiveDocument.Selection = "#ifndef " & UCase(szClassRoot) _
  & "_H_" & vbCrLf & _
  "#define " & UCase(szClassRoot) & "_H_" & vbCrLf & _
  vbCrLf & _
  "class " & szClassName & vbCrLf & _
  "{" & vbCrLf & _
  "public:        // object creation/destruction" & vbCrLf & _
  "   " & szClassName &  "();" & vbCrLf & _
  "   " & szClassName & "(const " & szClassName & "& source);" _
  & vbCrLf & _
  "   " & szClassName & "& " & "operator=(const " & szClassName _
  & "& right);" & vbCrLf & _
  "   virtual " & "~" & szClassName & "();" & vbCrLf &_
  vbCrLf &_
  "public:        // attribute modification" & vbCrLf & _
  vbCrLf & _
  "protected:     // protected members" & vbCrLf & _
  "   void assign(const " & szClassName & "& source);" _
  & vbCrLf & vbCrLf & _
  "private:       // attributes" & vbCrLf & _
  "};" & vbCrLf & _
  vbCrLf & _
  "#endif"
 
 ActiveDocument.Save szProjectPath & szHeaderFile


 ' now add the source file to the hard drive
 Documents.Add "Text"
 
 ActiveDocument.Selection.StartOfDocument
 
 ActiveDocument.Selection = "#include " & Chr(34) & szHeaderFile _
  & Chr(34) & vbCrLf & vbCrLf & _
  szClassName & "::" & szClassName & "()" & vbCrLf & _
  "{" & vbCrLf & _
  "   // nothing to do yet" & vbCrLf & _
  "}" & vbCrLf & _
  vbCrLf & _
  szClassName & "::" & szClassName & "(const " & szClassName _
  & "& source)" & vbCrLf & _
  "{" & vbCrLf & _
  "   assign(source);" & vbCrLf & _
  "}" & vbCrLf & _
  vbCrLf & _
  szClassName & "& " & szClassName & "::" & "operator=(const " _
  & szClassName & "& right)" & vbCrLf & _
  "{" & vbCrLf & _
  "   if (this != &right)" & vbCrLf & _
  "   {" & vbCrLf & _
  "      assign(right);" & vbCrLf & _
  "   }" & vbCrLf & _
  vbCrLf & _
  "   return (*this);" & vbCrLf & _
  "}" & vbCrLf & _
  vbCrLf & _
  szClassName & "::~" & szClassName & "()" & vbCrLf & _
  "{" & vbCrLf & _
  "   // nothing to do yet" & vbCrLf & _
  "}" & vbCrLf & _
  vbCrLf & _
  "void " & szClassName & "::assign(const " & szClassName _
  & "& source)" & vbCrLf & _
  "{" & vbCrLf & _
  "   // assign all of source's members to this*" & vbCrLf & _
  "}" & vbCrLf

 ActiveDocument.Save szProjectPath & szSourceFile

End Sub


Comments

  • Visual Studio long include definition

    Posted by Legacy on 08/05/2002 12:00am

    Originally posted by: TheRogue

    When Visual Studio creates a class, creates a long definition:

    #define AFX_VIEW2_H__8AD5B616_7E0A_11D2_BE74_990ECC8F5E7E__INCLUDED_

    whereas this macro creates a short one (not a complaint!):

    #define AFX_VIEW2_H_

    does anyone know how Visual Studio comes up with its long definition ?

    Reply
  • Need working macro

    Posted by Legacy on 10/24/2001 12:00am

    Originally posted by: Seema Sisodia

    I am confused how to use this macro. At the end many lines
    I see undescore something like this & _
    which gives error. Does someone have working macro? I would appreciate if you email me the macro.
    I tried cut pasting but doesn't seems to work
    Luv,
    Seema

    Reply
  • Works great !!

    Posted by Legacy on 10/09/2001 12:00am

    Originally posted by: Meetul Kinarivala

    Thanks for a very cool macro :)

    Reply
  • Nice Job !!

    Posted by Legacy on 09/02/2001 12:00am

    Originally posted by: JForce

    yes

    Reply
  • It works nicely !

    Posted by Legacy on 03/20/2001 12:00am

    Originally posted by: Coder

    The script worked well. The article gave a good demo about Devstudio customization using VB scripting.
    Thanks


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • A global data storage provider whose business is booming needed a best-in-class data center to serve as the backbone of its technical operations going forward—and it needed it delivered within a year.

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds