Add 'MacroReturn' command for Purebasic macros
Posted: Sat Jun 19, 2021 4:57 am
Hi. I'd like to suggest adding a 'MacroReturn' command to Purebasic macros. The point of this command would be to allow macros to return a value in a similar fashion to the way that a procedure returns a value. This would allow many simple 'GetValue()' procedures used as library interfaces to be replaced by macros for more efficient code.
The premise of the MacroReturn command would be that all the statements in the macro definition that appear before the statement containing the MacroReturn command would be expanded on the line the macro is called on as statements placed before the statement where the macro call appears. All the statements in the macro definition that appear after the statement containing the MacroReturn command would be expanded on the line the macro is called on as statements placed after the statement where the macro call appears. The code in the statement where the MacroReturn command appears (excluding the MacroReturn command itself) would replace the macro identifier where the macro is called.
Here's an example (taken from my threaded mouse library):-
Macro GetLeftMouseButtonClicks()
; Returns the number of times that the left mouse button has been clicked since the last call to this function.
; A click in this case means the button being pressed after it was in the released state, rather than a press followed by a release.
LockMutex( G_pb_mouse_mutex )
MacroReturn G_pb_mouse_button_1_clicks ; The number of button clicks are stored in this global variable.
G_pb_mouse_button_1_clicks = 0
UnLockMutex( G_pb_mouse_mutex )
EndMacro
; Line with macro call unexpanded.
InputEvent3D( G_pb_mouse_x, G_pb_mouse_y, GetLeftMouseButtonClicks(), input_key.s, special_key )
; Line with macro call expanded.
LockMutex( G_pb_mouse_mutex ) : InputEvent3D( G_pb_mouse_x, G_pb_mouse_y, G_pb_mouse_button_1_clicks, input_key.s, special_key ) : G_pb_mouse_button_1_clicks = 0 : UnLockMutex( G_pb_mouse_mutex )
; ---
; This is the procedure that the macro is intended to replace.
Procedure.i GetLeftMouseButtonClicks()
; Returns the number of times that the left mouse button has been clicked since the last call to this function.
; A click in this case means the button being pressed after it was in the released state, rather than a press followed by a release.
LockMutex( G_pb_mouse_mutex )
clicks = G_pb_mouse_button_1_clicks
G_pb_mouse_button_1_clicks = 0
UnlockMutex( G_pb_mouse_mutex )
ProcedureReturn clicks
EndProcedure
The premise of the MacroReturn command would be that all the statements in the macro definition that appear before the statement containing the MacroReturn command would be expanded on the line the macro is called on as statements placed before the statement where the macro call appears. All the statements in the macro definition that appear after the statement containing the MacroReturn command would be expanded on the line the macro is called on as statements placed after the statement where the macro call appears. The code in the statement where the MacroReturn command appears (excluding the MacroReturn command itself) would replace the macro identifier where the macro is called.
Here's an example (taken from my threaded mouse library):-
Macro GetLeftMouseButtonClicks()
; Returns the number of times that the left mouse button has been clicked since the last call to this function.
; A click in this case means the button being pressed after it was in the released state, rather than a press followed by a release.
LockMutex( G_pb_mouse_mutex )
MacroReturn G_pb_mouse_button_1_clicks ; The number of button clicks are stored in this global variable.
G_pb_mouse_button_1_clicks = 0
UnLockMutex( G_pb_mouse_mutex )
EndMacro
; Line with macro call unexpanded.
InputEvent3D( G_pb_mouse_x, G_pb_mouse_y, GetLeftMouseButtonClicks(), input_key.s, special_key )
; Line with macro call expanded.
LockMutex( G_pb_mouse_mutex ) : InputEvent3D( G_pb_mouse_x, G_pb_mouse_y, G_pb_mouse_button_1_clicks, input_key.s, special_key ) : G_pb_mouse_button_1_clicks = 0 : UnLockMutex( G_pb_mouse_mutex )
; ---
; This is the procedure that the macro is intended to replace.
Procedure.i GetLeftMouseButtonClicks()
; Returns the number of times that the left mouse button has been clicked since the last call to this function.
; A click in this case means the button being pressed after it was in the released state, rather than a press followed by a release.
LockMutex( G_pb_mouse_mutex )
clicks = G_pb_mouse_button_1_clicks
G_pb_mouse_button_1_clicks = 0
UnlockMutex( G_pb_mouse_mutex )
ProcedureReturn clicks
EndProcedure