Important alert: (current site time 7/15/2013 5:38:07 PM EDT)
 

article

Creating Accessors and Mutators using VC++ macros

Email
Submitted on: 3/19/2001 4:06:00 AM
By: Vitaly 
Level: Beginner
User Rating: Unrated
Compatibility: Microsoft Visual C++
Views: 8137
author picture
(About the author)
 
     This will help you to create mutators and accessors to private members of a class.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

Introduction

Insert the following code to your macro dsm file and run the AccessorMutator macro to use it.
Function FileType (ByVal doc)
	ext = doc.Name
	FileType = 0
	pos = Instr(ext, ".")
	if pos > 0 then
		Do While pos <> 1
			ext = Mid(ext, pos, Len(ext) - pos + 1)
			pos = Instr(ext, ".")
		Loop
		ext = LCase(ext)
	end if
	If ext = ".rc" Or ext = ".rc2" Then
		FileType = 4
	ElseIf ext = ".h" Then
		FileType = 1
	ElseIf ext = ".cpp" Then
		FileType = 2
	ElseIf doc.Language = dsJava Then
		FileType = 3
	ElseIf doc.Language = dsIDL Then
		FileType = 4
	ElseIf doc.Language = dsHTML_IE3 Or doc.Language = dsHTML_RFC1866 Then
		FileType = 5 
	ElseIf doc.Language = dsVBSMacro Then ' 
		FileType = 6 
	ElseIf ext = ".def" Then
		FileType = 7
	Else 
		FileType = 0
	End If
End Function
Function GetClassName()
'----------------------------------------
'DESCRIPTION: Gets the name of the class.
'Made by Vitaly Belman
'----------------------------------------
	set Doc = ActiveDocument.Selection
	if Doc.FindText("class \:w$\~;", dsMatchRegExp) = False then 'Regular classes
		Doc.FindText("class \:w\:b+"), dsMatchRegExp 'Derived classes
	else
		Doc.CharRight
	end if
	
	Doc.CharRight
	Doc.CharLeft
	Doc.WordLeft DsExtend
	GetClassName = Doc
End Function
Sub Jump()
'DESCRIPTION: toggles between CPP and H files in both directions with QuickFind 
'I used Michael Pelts' code for it.
on error resume next
	Dim myDocument
	Dim a
	Dim b
	Dim Flag
	Dim Flag1
	lookFor = ActiveDocument.Selection
	Flag1 = 0
	Flag = 1
	a = ActiveDocument.FullName
	tmp = InStr(a, ".cpp")
	If tmp Then
			b = Left(a, Len(a) - 3) + "h"
			Flag1 = 1
	Else
	tmp = InStr(a, ".h")
	If tmp Then
			b = Left(a, Len(a) - 1) + "cpp"
			Flag1 = 1
	End If
	End If
fileName = LCase(b)'first try lower case
Documents.Open fileName
if Err.Number <> 0 then 
Err.Clear
fileName = UCase(fileName)'try upper case
Documents.Open fileName
if Err.Number <> 0 then 
MsgBox Err.Description, 0 , "Error"
exit sub
End If
End If
if Len(lookFor) > 0 then
curLine = ActiveDocument.Selection.CurrentLine
curCol = ActiveDocument.Selection.CurrentColumn
lookForDef = "::" + lookFor
ActiveDocument.Selection.FindText lookForDef, dsMatchCase + dsMatchWord
if ActiveDocument.Selection = lookForDef then
curLine = ActiveDocument.Selection.CurrentLine
curCol = ActiveDocument.Selection.CurrentColumn - Len(ActiveDocument.Selection)
End if
ActiveDocument.Selection.Cancel
ActiveDocument.Selection.MoveTo curLine, curCol
ActiveDocument.Selection.FindText lookFor, dsMatchCase + dsMatchWord
End if
End Sub
Sub AccessorMutator
'----------------------------------------
'DESCRIPTION: Creates the Acc/Mut text.
'Made by Vitaly Belman
'----------------------------------------
	if FileType(ActiveDocument) <> 1 then msgbox("You can use this macro only when header file is the active ActiveDocument.Selectionument."):Exit Sub
	VarName = InputBox("Enter variable name.")
	VarKind = InputBox("Enter variable type.")
	ClassName = GetClassName()
	ActiveDocument.Selection.SelectAll
	ActiveDocument.Selection.LineDown
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection = VarKind & " " & VarName & "(); //Accessor"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection = "void Set" & VarName & "(" & VarKind & " " & VarName & "); //Mutator"
	Jump()
	ActiveDocument.Selection.SelectAll
	ActiveDocument.Selection.LineDown
	ActiveDocument.Selection.NewLine
	
	ActiveDocument.Selection = VarKind & " " & ClassName & "::" & VarName & "() //Accessor"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection = "{"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection.Indent
	ActiveDocument.Selection = "return m_" & VarName & ";"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection.BackSpace
	ActiveDocument.Selection = "}"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection.NewLine
	
	ActiveDocument.Selection = "void " & " " & ClassName & "::" & "Set" & VarName & "(" & VarKind & " " & VarName & "); //Mutator"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection = "{"
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection.Indent
	ActiveDocument.Selection = "m_" & VarName & " = " & VarName & ";" 
	ActiveDocument.Selection.NewLine
	ActiveDocument.Selection.BackSpace
	ActiveDocument.Selection = "}"
End Sub

Please notice that I am using the Default Indent option in Visual C++ and not the Smart Indent like many people do, thus it might cause some weird indents. I suggest switching to Default Indent or removing the unneeded indents in the code.

Vitaly Belman


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.