• 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] YSI Daily Tips
#38
2021-05-20: Yet Another Call Improvement



Three days ago introduced using function Name<spec>, and two days ago introduced CALL@Name. These are both methods use to specify what parameters a function expects, but they are used to pass the function as a pointer to another function that already knows what parameters the pointee should take.



Given:



Quote:

Caller(arg1, Func:cb<iii>, arg2)

{

____@.cb(arg1, arg2, arg1 arg2);

}



Callee(a, b, c)

{

}



main()

{

____Caller(4, addressof (Callee<iii>), 5);

}




The specifier is in this code twice - once in the parameters of Caller to say it wants a function that takes three parameters, and once in the addressof to say that Callee is indeed a function that meets the requirements of taking three parameters.? The same basic problem exists if you?re using using or CALL@ as well - repetition of information.



Because of how this all works, you actually can?t stop this information being duplicated, but you can hide it such that users of your function never need to worry about it.? This is normally done with &:



Quote:

Caller(arg1, Func:cb<iii>, arg2)

{

____@.cb(arg1, arg2, arg1 arg2);

}



Callee(a, b, c)

{

}



main()

{

____Caller(4, &Callee, 5);

}




Much cleaner than all other solutions, but how does it work?? That?s not a standard operator.? The answer is that oft-maligned feature - macros:



Quote:

#define Caller(%0,&%1,%2) Caller(%0,addressof(%1<iii>),%2)




Using this macro (placed almost anywhere) will detect when the second parameter (Nth parameter in other calls - just depends when you want the pointer) starts with &.? If it does it replaces that parameter with a typed call to addressof, as was done in earlier examples.? The fact that th function and macro have exactly the same name is important - this allows all the methods to work, not just one.



For Dialog_ShowCallback this would be:



Quote:

#define Dialog_ShowCallback(%0,&%1,%2) Dialog_ShowCallback(%0,addressof(%1<iiiis>),%2)




For BCrypt_CheckInline:



Quote:

#define BCrypt_HashInline(%0,%1,&%2) BCrypt_HashInline(%0,%1,addressof(%2<s>))




For cb_MoveDynamicObject:



Quote:

#define cb_MoveDynamicObject(&%0,%1) cb_MoveDynamicObject(addressof(%0<i>),%1)




The last macro parameter will detect ALL remaining function parameters after the & - you only need to explicitly specify the parameters before &.? Thus this technique works for vararg functions as well.


Messages In This Thread
YSI Daily Tips - by Y_Less - 2021-05-01, 05:25 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 05:40 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 06:59 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 06:59 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 06:59 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:06 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:06 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:06 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:07 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:07 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:08 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:08 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:08 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:09 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:09 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:09 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:09 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:10 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-01, 07:11 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-02, 07:44 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-03, 01:17 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-04, 08:08 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-05, 09:44 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-06, 02:46 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-07, 05:13 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-08, 08:08 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-09, 10:58 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-11, 08:55 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-11, 08:56 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-12, 09:41 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-13, 01:20 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-14, 08:20 PM
RE: YSI Daily Tips - by Y_Less - 2021-05-16, 07:41 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-16, 07:49 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-19, 11:25 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-19, 11:26 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-19, 11:27 AM
RE: YSI Daily Tips - by Y_Less - 2021-05-20, 10:05 AM
RE: YSI Daily Tips - by Y_Less - 2021-06-01, 09:26 AM
RE: YSI Daily Tips - by Y_Less - 2021-06-25, 08:06 PM
RE: YSI Daily Tips - by Y_Less - 2021-11-17, 09:12 PM
RE: YSI Daily Tips - by Y_Less - 2021-11-19, 12:21 AM
RE: YSI Daily Tips - by Y_Less - 2022-12-30, 09:56 PM
RE: YSI Daily Tips - by Y_Less - 2023-01-02, 01:25 AM
RE: YSI Daily Tips - by Y_Less - 2023-01-03, 12:17 AM

Forum Jump: