Skip to content
⌘ NSIS Forum Archive

Macro overloading

5 posts

ChocJunkie#

Macro overloading

Hey,

Is there a possibility to overload macros like e.g. overloading methods in Java?

I would like to have something like this:
!macro myMacro param1
!macro myMacro param1 param2
!macro myMacro param1 param2 param3
This way I dont have to use "" for the second and/or third parameter.

Thanks 🙂

CJ
Zinthose#
Not that I've found. But, here is a little macro trick I whipped together that simulates it nicely.

!macro _DoStuff _Params
    !searchparse /noerrors `${_Params}` '' _Param1 `|` _Param2 `|` _Param3
    DetailPrint "###### Param Test ####"
    DetailPrint "  P1 = ${_Param1}"
    DetailPrint "  P2 = ${_Param2}"
    DetailPrint "  P3 = ${_Param3}"
    DetailPrint ""
    !undef _Param1
    !undef _Param2
    !undef _Param3
!macroend
!define DoStuff `!insertmacro _DoStuff`
Section Test
    ${DoStuff} "Hello|There|World"
    ${DoStuff} "Hello||World"
    ${DoStuff} "Hello|World"
    ${DoStuff} "Hello World"
    ${DoStuff} "||Hello World"
SectionEnd 
Output is:
###### Param Test ####
P1 = Hello
P2 = There
P3 = World

###### Param Test ####
P1 = Hello
P2 =
P3 = World

###### Param Test ####
P1 = Hello
P2 = World
P3 =

###### Param Test ####
P1 = Hello World
P2 =
P3 =

###### Param Test ####
P1 =
P2 =
P3 = Hello World

Completed
Zinthose#
Evolution of previous post:

OutFile Test.exe
## Macro Helper Macros
    !define MacroParams "!insertmacro _MacroParams"
    !macro _MacroParams _ParamNames _Values
        !searchreplace _TMP `${_ParamNames}` "|" " '|' "
        !searchparse /noerrors `${_Values}` '' ${_TMP}
        !undef _TMP
    !macroend
    
    !define MacroClean "!insertmacro _MacroClean"
    !macro _MacroClean _ParamNames
        !searchparse /ignorecase /noerrors `${_ParamNames}` '' _TMP2 "|" _TMP
        !undef _TMP2
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !if `_TMP` != ""
            !searchparse /ignorecase /noerrors `${_TMP}` '' _TMP2 "|" _TMP
            !undef _TMP2
        !endif
        !undef _TMP
    !macroend
## User Defined MAcro
    !macro _DoStuff _Params
        ${MacroParams} "_Param1|_Param2|_Param3" `${_Params}`
        
        DetailPrint "###### Param Test ######"
        DetailPrint "  P1 = ${_Param1}"
        DetailPrint "  P2 = ${_Param2}"
        DetailPrint "  P3 = ${_Param3}"
        DetailPrint ""
        
        ${MacroClean} "_Param1|_Param2|_Param3"
    !macroend
!define DoStuff `!insertmacro _DoStuff`
## Example
    Section Example
    
        ${DoStuff} "Hello|There|World"
        ${DoStuff} "Hello||World"
        ${DoStuff} "Hello|World"
        ${DoStuff} "Hello World"
        ${DoStuff} "||Hello World"
        
    SectionEnd 
Still more to do but I have to quit for tinkering for awhile.
Zinthose#edited
MightyMacro.nsh

Aight, enough tinkering for tonight.

I turned it into an include header and cleaned it up a bit.

Play around with it and let me know if you have any issues or suggestions.

MightyMacro.nsh
/* EXAMPLE
    !include MightyMacro.nsh
    
    OutFile MightyMacroDemo.exe
    
    ## User Defined Macro Example
        !macro _HelloWorld _Values
            !define _Parameters "_Index|_Param1|_Param2|_Param3"
            ${MacroParams} ${_Parameters} `${_Values}`
            
            DetailPrint "###### Param Test ${_Index} ######"
            DetailPrint "  _Values = ${_Values}"
            DetailPrint "  _Param1 = ${_Param1}"
            DetailPrint "  _Param2 = ${_Param2}"
            DetailPrint "  _Param3 = ${_Param3}"
            DetailPrint ""
            
            ${MacroClean} ${_Parameters}
            !undef _Parameters
        !macroend
        !define HelloWorld `!insertmacro _HelloWorld`
    ## Execution Example
        Section Example
        
            ${HelloWorld} "1|Hello|There|World"
            ${HelloWorld} "2|Hello||World"
            ${HelloWorld} "3|Hello|World"
            ${HelloWorld} "4|Hello World"
            ${HelloWorld} "5|||Hello World"
            
        SectionEnd
*/
!ifndef __MightyMacro__
!define __MightyMacro__
!ifndef MightyMacroDelimiter
    !define MightyMacroDelimiter "|"
!endif
## Macro Parameter Helper
    !define MacroParams "!insertmacro _MacroParams"
    !macro _MacroParams _ParamNames _Values
        !searchreplace _TMP `${_ParamNames}` "${MightyMacroDelimiter}" " '${MightyMacroDelimiter}' "
        !searchparse /noerrors `${_Values}` '' ${_TMP}
        !undef _TMP
    !macroend
## Macro used to undefine trailing parameter names within the _MacroClean Macro
    !macro _MacroClean_
        !if `${_ParamNames}` != ""
            !searchparse /ignorecase /noerrors `${_ParamNames}` '' _TMP "${MightyMacroDelimiter}" _ParamNames
            !ifdef "${_TMP}"
                !undef ${_TMP}
                !undef _TMP
            !endif
        !endif
    !macroend
    
## MacroClean - Use to cleanup the defined parameters temporarily created 
    !define MacroClean "!insertmacro _MacroClean"
    !macro _MacroClean _ParamNames
        ## Macro used to undefine trailing parameter names
            !define _MacroClean_ "!insertmacro _MacroClean_"
            
        ## Undefine Parameter names  [20 parameters "Should" be adiquate
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            ${_MacroClean_}
            
            
            
        ## One last check, then issue a warning if more paramters are found.
            !if `${_ParamNames}` != ""
                !warning "Parrent macro has more than 20 parameters! This may cause define errors!"
            !endif
            
        ## Final Cleanup
            !undef _MacroClean_
            !undef _ParamNames
        
    !macroend
!endif 
Sample Output of Demo
###### Param Test 1 ######
_Values = 1|Hello|There|World
_Param1 = Hello
_Param2 = There
_Param3 = World

###### Param Test 2 ######
_Values = 2|Hello||World
_Param1 = Hello
_Param2 =
_Param3 = World

###### Param Test 3 ######
_Values = 3|Hello|World
_Param1 = Hello
_Param2 = World
_Param3 =

###### Param Test 4 ######
_Values = 4|Hello World
_Param1 = Hello World
_Param2 =
_Param3 =

###### Param Test 5 ######
_Values = 5|||Hello World
_Param1 =
_Param2 =
_Param3 = Hello World

Completed
I also placed this on the Wiki:
ChocJunkie#
Thanks for your great work! 🙂
For now, I've solved my problem differntly, so that I will have a detailed look on your solution later.

Thanks again!! 🙂