[fpc-devel] Macro Processing

Florian Klaempfl florian at freepascal.org
Mon May 16 08:37:12 CEST 2011


Am 16.05.2011 02:30, schrieb Joerg Schuelke:
> Am Sun, 15 May 2011 13:26:03 +0200 (CEST)
> schrieb Daniƫl Mantione <daniel.mantione at freepascal.org>:
> 
>> Feel free to come up with examples and convince us. They need to be 
>> examples of code that is much more awkward to write without macro's.
> 
> We extend the small enumeration example:
> 
> Think of it, as a server program with a lot of callback procedures and
> associated data
> 
>   procedure cb_proc1();begin ... end;
>   procedure cb_proc2();begin ... end;
>   procedure cb_proc3();begin ... end;
>   procedure cb_proc4();begin ... end;
>   procedure cb_proc5();begin ... end;
>   procedure cb_proc6();begin ... end;
> 
> type
>   callenum=(
>     proc1,
>     proc2,
>     proc3,
>     proc4,
>     proc5,
>     proc6
>   );
> 
>   callproc=procedure();
>   inforec=record
>     id:callenum;name:string[12];address:callproc
>   end;
> 
> const
>   infoarr:array[callenum] of inforec={
>     {id:proc1;name:'proc1';address:@cb_proc1},
>     {id:proc2;name:'proc2';address:@cb_proc2},
>     {id:proc3;name:'proc3';address:@cb_proc3},
>     {id:proc4;name:'proc4';address:@cb_proc4},
>     {id:proc5;name:'proc5';address:@cb_proc5},
>     {id:proc6;name:'proc6';address:@cb_proc6}
>   }
> 
> What I possibly would do is:
> 
> {$Makro entry(n):={id:proc %% %n%;          // concat with parameter
>                    name:'proc' %% % %n%;    // concat with str par
>                    address:@cb_proc %% %n%  // concat with parameter
>                   }
> }
> 
> used with the explicit syntax:
> 
>   infoarr:array[1..6] of inforec={
>     {$Expand entry(1)},
>     {$Expand entry(2)},
>     {$Expand entry(3)},
>     {$Expand entry(4)},
>     {$Expand entry(5)},
>     {$Expand entry(6)},
>   }
> 
> thats nice enough if you have 57 elements in your callenum. Would you
> say then, use an IDE instead of? Every time I change the inforec, which
> is possibly not that seldom, I only change the macro once. Is this not
> nice? Macros simply can help to keep the things together.

You still need to keep infoarr and callenum in sync so "simple" macros
are only half of a solution in this case.



More information about the fpc-devel mailing list