View Single Post
Old 18th March 2010, 21:41   #1
Zinthose
Senior Member
 
Join Date: May 2009
Posts: 152
Talking Macro params using a variable or stack

Playing around with the macros I found that I can make some pretty powerful stuff before the code is even compiled.

I'm sharing here in hopes someone will find it useful or thought provoking.

Typically when you call a defined macro you are confined to the following convention:
PHP Code:
${MyMacro} $'Some Value' 
What if we could add stack manipulations to it to avoid excessive pops and pushes by dynamically altering the calls. Say, something like this:
PHP Code:
${MyMacros s 
Now the macro will use the stack for those values. We can even mix and match.
PHP Code:
${MyMacro} $0 s
${MyMacro'Some Value' 
Lets say I have want to add an item to a comma separated list multiple times. Using some fancy macro / stack manipulations I came up with this:
PHP Code:
!ifmacrondef _CSV
    
!macro _CSV _ReturnValue _Source _Value
        
!if `${_Source}` != s
            Push 
`${_Source}`
            !if `${
_Value}` == s
                Exch
            
!else
                
Push `${_Value}`
            !endif
        !else if `${
_Value}` != s
            Push 
`${_Value}`
        !endif
        
Call CSV
        
!if ${_ReturnValue} != s
            Pop 
${_ReturnValue}
        !endif
    !
macroend
    
!define CSV `!insertmacro _CSV`
    Function 
CSV
        
;Stack _Value _Source
        Exch 
$; $0 _Source
        Exch    
_Source $0
        Exch 
$; $$0
        
        StrCmp 
'' $1 StrCmpEnd
            StrCpy 
$`$1,$0`
        
StrCmpEnd:
        
        
Pop $1
        Exch 
$0
    FunctionEnd
!endif

Section CSV
    
${CSV"" "Hello"
    
${CSVs s "World"
    
${CSVs s "This"
    
${CSVs s "is"
    
${CSVs s "a"
    
${CSV} $0 s "test"
    
DetailPrint $0
    
    
${CSV} $$"Oh and one more thing!"
    
DetailPrint $0
SectionEnd 
Pretty cool eh?

Here is an example that is a bit simpler:
PHP Code:
!ifmacrondef _Msg
    
!macro _Msg _ReturnValue _Message
        
!if `${_Message}` != s
            Push 
`${_Message}`
        !endif
        
Call Msg
        
!if ${_ReturnValue} != s
            Pop 
${_ReturnValue}
        !endif
    !
macroend
    
!define Msg `!insertmacro _Msg`
    Function 
Msg
        Exch 
$0
        StrCpy 
$'The Message is "$0"'
        
Exch $0
    FunctionEnd
!endif

Section Msg
    
${Msg$R0 "Hello!"
    
DetailPrint $R0
    
    
${Msg"Hello There!"
    
Pop $R1
    DetailPrint $R1
    
    Push 
"Hello World!"
    
${Msgs s
    Pop $R3
    DetailPrint $R3
SectionEnd 
Zinthose is offline   Reply With Quote