C++ Visual studio宏-入门|将定义从.cpp复制到.h

C++ Visual studio宏-入门|将定义从.cpp复制到.h,c++,visual-studio,macros,C++,Visual Studio,Macros,是否可以执行将函数定义复制到声明的宏(,也可能相反)?比如说 Foo::Foo(int aParameter, int bParameter){ // } int Foo::someMethod(char aCharacter) const { return 0; } 从.cpp文件中可以看到: class Foo { Foo(int aParameter, int bParameter); int someMethod(char aCharacter)

是否可以执行将函数定义复制到声明的宏(,也可能相反)?比如说

Foo::Foo(int aParameter, int bParameter){
    //
}

int Foo::someMethod(char aCharacter) const {
    return 0;  
}
从.cpp文件中可以看到:

class Foo {
    Foo(int aParameter, int bParameter);

    int someMethod(char aCharacter) const;
};
在.cpp文件中

此外:


如果有人拥有针对Visual Studio.Net的优秀教程或文档知识(可能也涵盖了上述问题),我可能也会接受这一回答,因为这几乎不可能获得100%的可靠性。必须编写一个C++语言解析器,以便能够准确地从声明中提升类名,并处理诸如默认参数值和缺少参数名等复杂度。在任何语言中编写C++解析器都是困难的,但你会发现VBA缺乏。
退房。他们的网站现在已经关闭了(嗯,哦),无法提供准确的链接。

不,这几乎不可能获得100%的可靠性。必须编写一个C++语言解析器,以便能够准确地从声明中提升类名,并处理诸如默认参数值和缺少参数名等复杂度。在任何语言中编写C++解析器都是困难的,但你会发现VBA缺乏。
退房。他们的网站现在已关闭(嗯,哦),无法提供准确的链接。

它既不美观也不快速,但确实有效。
使用录制宏功能并浏览对象浏览器以获取调用,然后将其拼接在一起。
我的visual basic知识有限,因此对代码的任何反馈都将非常有用。例如,当我试图定位
类{
行时,速度会大大减慢。有什么加速的方法吗

Imports EnvDTE

Public Module Module1
    Function getFileName(ByRef str As String) As String
        If (str = "main.cpp") Then
            Return ""
        Else
            Dim pos As Integer
            pos = InStr(str, ".cpp")
            If (pos = 0) Then
                ' not a .cpp file.'
                Return ""
            Else
                Dim header As String
                header = Left(str, pos - 1)
                Return header
            End If
        End If
        Return ""
    End Function

    Function getParts(ByRef str As String, ByRef returnvalue As String, ByRef classname As String, ByRef identifier As String, ByRef arguments As String) As String
        ' common function looks like:'
        '   <return_value> <classname>::<identifier>([arguments])[{]'
        '                 ^divider    ^colonposition            ^parenthesisEnd'
        '                  ^classnamepos            ^parenthesisStart'
        ''
        ' exceptions: ctor and dtor'
        '   <classname>::[~]<classname>([arguments])[{]'

        Dim colonposition As Integer
        Dim classnameposition As Integer
        Dim divider As Integer
        Dim divider2 As Integer
        Dim parenthesisStart As Integer
        Dim parenthesisEnd As Integer

        colonposition = InStr(str, "::")
        parenthesisStart = InStr(str, "(")
        parenthesisEnd = InStr(str, ")")
        If (colonposition = 0 Or parenthesisStart = 0 Or parenthesisEnd = 0) Then
            Return "Not a function line" ' no colons or parenthesis found? maybe not a function line'
        End If

        divider = InStr(str, " ")

        ' do we have a ctor/dtor?'
        If (divider > colonposition Or divider = 0) Then

            Return "constructor or destructor"
        End If
        ' this might be a function'
        While True
            divider2 = InStr(divider + 1, str, " ")
            If (divider2 > colonposition) Then
                Exit While
            End If
            divider = divider2
        End While
        ' now we have the full return value in 0 -> divider-1'
        returnvalue = Left(str, divider - 1)
        ' and the classname as well'
        classname = Left(Right(str, str.Length - divider), colonposition - divider - 1)
        'indentifier is right after the :: and before the parenthesis'
        identifier = Left(Right(str, str.Length - colonposition - 1), parenthesisStart - colonposition - 2)
        ' and not to mention the arguments between the parenthesis'
        arguments = Left(Right(str, str.Length - parenthesisStart), parenthesisEnd - parenthesisStart - 1)


        Return "Success"
    End Function

    Sub getDefinition()
        Dim sourcefile As String
        Dim filename As String
        Dim header As String
        Dim returnvalue As String
        Dim classname As String
        Dim identifier As String
        Dim arguments As String

        Dim str As String
        Dim line As String
        Dim pos As Integer


        sourcefile = DTE.ActiveDocument.Name
        ' get the filename for the current file (without the extension)'
        filename = getFileName(sourcefile)
        If (filename.Length = 0) Then
            MsgBox("Im not in a .cpp file", , "GetDefinition")
            Return
        End If

        ' get the current line'
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.EndOfLine(True)
        line = DTE.ActiveDocument.Selection.Text()
        ' now interpret the line'
        str = getParts(line, returnvalue, classname, identifier, arguments)
        If (Not str = "Success") Then
            MsgBox(str, , "GetDefinition")
            Exit Sub
        End If
        ' the str should be put into the header file as of:'
        ' class <classname>[:<base>][{]'
        ' [{]'
        '     <--- somewhere here'
        ' }'

        ' attach the header ending'
        header = filename + ".h"
        ' activate the header file'
        DTE.Windows.Item(header).Activate()
        DTE.ActiveDocument.Selection.StartOfDocument()


        While (True)
            DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
            DTE.ActiveDocument.Selection.EndOfLine(True)
            line = DTE.ActiveDocument.Selection.Text()
            pos = InStr(line, classname)
            If (Not pos = 0) Then
                Exit While
            End If
            DTE.ActiveDocument.Selection.LineDown(False, 1)
        End While
        ' found the class definition'
        While (True)
            pos = InStr(line, "{")
            If (Not pos = 0) Then
                Exit While
            End If
            DTE.ActiveDocument.Selection.LineDown(False, 1)
        End While
        DTE.ActiveDocument.Selection.EndOfLine(False)
        DTE.ActiveDocument.Selection.NewLine()

        DTE.ActiveDocument.Selection.Text = returnvalue & " " & identifier & "(" & arguments & ");"


    End Sub
End Module
导入EnvDTE
公共模块模块1
函数getFileName(ByRef str作为字符串)作为字符串
如果(str=“main.cpp”),则
返回“”
其他的
作为整数的Dim pos
pos=仪表(str,.cpp)
如果(位置=0),则
“不是.cpp文件。”
返回“”
其他的
将标题设置为字符串
标题=左侧(str,位置-1)
返回页眉
如果结束
如果结束
返回“”
端函数
函数getParts(ByRef str作为字符串,ByRef returnvalue作为字符串,ByRef classname作为字符串,ByRef标识符作为字符串,ByRef参数作为字符串)作为字符串
'公共函数看起来像:'
“::([arguments])[{]”
“^divider^colonposition^圆括号结束”
“^classnamepos^括号开始”
''
'例外:ctor和dtor'
“::[~]([arguments])[{]”
作为整数的Dim冒号
Dim CLASSNAME位置为整数
作为整数的Dim除法器
作为整数的Dim divider2
小括号开始为整数
小括号结束为整数
colonposition=InStr(str,“:”)
圆括号start=InStr(str,“(”)
圆括号end=InStr(str,“)”)
如果(colonposition=0或圆括号开始=0或圆括号结束=0),则
返回“非函数行”'未找到冒号或括号?可能不是函数行'
如果结束
分隔器=仪表(str,“”)
“我们有ctor/dtor吗?”
如果(分隔器>并置或分隔器=0),则
返回“构造函数或析构函数”
如果结束
'这可能是一个函数'
虽然是真的
除法器2=InStr(除法器+1,str,“”)
如果(divider2>colonposition),则
退出时
如果结束
除法器=除法器2
结束时
'现在我们在0->divider-1中有了完整的返回值'
returnvalue=Left(str,除法器-1)
'还有类名'
classname=Left(右(str,str.Length-divider),colonposition-divider-1)
'标识符正好位于::之后和括号之前'
标识符=左(右(str,str.Length-colonposition-1),圆括号start-colonposition-2)
'更不用说括号之间的参数'
参数=左(右(str,str.Length-圆括号开始),圆括号结束-圆括号开始-1)
返回“成功”
端函数
子定义()
将源文件设置为字符串
将文件名设置为字符串
将标题设置为字符串
将返回值设置为字符串
将类名设置为字符串
作为字符串的Dim标识符
将参数设置为字符串
作为字符串的Dim str
将线变暗为字符串
作为整数的Dim pos
sourcefile=DTE.ActiveDocument.Name
'获取当前文件的文件名(不带扩展名)'
filename=getFileName(sourcefile)
如果(filename.Length=0),则
MsgBox(“Im不在.cpp文件中”,“GetDefinition”)
返回
如果结束
'获取当前行'
DTE.ActiveDocument.Selection.StartOffline(VSStartOfflineOptions.VSStartOfflineOptions第一列)
DTE.ActiveDocument.Selection.EndOfLine(True)
line=DTE.ActiveDocument.Selection.Text()
“现在解释这句话”
str=getParts(行、返回值、类名、标识符、参数)
如果(不是str=“Success”),则
MsgBox(str,“GetDefinition”)
出口接头
如果结束
'str应在以下日期放入头文件:'
'类[:][{]'
' [{]'

“它既不漂亮也不快,但确实有效。
使用录制宏功能并浏览对象浏览器以获取调用,然后将其拼接在一起。
我的visual basic知识有限,因此对代码的任何反馈都将非常有帮助。。例如,我在尝试查找
类{
行时速度会大大减慢。有什么加快速度的建议吗

Imports EnvDTE

Public Module Module1
    Function getFileName(ByRef str As String) As String
        If (str = "main.cpp") Then
            Return ""
        Else
            Dim pos As Integer
            pos = InStr(str, ".cpp")
            If (pos = 0) Then
                ' not a .cpp file.'
                Return ""
            Else
                Dim header As String
                header = Left(str, pos - 1)
                Return header
            End If
        End If
        Return ""
    End Function

    Function getParts(ByRef str As String, ByRef returnvalue As String, ByRef classname As String, ByRef identifier As String, ByRef arguments As String) As String
        ' common function looks like:'
        '   <return_value> <classname>::<identifier>([arguments])[{]'
        '                 ^divider    ^colonposition            ^parenthesisEnd'
        '                  ^classnamepos            ^parenthesisStart'
        ''
        ' exceptions: ctor and dtor'
        '   <classname>::[~]<classname>([arguments])[{]'

        Dim colonposition As Integer
        Dim classnameposition As Integer
        Dim divider As Integer
        Dim divider2 As Integer
        Dim parenthesisStart As Integer
        Dim parenthesisEnd As Integer

        colonposition = InStr(str, "::")
        parenthesisStart = InStr(str, "(")
        parenthesisEnd = InStr(str, ")")
        If (colonposition = 0 Or parenthesisStart = 0 Or parenthesisEnd = 0) Then
            Return "Not a function line" ' no colons or parenthesis found? maybe not a function line'
        End If

        divider = InStr(str, " ")

        ' do we have a ctor/dtor?'
        If (divider > colonposition Or divider = 0) Then

            Return "constructor or destructor"
        End If
        ' this might be a function'
        While True
            divider2 = InStr(divider + 1, str, " ")
            If (divider2 > colonposition) Then
                Exit While
            End If
            divider = divider2
        End While
        ' now we have the full return value in 0 -> divider-1'
        returnvalue = Left(str, divider - 1)
        ' and the classname as well'
        classname = Left(Right(str, str.Length - divider), colonposition - divider - 1)
        'indentifier is right after the :: and before the parenthesis'
        identifier = Left(Right(str, str.Length - colonposition - 1), parenthesisStart - colonposition - 2)
        ' and not to mention the arguments between the parenthesis'
        arguments = Left(Right(str, str.Length - parenthesisStart), parenthesisEnd - parenthesisStart - 1)


        Return "Success"
    End Function

    Sub getDefinition()
        Dim sourcefile As String
        Dim filename As String
        Dim header As String
        Dim returnvalue As String
        Dim classname As String
        Dim identifier As String
        Dim arguments As String

        Dim str As String
        Dim line As String
        Dim pos As Integer


        sourcefile = DTE.ActiveDocument.Name
        ' get the filename for the current file (without the extension)'
        filename = getFileName(sourcefile)
        If (filename.Length = 0) Then
            MsgBox("Im not in a .cpp file", , "GetDefinition")
            Return
        End If

        ' get the current line'
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.EndOfLine(True)
        line = DTE.ActiveDocument.Selection.Text()
        ' now interpret the line'
        str = getParts(line, returnvalue, classname, identifier, arguments)
        If (Not str = "Success") Then
            MsgBox(str, , "GetDefinition")
            Exit Sub
        End If
        ' the str should be put into the header file as of:'
        ' class <classname>[:<base>][{]'
        ' [{]'
        '     <--- somewhere here'
        ' }'

        ' attach the header ending'
        header = filename + ".h"
        ' activate the header file'
        DTE.Windows.Item(header).Activate()
        DTE.ActiveDocument.Selection.StartOfDocument()


        While (True)
            DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
            DTE.ActiveDocument.Selection.EndOfLine(True)
            line = DTE.ActiveDocument.Selection.Text()
            pos = InStr(line, classname)
            If (Not pos = 0) Then
                Exit While
            End If
            DTE.ActiveDocument.Selection.LineDown(False, 1)
        End While
        ' found the class definition'
        While (True)
            pos = InStr(line, "{")
            If (Not pos = 0) Then
                Exit While
            End If
            DTE.ActiveDocument.Selection.LineDown(False, 1)
        End While
        DTE.ActiveDocument.Selection.EndOfLine(False)
        DTE.ActiveDocument.Selection.NewLine()

        DTE.ActiveDocument.Selection.Text = returnvalue & " " & identifier & "(" & arguments & ");"


    End Sub
End Module
导入EnvDTE
公共模块模块1
作用