Define method

One of the very boring things when hacking a little function directly into the Sourcefile is to copy its definition to the Headerfile.

Here is a quick Solution:

Put the Cursor in the Line where the Function starts (for Example : void foo( int x) ). Call the Makro 'DefineMethod'. It searches for the header file, and then for the beginnig of the public, protected and private blocks. After that it ask you in which block your function is defined. If there is no block the macro creates one.

It uses the 'ToggleHandCPP' macro from Ian Southwell (link here) !


'
'MTMacro.dsm - Written by M.Taupitz
'
'------------------------------------------------------------------------------
'FILE DESCRIPTION: copys the definition from the CPP to the header.
'------------------------------------------------------------------------------

Sub DefineMethod()
'DESCRIPTION: Copy the Definition into the Header-File
	strHpt = ActiveDocument.FullName
	if right(strHpt,3) = "CPP" Or right (strHpt,3) = "cpp" Then
		ActiveDocument.Selection.SelectLine
		strText = ActiveDocument.Selection.Text
		if (Instr(strText, "::" ) = 0) Then
			MsgBox("Line not valid !!")
			Exit Sub
		End If

		pos = Instr(strText, "::")
		strName = Right(strText, (Len(strText) - (pos+1)))

		strClass = Left(strText,pos - 1)
		while (instr(strClass, " ") > 0)
			pos = instr(strClass, " ")
			strTyp = strTyp & Left(strClass, pos)
			strClass = Right(strClass, Len(strClass) - (pos) )
		wend

		strName = strTyp & "	" & Left(strName,Len(strName)-2 ) '(without CRLF also -2)
		iCou = InStr(ActiveDocument.Selection.Text,"//")
		if (iCou > 0) Then
			strName = strName & Trim( Left(strName, iCou - 1))
		End If
		while (instr(strName, ")" ) = 0 )
			ActiveDocument.Selection.StartOfLine
			ActiveDocument.Selection.LineDown dsMove
			ActiveDocument.Selection.StartOfLine dsExtend
			ActiveDocument.Selection.EndOfLine dsExtend
			while Left(strName,1) = "	" Or Left(strName,1) = " "
				strName = Right(strName, Len(strName) -1)
			Wend
			iCou = InStr(ActiveDocument.Selection.Text,"//")
			if (iCou = 0)Then
				strName = strName & Trim(ActiveDocument.Selection.Text)
			Else
				strName = strName & Trim( Left(ActiveDocument.Selection.Text, iCou - 1))
			End If

			' kill the Tabs
			tabCou = InStr(strName, vbTab)
			while tabCou > 0
				strName = (Left(strName, tabCou-1)) & (Right(strName, Len(strName)-tabCou ) )
				tabCou = InStr(strName, vbTab)
			Wend 'tabCou
		Wend

		strName = strName & ";" & vbCrLf

		ToggleHandCPP

		ActiveDocument.Selection.SelectAll
		strHead = ActiveDocument.Selection.Text

		if (instr(strHead,strClass)  = 0) Then
			MsgBox(" Can't find class " & strClass & " !!")
			ToggleHandCPP
			Exit Sub
		End If

		pos = instr(strHead,strClass)

		linePublic = 0
		linePrivate = 0
		lineProtected = 0
		ActiveDocument.Selection.EndOfDocument
		lineBottom = ActiveDocument.Selection.CurrentLine

		ActiveDocument.Selection.StartOfDocument
		ActiveDocument.Selection.StartOfLine
		ActiveDocument.Selection.SelectLine
		strLine = ActiveDocument.Selection.Text
		while (instr(strLine, "private:" ) = 0  And ActiveDocument.Selection.CurrentLine <> lineBottom)
			ActiveDocument.Selection.StartOfLine
			ActiveDocument.Selection.LineDown dsMove
			ActiveDocument.Selection.SelectLine
			strLine = ActiveDocument.Selection.Text
		Wend
		if (ActiveDocument.Selection.CurrentLine < lineBottom) Then
			linePrivate = ActiveDocument.Selection.CurrentLine
		else
			linePrivate = 0
		end if

		ActiveDocument.Selection.StartOfDocument
		ActiveDocument.Selection.SelectLine
		strLine = ActiveDocument.Selection.Text
		while (instr(strLine, "protected:" ) = 0 And ActiveDocument.Selection.CurrentLine <> lineBottom)
			ActiveDocument.Selection.StartOfLine
			ActiveDocument.Selection.LineDown dsMove
			ActiveDocument.Selection.SelectLine
			strLine = ActiveDocument.Selection.Text
		Wend
		if (ActiveDocument.Selection.CurrentLine < lineBottom) Then
			lineProtected = ActiveDocument.Selection.CurrentLine
		else
			lineProtected = 0
		end if

		ActiveDocument.Selection.StartOfDocument
		ActiveDocument.Selection.SelectLine
		strLine = ActiveDocument.Selection.Text
		while (instr(strLine, "public:" ) = 0 And ActiveDocument.Selection.CurrentLine <> lineBottom)
			ActiveDocument.Selection.StartOfLine
			ActiveDocument.Selection.LineDown dsMove
			ActiveDocument.Selection.SelectLine
			strLine = ActiveDocument.Selection.Text
		Wend
		if (ActiveDocument.Selection.CurrentLine < lineBottom) Then
			linePublic = ActiveDocument.Selection.CurrentLine
		else
			linePublic = 0
		end if


		ActiveDocument.Selection.StartOfDocument
		ActiveDocument.Selection.SelectLine
		strLine = ActiveDocument.Selection.Text
		while (instr(strLine, "{" ) = 0 And ActiveDocument.Selection.CurrentLine <> lineBottom)
			ActiveDocument.Selection.StartOfLine
			ActiveDocument.Selection.LineDown dsMove
			ActiveDocument.Selection.SelectLine
			strLine = ActiveDocument.Selection.Text
		Wend
		if (ActiveDocument.Selection.CurrentLine < lineBottom) Then
			lineStart = ActiveDocument.Selection.CurrentLine
		else
			lineStart = 0
		end if

		bAnswer = MsgBox("Copy the definition as -public-(YES), -protected-(NO) or -private-(CANCEL) ?", vbYesNoCancel)

		Replace strName, vbCrLf, " "
		Replace strName, vbCr, " "
		Replace strName, vbLf, " "

		If bAnswer = vbYes Then
			if linePublic = 0 Then
				ActiveDocument.Selection.GoToLine lineStart+1, dsSelect
				ActiveDocument.Selection = ActiveDocument.Selection & "public:" & vbCrLf & vbTab & strName
			Else
				ActiveDocument.Selection.GoToLine linePublic+1, dsSelect
				ActiveDocument.Selection = ActiveDocument.Selection & vbTab & strName
			End If
		End If

		If bAnswer = vbNo Then
			if lineProtected = 0 Then
				ActiveDocument.Selection.GoToLine lineStart+1, dsSelect
				ActiveDocument.Selection =  ActiveDocument.Selection & "protected:" & vbCrLf & vbTab & strName
			Else
				ActiveDocument.Selection.GoToLine lineProtected+1, dsSelect
				ActiveDocument.Selection = ActiveDocument.Selection & vbTab & strName
			End If
		End If

		If bAnswer = vbCancel Then
			if linePrivate = 0 Then
				ActiveDocument.Selection.GoToLine lineStart+1, dsSelect
				ActiveDocument.Selection = ActiveDocument.Selection & "private:" & vbCrLf & vbTab & strName
			Else
				ActiveDocument.Selection.GoToLine linePrivate+1, dsSelect
				ActiveDocument.Selection = ActiveDocument.Selection & vbTab & strName
			End If
		End If

		ToggleHandCPP
	End If 'right = "CPP"

End Sub


Comments

  • Cool but i would need it reverse

    Posted by Legacy on 01/29/2002 12:00am

    Originally posted by: poidi

    hi,

    nice stuff!

    on the other hand, i prefer it the other direction.

    first i write the .h file, then i would like to have some macro that writes the prototype and the body of the actual function (or maybe all declarated functions in the .h file)
    into the .cpp file

    thx
    poidi

    Reply
  • good stuff! but ToggleHandCPP link is wrong

    Posted by Legacy on 03/23/2000 12:00am

    Originally posted by: les engelbrecht

    i tracked down the ToggleHandCPP macro to:
    http://www.codeguru.com/devstudio_macros/open_current_header.shtml

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

Top White Papers and Webcasts

  • This ESG study by Mark Peters evaluated a common industry-standard disk VTl deduplication system (with 15:1 reduction ratio) versus a tape library with LTO-5, drives with full nightly backups, over a five-year period.  The scenarios included replicated systems and offsite tape vaults.  In all circumstances, the TCO for VTL with deduplication ranged from about 2 to 4 times more expensive than the LTO-5 tape library TCO. The paper shares recent ESG research and lots more. 

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds