I’m converting a open-source application from C to Delphi, but I’m having problems converting these lines:
#define IS_IN_SEARCH(mb, offset) (mb->searchmask[(offset)/8] & (1<<((offset)%8)))
#define REMOVE_FROM_SEARCH(mb, offset) mb->searchmask[(offset)/8] &= ~(1<<((offset)%8));
The only thing I guess to do is create a function, but don’t know the rest..
I have no idea how to convert it to Delphi.
Can anyone help me?
Thanks
Start by creating two-argument functions. (You know it should be two arguments because the macros have two arguments and the bodies of the macros don’t refer to any other identifiers besides the arguments.
In addition, if you wish to inline the code, meaning, get the compiler to place the generated code declared in the above 2 functions at every call-site, you can add the inline directive to the declarations, like so:
Next, figure out what those types should be. To do that, look at how the arguments and the macros are used.
offsetis probably an integer because the macros use the modulo operator on it, so replaceYwithIntegerin your function declarations.mbshould be, but you have the rest of the code, so you can look at the places where the macros are used. Find the type of the first actual parameter, and then replaceXwith that type. It’s probably a pointer to some structure type, like a record or class.The return type of the first function is whatever the type of the expression is. That’s probably an integer, but based on the name of the function, it’s really a Boolean type. Any non-zero integer is considered true. Replace
ZwithBoolean.The result of the second macro is probably also an integer, but its name and syntax suggest that its result is always ignored, so you can get rid of
Wand change the second function into a procedure.Finally, fill in the bodies of the functions. Convert C syntax to Delphi, and you get this: