Page 1 of 1

Multi-Routine through Macro - Convenience mode for the lazy.

Posted: Mon Jan 07, 2008 4:00 am
by superadnim
Ok, So I had a few procedures in my DLL and I was using some of them inside my code, as it turns out, I could save a substantial amount of cycles if I skipped the proc calls all together, within my application - and that's what I did, by defining a macro inside my procedure, this macro had an underscore as a prefix which is a simple way to know that it's the macro version of the code and not the actual proc itself.

Code: Select all

Procedure.d MyProcedure(a,b,c)
	Macro _MyProcedure(a,b,c)
		(a+b+c)*0.333333
	EndMacro
	ProcedureReturn _MyProcedure(a,b,c)
EndProcedure

Debug MyProcedure(10,20,30)
Debug _MyProcedure(10,20,30)
I know it's stupid and simple, but someone might find it as useful (if not more) as I did.

I don't really like doing this, but I think since it's a pre-processor thing I'm dealing with, the code isn't actually as dirty as having the macro outside the procedure (because then it becomes messy to update, etc... at least it happened to me).

This way you can decide whether you want to call as a macro or as a procedure.

The only limitation is with multi-line code... due to the fact that PB macro instructions are quite limited as of V4.1x -still...

Posted: Mon Jan 07, 2008 4:07 am
by pdwyer
whats the difference between putting the macro inside the proc and outside?

I'm guessing that the idea is so that you have one point of update, rather than two for proc and macro but if you put the macro outside the proc and call it from the proc the same way wouldn't it be cleaner?

I don't use macros much myself and I'm not really sure what this achieves.

Thanks

Posted: Mon Jan 07, 2008 4:11 am
by superadnim
It's just so you don't have the macro defined outside the proc, when theres 30 procedures in that source it looks nasty and confusing if not. At least with me :)

I didn't know it was possible to define macros inside procedures...

Posted: Mon Jan 07, 2008 6:54 am
by pdwyer
neither did I... my first thought was "what's the scope then?" ;)

Posted: Mon Jan 07, 2008 2:50 pm
by Trond
Tip: Be very careful with that, or things might break:

Code: Select all

Procedure.l MyProcedure(a,b,c) 
   Macro _MyProcedure(a,b,c) 
      (a+b+c)*33 
   EndMacro 
   ProcedureReturn _MyProcedure(a,b,c) 
EndProcedure 

Debug MyProcedure(10,20,30) | 4096
Debug _MyProcedure(10,20,30) | 4096

Posted: Mon Jan 07, 2008 2:58 pm
by pdwyer
:shock:

Suddenly I'm happy that I've never created a PB macro, that would be the cause of a bug for me that I'd have a lot of trouble finding.

I suppose people who deal with macros a lot know the trips and traps...

Posted: Mon Jan 07, 2008 6:37 pm
by Demivec
pdwyer wrote:I suppose people who deal with macros a lot know the trips and traps...
A trick for the situation demonstrated by Trond is to place parenthesis around the formula returned (via substitution) like this:

Code: Select all

Procedure.l MyProcedure(a,b,c)
   Macro _MyProcedure(a,b,c)
      ((a+b+c)*33)
   EndMacro
   ProcedureReturn _MyProcedure(a,b,c)
EndProcedure

Debug MyProcedure(10,20,30) | 4096
Debug _MyProcedure(10,20,30) | 4096

Posted: Mon Jan 07, 2008 6:41 pm
by #NULL
in such macros always use additional outer braces:

Code: Select all

Macro _MyProcedure(a,b,c)
  ( (a+b+c)*33 )
EndMacro
<edit: better said twice 8) >

Posted: Mon Jan 07, 2008 6:46 pm
by freak
add them around the parameters too, as you can get the same problem there.

Code: Select all

( ((a)+(b)+(c))*33 ) 
now you're on the safe side ;)

Posted: Mon Jan 07, 2008 11:48 pm
by Thalius
this looks rather odd tho and remdinds me a bit of... nevermind ( anyone remember that ascii boobie line .. ? :lol: ) - ok i admit it was a long day and i had my drink :D

Thalius

Posted: Wed Jan 09, 2008 1:40 pm
by Dreamland Fantasy
pdwyer wrote::shock:

Suddenly I'm happy that I've never created a PB macro, that would be the cause of a bug for me that I'd have a lot of trouble finding.

I suppose people who deal with macros a lot know the trips and traps...
I used macros for some of my graphics stuff to significantly speed things up. I ended up having a bug which took me days to track down and it just turned out to be that I had not put brackets around the variables in the macro.

I was doing something along the lines of 2 * y + x within the macro, but if y = a + 1 the macro was returning 2 * a + 1 + x instead of 2 * (a + 1) + x.

Needless to say I've never made that same mistake again! :roll:

Kind regards,

Francis.

Posted: Wed Jan 09, 2008 4:13 pm
by #NULL
if someone is new to macros, also remember that you can get problems with using parameters multiple times:

Code: Select all

Macro twice(param)
  Str(param) + ", " + Str(param)
EndMacro

Debug twice(Random(100))