A macro for exporting functions!

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

A macro for exporting functions!

Post by srod »

Hi,

here are 2 simple macros which I just knocked up to get me out of a hole!

Basically, one of my own user libraries contains a whole bunch of exported functions -as per usual! :) A second library in development needs to use these same functions and for various reasons I need to use the original library as a source code include file rather than simply appealing to the original user library and import the functions etc. Indeed I only ever use source code includes where possible anyhow! :)

No problem you say.

Well, in order to avoid compiler errors when both libraries are installed on a machine, I have to ensure that the previously exported functions being included into the source of the new library are NOT exported again, i.e. they do NOT use ProcedureDLL.

Well, no great deal doing a search and replace to replace the offending ProcedureDLL's with Procedure etc. except that I will effectively be using two versions of the same source with this method and that's not good.

The macros below sort this mess out and without me having to maintain two different versions of the original source etc.

I know that it is unlikely that anyone else will find themselves in this position, but, well if it's useful for someone else then all well and good! :)

***EDIT : my amateur macros have been replaced by Tinman's far more slicker versions. Thanks Tinman!

Code: Select all

;The following two macros allow you to easily switch between creating a user library in 
;which certain procedures must be exported and using your source as an include file in 
;which you probably do not wish to export any procedures.

;Useful if you are building a second user library based on source ADAPTED from an existing user 
;library in which of course you do not want two versions of the same exported function
;in two different user libraries. This would cause compiler errors.

;Thanks to Tinman.

;******Uncomment the following constant in order to export the targeted procedures******.
  ;#MySourceCode_BuildUserLibrary = 1


CompilerIf Defined(MySourceCode_BuildUserLibrary, #PB_Constant) 
  Macro DeclareProc : DeclareDLL : EndMacro 
  Macro Proc : ProcedureDLL : EndMacro 
CompilerElse 
  Macro DeclareProc : Declare : EndMacro 
  Macro Proc : Procedure : EndMacro 
CompilerEndIf 



;============================================TEST 1===================================
;No return from procedure so .l is assumed.

;The procedure 'TestProc(a, b)' will be exported only if the constant 
;#MySourceCode_BuildUserLibrary is defined by uncommenting the line at the top of this file.
;Otherwise, it will remain as an internal function.

  DeclareProc TestProc(a, b)
  Debug TestProc(100, 50)

  Proc TestProc(a ,b)
    ProcedureReturn a+b
  EndProcedure
;=====================================================================================

;============================================TEST 2===================================
;String return.

;The procedure 'StringProc.s(a$, b$)' will be exported only if the constant 
;#MySourceCode_BuildUserLibrary is defined by uncommenting the line at the top of this file.
;Otherwise, it will remain as an internal function.

  DeclareProc.s StringProc(a$, b$)
  Debug StringProc("Hello", "Matey!")

  Proc.s StringProc(a$ ,b$)
    ProcedureReturn a$ + " " + b$
  EndProcedure
;=====================================================================================
If you need to use c style functions then you just need to adjust the macros as appropriate.

Regards.
Last edited by srod on Thu Nov 15, 2007 2:38 pm, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Would it not have been possible to do something like this, a lot like yours but cleaner (IMHO):

Code: Select all

CompilerIf Defined(MySourceCode_BuildUserLibrary, #PB_Constant)
  Macro DeclareImpExpProc : DeclareDLL : EndMacro
  Macro ImpExpProc : ProcedureDLL : EndMacro
CompilerElse
  Macro DeclareImpExpProc : Declare : EndMacro
  Macro ImpExpProc : Procedure : EndMacro
CompilerEndIf 

;#MySourceCode_BuildUserLibrary = 1

DeclareImpExpProc.s StringProc(a$, b$)
Debug StringProc("Hello", "Matey!") 

ImpExpProc.s StringProc(a$ ,b$)
  ProcedureReturn a$ + " " + b$ 
EndProcedure 
More like the original syntax, but I can't test whethe rit works just now.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

That's a damn sight better than mine! :wink:

That's my inexperience with macro's shining through!

I'll remove my code and point people to yours.

Thanks a lot Tinman.
I may look like a mule, but I'm not a complete ass.
Post Reply