Macro to Add New Classes to Visual C++ Projects
Posted
by Paul Wendt
on March 19th, 2001
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:00amOriginally 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 ?
ReplyNeed working macro
Posted by Legacy on 10/24/2001 12:00amOriginally posted by: Seema Sisodia
I am confused how to use this macro. At the end many lines
ReplyI 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
Works great !!
Posted by Legacy on 10/09/2001 12:00amOriginally posted by: Meetul Kinarivala
Thanks for a very cool macro :)
ReplyNice Job !!
Posted by Legacy on 09/02/2001 12:00amOriginally posted by: JForce
yes
ReplyIt works nicely !
Posted by Legacy on 03/20/2001 12:00amOriginally posted by: Coder
The script worked well. The article gave a good demo about Devstudio customization using VB scripting.
Thanks
Reply