Forum > General

how to translate this from c

<< < (2/3) > >>

Thaddy:

--- Quote from: 440bx on October 07, 2024, 07:23:39 pm ---which isn't a problem in the case of "const" arrays in Pascal but can be a problem for a cdecl function.)

--- End quote ---
If the code is external that does not really matter, you can use varargs interchangeably with array of const parameters, only the call signature changes.(In Delphi this is not possible).
See https://www.freepascal.org/docs-html/ref/refsu97.html#:~:text=14.9.26%20varargs.%20This%20modifier%20can%20only%20be%20usedIt is even documented..  :D 8-)

You can use it with pascal too. Wll write and add short example later.

PascalDragon:

--- Quote from: Key-Real on October 07, 2024, 10:11:20 am ---
--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#define __va_rounded_size(TYPE)  \  (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int)) #define va_start(AP, LASTARG)                                           \ (AP = ((char *)&(LASTARG) + __va_rounded_size(LASTARG)))  #define va_end(AP) AP = (char *)NULL #define va_arg(AP, TYPE)                                                \ (AP = ((char *) (AP)) += __va_rounded_size (TYPE),                     \  *((TYPE *) ((char *) (AP) - __va_rounded_size (TYPE))))  
--- End quote ---

Considering that these macros are for variable arguments (aka “varargs”) handling: it is not considered supported to implement C-style functions that handle varargs. And if you have to declare functions that take varargs then there should normally also be a variant of that C-style function that simply takes variant arguments and then you can simply implement as a function with cdecl; varargs; directives. If there isn't such a function then you're essentially out of luck.

Warfley:
You can handle this with generics:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---generic function __va_rounded_size<T>: SizeInt;inline;begin  Result := ((sizeof(T) + sizeof (cint) - 1) div sizeof (cint)) * sizeof (cint)end; generic procedure va_start<T>(out AP: Pointer; var LastArg: T);inline;begin  AP := Pointer(@LastArg) + specialize __va_rounded_size<T>;end; procedure va_end(out AP: Pointer);inline;begin  AP := nil;end; generic function va_arg<T>(var AP: Pointer):T;inline;type  PT=^T;begin  Result := PT(AP)^;  Inc(AP, specialize __va_rounded_size<T>);end;

MarkMLl:
Thanks for that example @Warfley, useful. Which FPC versions support generics outside classes?

MarkMLl

Warfley:
Generic functions are supported since 3.2 I guess.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version