Archive: define function on-demand?


define function on-demand?
I'm just trying to clean up my code a bit. I like to wrap my own functions in an NSH file that I can !include when I need them using macros and return varaibles. (I find this much more convenient than using stack commands within a section or function.)

My list of functions (and NSH files) keeps growing. I'd like to take the ones I use most often and put then into a single NSH file. However, I might not use every single function declared in an NSH file.

I'd like to know if anyone has any tricks of defining a function "on demand". In other words, I want to automatically create a function only when needed. I thought perhaps I could encapsulate the function within "!ifdef" statements, but I'm not sure if that's the right way to go.

Here's an example of a basic NSH file layout that I currently use. Any advice would be appreciated. Thanks!
______________________________________________________
!macro MyFunc Return_var Input_var
push ${Input_var}
Call MyFunc
Pop ${Return_var}
!macroend

!define CoolFunction "!insertmacro MyFunc"

Function MyFunc
Exch $0
push $1
; do stuff with $0
; store result in $1
exch $1
exch
pop $0
FunctionEnd


it would definitely be nicer if one could push instructions to a function in a php-like way.

Call MyFunc ${1} ${2} ${3}


That's what StrFunc/UseFunc, and Instructor's header files do. The best of them would be UseFunc, because function calls and the functions themselves have less instructions than the others.

Just look at the call to StrCase now:


!macro ${_FuncCall} ResultVar String Type
${_UseFunc_FuncCall_Start}
${UseFunc_Echo} `$ {${_Un}${_Name}} "${ResultVar}" "${String}" "${Type}"`
Push `${String}`
Push `${Type}`
Call `${_Un2}${_Name}`
Pop `${ResultVar}`
${_UseFunc_FuncCall_End}
!macroend


This call is suitable for both installer and uninstaller. The echo line is optional.

Or I could use your example function to create one with UseFunc. It would be built like this:


${_UseFunc_DefHeader} `Cool` `NSIS Cool Functions Header File` `1` `0` `` `` `2005 Comperio`

${_UseFunc_DefFunc} `Function` `CoolFunction Function` `1` `0` `` `` `2005 Comperio`

!macro ${_FuncCall} Return_var Input_var
${_UseFunc_FuncCall_Start}
${UseFunc_Echo} `$ {${_Un}${_Name}} "${Return_var}" "${Input_var}"`
Push `${Input_var}`
Call `${_Un2}${_Name}`
Pop `${Return_var}`
${_UseFunc_FuncCall_End}
!macroend

!macro ${_Func}
${_UseFunc_Func_Start}
Exch $0
push $1
; do stuff with $0
; store result in $1
exch $1
exch
pop $0
${_UseFunc_Func_End}
!macroend


I'm just not sure if the version and credits should be necessary. Those are only used when the internal echo message when a function is included, and to create new defines just for the version and the credits.

You see there that the header has the prefix name for all its functions called "Cool". If you have a function called "Function", you would have the final function name as "CoolFunction". Also, the name of the function is used for all its posterior uses: there is no need to put it several times. You can also use the ${_Name} define for this.

The function can be included the same way as you did with StrFunc. ${CoolFunction} outside functions and sections for function definition, ${CoolFunction} "Return_var" "Input_var" inside sections or functions. Any function definition is only added to the size of the installer if you call it first.

I'll release it to public soon as it has a documentation.