This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Macro To Automate MSVC Class Creation
  Submitted by



This is not really a tip but more like a bit of code to help you out speed up the creation of new classes in Visual C++. I did this because I was tired of always typing back the same thing or copy-pasting from a general template I was used to work with. The code is a VBScript macro which can be used in Visual C++ to automate the process of creating a class and adding the files to the active project. Feel free to adapt it to your programming style.

How to use it:
  • 1. select Tools -> Macro...
  • 2. type in a macro name (i.e. CreateClass) and hit EDIT
  • 3. Copy paste the code below


  • Personnally I added the macro on a toolbar ( Tools -> Macro... -> Option >> -> Toolbars ) so that it is only one click away

    Hope it helps, it did for me

    cheers

    Frank



    Sub CreateClass()
    'DESCRIPTION: Create a new class with preformatted .cpp and .h files
     
     dim className
     dim parentClassName
     dim retValue
     dim cppFile
     dim hFile
     
     className = InputBox("Name of the new class:","Create Class")
     
     if className = "" then
      retValue = MsgBox("No class name specified or aborted",48,"Create Class - Error")
      Exit Sub
     end if
     
     'is the new class a derived class?
     retValue = MsgBox("Is the new class a derived class?", 36, "Create Class")
     'if so get the name of the parent class
     if retValue = 6 then 
      parentClassName = InputBox("Name of the parent class:","Create Class")
     else
      parentClassName = ""
     end if
     
     'Create header file
     set hFile = Documents.Add("Text")
     hFile.Language = dsCPP
     
     'format it
     hFile.Selection = "#ifndef " + UCase(className) + "_H"
     hFile.Selection.NewLine
     hFile.Selection = "#define " + UCase(className) + "_H"
     hFile.Selection.NewLine
     hFile.Selection.NewLine
     
     'include parent class header if necessary
     if parentClassName <> "" then 
      hFile.Selection = "#include """ + parentClassName + ".h"""
      hFile.Selection.NewLine
     end if
     
     hFile.Selection.NewLine
     hFile.Selection = "/////////////////////////////////////////////////////////////////////////////////"
     hFile.Selection.NewLine
     hFile.Selection = "//"
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection = UCase(className) + " class declaration"
     hFile.Selection.NewLine
     hFile.Selection = "/////////////////////////////////////////////////////////////////////////////////"
     hFile.Selection.NewLine
     hFile.Selection.NewLine
     hFile.Selection = "class " + className
     
     'make the class a derived class from the parent class if necessary
     if parentClassName <> "" then 
      hFile.Selection = " : public " + parentClassName
     end if
     
     hFile.Selection.NewLine
     hFile.Selection = "{"
     hFile.Selection.NewLine
     hFile.Selection.Backspace
     hFile.Selection = "public:"
     hFile.Selection.NewLine
     hFile.Selection = className + "();"
     hFile.Selection.NewLine
     hFile.Selection = "virtual ~" + className + "();"
     hFile.Selection.NewLine
     hFile.Selection.NewLine
     hFile.Selection.Backspace
     hFile.Selection = "private:"
     hFile.Selection.NewLine
     hFile.Selection.NewLine
     hFile.Selection.Backspace
     hFile.Selection = "};"
     hFile.Selection.NewLine
     hFile.Selection.NewLine
     hFile.Selection = "/////////////////////////////////////////////////////////////////////////////////"
     hFile.Selection.NewLine
     hFile.Selection = "//"
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection.Indent
     hFile.Selection = "Inline functions"
     hFile.Selection.NewLine
     hFile.Selection = "/////////////////////////////////////////////////////////////////////////////////"
     hFile.Selection.NewLine
     hFile.Selection.NewLine
     hFile.Selection = "#endif"
     
     'Save header file
     hFile.Save( className + ".h" )
     
     'Add it to the project
     ActiveProject.AddFile(className + ".h")
     

    'Create cpp file set cppFile = Documents.Add("Text") cppFile.Language = dsCPP 'format it cppFile.Selection = "#include """ + className + ".h""" cppFile.Selection.NewLine cppFile.Selection.NewLine cppFile.Selection.NewLine cppFile.Selection = "/////////////////////////////////////////////////////////////////////////////////" cppFile.Selection.NewLine cppFile.Selection = "//" cppFile.Selection.Indent cppFile.Selection.Indent cppFile.Selection.Indent cppFile.Selection.Indent cppFile.Selection.Indent cppFile.Selection.Indent cppFile.Selection = "Constructors / Destructor" cppFile.Selection.NewLine cppFile.Selection = "/////////////////////////////////////////////////////////////////////////////////" cppFile.Selection.NewLine cppFile.Selection = className + "::" + className + "() {" cppFile.Selection.NewLine cppFile.Selection.NewLine cppFile.Selection.Backspace cppFile.Selection = "}" cppFile.Selection.NewLine cppFile.Selection.NewLine cppFile.Selection = className + "::~" + className + "() {" cppFile.Selection.NewLine cppFile.Selection.NewLine cppFile.Selection.Backspace cppFile.Selection = "}" 'Save header file cppFile.Save( className + ".cpp" ) 'Add it to the project ActiveProject.AddFile(className + ".cpp") End Sub

    The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

     

    Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
    Please read our Terms, Conditions, and Privacy information.