Hi!
During our port of Gtk+, we found an 2-Byte-Character-Type (GdkWChar, just an Integer) and strings, based on them. Is there any support for these 2-byte characters in GPC?
Does anyone have an idea how to port this macro to GPC?
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
It just returns an integer with the offset of a member of a structure. Example (in Pascal):
--------- Type foo = Record x, y, z : Integer; End; Var result:Integer; Begin result := offsetof(foo, x); (* Result is 0 *) result := offsetof(foo, y); (* Result is 4 *) result := offsetof(foo, z) (* Result is 8 *) End. ---------------
I really don't know how to implement offsetof, because its based on a type-declaration, not an instance of foo.
Eike
Eike Lange wrote:
Hi!
During our port of Gtk+, we found an 2-Byte-Character-Type (GdkWChar, just an Integer) and strings, based on them. Is there any support for these 2-byte characters in GPC?
No. Of course, you can define such a type (Cardinal(16) or something) and implement operations for them (or call the library's ones), but there's not built-in support (yet?).
Does anyone have an idea how to port this macro to GPC?
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
It just returns an integer with the offset of a member of a structure. Example (in Pascal):
Type foo = Record x, y, z : Integer; End; Var result:Integer; Begin result := offsetof(foo, x); (* Result is 0 *) result := offsetof(foo, y); (* Result is 4 *) result := offsetof(foo, z) (* Result is 8 *) End.
I really don't know how to implement offsetof, because its based on a type-declaration, not an instance of foo.
Nicola asked me the same question in private mail recently. Here's a copy of my answer:
: > Uhmm, see: : > : > : /* retrive a structure offset */ : > : #ifdef offsetof : > : #define GTK_STRUCT_OFFSET(struct, field) ((gint) offsetof (struct, field)) : > : #else /* !offsetof */ : > : #define GTK_STRUCT_OFFSET(struct, field) ((gint) ((gchar*) &((struct*) 0)->field)) : > : #endif /* !offsetof */ : > : > Have you got any clues to do this in Pascal?? : : Only a kludgy way. But since the backend supports offsetof, it might : be rather trivial to make it available in GPC. Please talk to Peter. : : program Foo; : : type : MyRec = record : foo, bar : Integer : end; : : var : Dummy : MyRec; : : begin : Writeln (PtrCard (@Dummy.bar) - PtrCard (@Dummy)) : end. : : Or without a dummy variable, quite similar to the 2nd C macro: : : program Foo; : : type : PMyRec = ^MyRec; : MyRec = record : foo, bar : Integer : end; : : begin : Writeln (PtrCard (@PMyRec (0)^.bar)) : end.
Frank