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

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

  • When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date