Recent

Author Topic: I found an actual use for MACROS  (Read 558 times)

jamie

  • Hero Member
  • *****
  • Posts: 7852
I found an actual use for MACROS
« on: June 09, 2026, 02:02:42 am »
Been working on a large C++ translation and there are so many circular loop errors in the interface section due to required units I had to set many of the function return definitions in the interface section to TOBJECT, which caused havoc in the implementation section with all the Type Casting that was required, because there I can include the required units to get the TYPES.

I found a use for MACROS  :o
Uses ,,,,, etc

{$MACRO On}
{$DEFINE AShortFunctionName:=TheTrueFunctionClassResults(A_Function_THAT_RETURNS_ONLY_TOBJECT)}


With all of this, I no longer need to type cast all these items in the implementation section.

in the end, it's still the same in source for the compiler but makes your code look a lot nicer and less complex looking.

The only issue I see so far with this is CODE tools does not seem to know how handle this?

Jamie





The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19464
  • Glad to be alive.
Re: I found an actual use for MACROS
« Reply #1 on: June 09, 2026, 06:36:11 am »
Another disadvantage is that macro's are unit local.
Hence I put them in include files.
Any "programmer" that knows only one programming language is not a programmer

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12955
  • FPC developer.
Re: I found an actual use for MACROS
« Reply #2 on: June 09, 2026, 09:05:59 am »
And how is it better than an inline function that does the same?

Zvoni

  • Hero Member
  • *****
  • Posts: 3441
Re: I found an actual use for MACROS
« Reply #3 on: June 09, 2026, 11:41:54 am »
Boy, do i hate macros in C (Never had to translate C++, but the same applies)

If i encountered a macro in C, i usually wrote it as a nested Procedure/Function.
if later on the same macro got called by another function, i moved the nested Proc outside

marcov's argument as inline not withstanding (which is what the c-compiler actually does: inlining the macro at the position of the call)
another approach might be anonymous function at the position of the call

EDIT: *sigh*
I'm an Idiot
My answer above has nothing to do with Jamie's post.....
« Last Edit: June 09, 2026, 11:43:35 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 7852
Re: I found an actual use for MACROS
« Reply #4 on: June 09, 2026, 11:51:07 am »
And how is it better than an inline function that does the same?

Only one line to define

I do use inline if parameters are required.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19464
  • Glad to be alive.
Re: I found an actual use for MACROS
« Reply #5 on: June 09, 2026, 01:07:16 pm »
Yes, you are right: use of macro's as you propose and I do is a fingers and keyboard saver.

For others: those are not parameterized macro's. These are not possible in FPC's macro system and you may easily mis-read what Jamie means. He writes the full parameter specialized for every case..
« Last Edit: June 09, 2026, 01:11:47 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

jamie

  • Hero Member
  • *****
  • Posts: 7852
Re: I found an actual use for MACROS
« Reply #6 on: June 09, 2026, 11:48:04 pm »
I just tried an InLine or non-Inline. can't do it.

I would need to make the prototype in the interface with the resulting type I need but then I need to include the required unit and that in turn ends with a circular reference error.

The Inline method does not have access to the current class instance (SELF) so it cannot see the inner TOBJECT method I am trying to cast simply.

The MACRO stays as it is the only thing that works because it is defined in the IMPLEMENTATION section at the start just after the USES list where I do include the required unit.

Jamie
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12955
  • FPC developer.
Re: I found an actual use for MACROS
« Reply #7 on: June 11, 2026, 11:35:23 am »
In my work job, I use C for embedded work, and there I use macros a lot.

Some reasons are legacy (migrated to a compiler that could do C99/static inline pretty late), some not.  In the past we used 20-33MIPS/16-bit and even less (8-bit, 8MIPS) CPUs, while new prints now are for 32-bit 200MHZ/MIPS parts.

Most macros are basically pin aliases in the board definition, so that firmwares can migrate to later boards which might shuffle pins.

Some others are as aliases for peripherals (which are basically register names), which you could see as a kind of generic. The peripheral register names are created with some ## magic.

If I would start new, I probably would go to a more arduino level with a bit more runtime overhead. But most firmwares are relative small in absolute size (20-30kb) and lines (5000 lines main C file is already much, with maybe some more in libraries)

I do sometimes wonder how I would handle this in Pascal (go with the more arduino like option, or use an extra macro processor?).

I also have about 100kline C++ code. That is not embedded, so I don't really use macros there.

 

TinyPortal © 2005-2018