• 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] YSI Daily Tips
#19
2021-05-01: y_unique



Most people know about y_hooks, and that if you use it in multiple files you need to include it in every one.? But why?? The reason is that every time you include it, it generates a new unique name for the hooked functions so that two hook OnPlayerConnects in a row don?t have the same name for the compiler.? But how?? y_unique



Including y_hooks after the first time just in turn includes y_unique.? This library exports two symbols - UNIQUE_SYMBOL and UNIQUE_FUNCTION.


  • UNIQUE_SYMBOL is just a number:




Quote:

#include <YSI_Coding\y_unique>

printf("Value: %d", UNIQUE_SYMBOL);

#include <YSI_Coding\y_unique>

printf("Value: %d", UNIQUE_SYMBOL);




That will print two numbers (probably sequential, but that?s not a guarantee).


  • UNIQUE_FUNCTION is a macro that takes a symbol name and injects a number in to it:




Quote:

#include <YSI_Coding\y_unique>

UNIQUE_FUNCTION<My...thFunction>()

{

}



#include <YSI_Coding\y_unique>

UNIQUE_FUNCTION<My...thFunction>()

{

}




That will make two functions with names such as My008thFunction and My009thFunction.? The zeros are by design, and the numbers are injected in to the symbol in place of the given ...s.? The <Function...Name> pattern is used in a few other places in YSI as well, most notably y_groups.? If you?re wondering why it is done this way, the following code won?t work:



Quote:

#include <YSI_Coding\y_unique>

MyUNIQUE_SYMBOLthFunction()

{

}



#include <YSI_Coding\y_unique>

MyUNIQUE_SYMBOLthFunction()

{

}




That will just produce two functions both called exactly MyUNIQUE_SYMBOLthFunction, and the following code is a syntax error:



Quote:

#include <YSI_Coding\y_unique>

My UNIQUE_SYMBOL thFunction()

{

}



#include <YSI_Coding\y_unique>

My UNIQUE_SYMBOL thFunction()

{

}




In C this could be done with the ## operator, but while there are proposals to add it to pawn, they haven?t materialised yet.



How does y_unique generate a new number every time you include it? It?s just a massive massive string of #if UNIQUE_SYMBOL == 4s etc. Its split up in to files by 100s, and within those in to groups of 10 for speed, but there?s really nothing more to it than that. This also means the numbers are limited to as high as the defines go (currently 999).


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: