Converting C/C++ Code to D (#define extern)

Serg Kovrov kovrov at no.spam
Tue Sep 26 02:06:19 PDT 2006


Hi exiquio, you wrote:
> #define PawnListStart(pos,side) PieceListStart(pos,PawnOfColour(side))
> #define KnightListStart(pos,side) PieceListStart(pos,KnightOfColour(side))
> #define BishopListStart(pos,side) PieceListStart(pos,BishopOfColour(side))
> #define RookListStart(pos,side) PieceListStart(pos,RookOfColour(side))
> #define QueenListStart(pos,side) PieceListStart(pos,QueenOfColour(side))
> 
> 
> #define MovePiece(pos,from,to) do {                 \
>     PieceList(pos,to) = PieceList(pos,from);            \
>     PrevPiece(pos,NextPiece(pos,to)) = to;      \
>     NextPiece(pos,PrevPiece(pos,to)) = to;      \
>   } while(0)

It is a good example of C-macros used when inline functions should be. 
Just define functions that do this stuff.

> result_type PawnListStart(pos_type pos, side_type side)
> {
>   return PieceListStart(pos, PawnOfColour(side));
> }
> ...
> void MovePiece(pos_type pos, from_type from, to_type to)
> {
>   do
>   {
>     PieceList(pos, to) = PieceList(pos,from);
>     PrevPiece(pos, NextPiece(pos, to)) = to;
>     NextPiece(pos, PrevPiece(pos, to)) = to;
>   }
>   while(0);
> }

Although later is somewhat suspicious - how loop meant to break?. 
Perhaps there is another macro called as function, that has a break 
statement. You could investigate what exactly it should do and rewrite 
it more clear.

-- 
serg.



More information about the Digitalmars-d-learn mailing list