GenerateConstructor.refscript

This page describes the GenerateConstructor RefactorScript shipped with VisualGDB.

Overview

This script iterates over all the fields of a class and generates a constructor initializing them.
You can use it by selecting a class in Code Explorer -> Outline, or via Smart Tags in the code editor.

You can easily customize the script by saving it under %LOCALAPPDATA% via the "Save As" button and changing the parts listed below:
1. The 'inline' variable determines whether to an inline constructor vs. a separate declaration and implementation.
2. The MakeArgumentName() function translates field names to constructor argument names.
3. The GetReferenceSymbol() function defines whether an argument should be passed by reference.

Script


[Main, Description("Generate a constructor"), SuggestFor(Record)]
generator GenerateConstructor(Record class, bool inline = true)
{
    if (inline)
        DoGenerateConstructor(class, false, false);
    else
    {
        DoGenerateConstructor(class, true, false);
>    
        DoGenerateConstructor(class, false, true);
    }
}

generator DoGenerateConstructor(Record class,
                                bool declarationOnly,
                                bool qualifyName)
{
    if (qualifyName)
>>$class.QualifiedName::$class.ShortName(
    else
>>$class.ShortName(
    MakeArgList(class.NonStaticFields);
    if (declarationOnly)
    {
>);
        return;
    }

>)
    MakeInitializerList(class.NonStaticFields);
>{
>}
}

function MakeArgumentName(string fieldName)
{
    //Change the line below to generate different names for the constructor arguments.
    return fieldName.StripPrefix("m_").LowercaseFirst();
}

function GetReferenceSymbol(Field field)
{
    //Pass classes and structures (i.e. records) by reference, everything else - by value
    if (field.Type.Resolved.IsPointerOrReference)
        return "";

    if (field.Type.Resolved.IsRecord)
    {
        //Uncomment the line below to pass specific class instances by value
        //if (field.Type.Resolved.Name.Contains("MyClassPassedByValue"))
        //    return "&";

        return "&";
    }

    return "";
}


generator MakeArgList(Array decls)
{
    foreach(decl in decls)
    {
        set name = MakeArgumentName(decl.ShortName);
        set refSymbol = GetReferenceSymbol(decl);
        >>$decl.Type.Literal $refSymbol$name
        if (!decl.IsLast)
            >>, 
    }
}

generator MakeInitializerList(Array decls)
{
    foreach(decl in decls)
    {
        set argName = MakeArgumentName(decl.ShortName);
        if (decl.IsFirst)
>>        :
        else
>>        ,

> $decl.ShortName($argName)

    }
}

Sample Input

class SimpleClass
{
public:
    int IntField;
    void *PointerField;
};

Sample Output

SimpleClass(int intField, void * pointerField)
        : IntField(intField)
        , PointerField(pointerField)
{
}
A detailed reference on RefactorScript syntax and data model is available here.
See this page for a full list of RefactorScripts shipped with VisualGDB.