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

Share your advanced PureBasic knowledge/code with the community.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

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

Post 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...
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post 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...
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

neither did I... my first thought was "what's the scope then?" ;)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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...
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post 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) >
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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 ;)
quidquid Latine dictum sit altum videtur
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post 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
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Post 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.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post 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))
Post Reply