On Thu, 2018-06-21 at 22:25 +0700, Ryan Joseph wrote: > > > I did a search and found only a few hits on the topic. Since I’ve been > giving theoretical examples here’s something more practical a user > attempted. Can you do that without macros? If I had to guess he had some > funky code and just wanted to reduce typing and copy/paste bugs. > > {$define HOFFSET(rec,field) := pointer(@rec.field) - pointer(@rec)}
That funky code is the way some structured elements are defined when using HDF5 file format library. It's pure C optimized for speed and my goal was to adapt it to pascal. Using somesuch in C seems to be quite normal, it has an entry in Wikipedia... In the end I made the macro an inlined pascal function using the method of "cruel casting"[tm]. After preprocessing C code should look similar. <C> #define HOFFSET(S,M) (offsetof(S,M)) // offsetof is a standard define H5Tinsert(s1_tid, "a_name", HOFFSET(s1_t, a), H5T_NATIVE_INT); H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE); </C> <Pascal> function HOFFSETP(rectypevar: pointer; fieldvar: pointer): longint; inline; begin HOFFSETP := longint(fieldvar - rectypevar); end; H5Tinsert(s2_tid, 'c_name', HOFFSETP(@s2[0], @s2[0].c), H5T_NATIVE_DOUBLE); H5Tinsert(s2_tid, 'a_name', HOFFSETP(@s2[0], @s2[0].a), H5T_NATIVE_INT); </Pascal> > type > s1_t = record > a: longint; > b: single; > c: double; > end; > var > s1: s1_t; > BEGIN > s1.a := 12345; > s1.b := 1.000000001; > s1.c := 1.000000002; > > writeln(HOFFSET(s1, a)); > END. > > Regards, > Ryan Joseph > > _______________________________________________ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Marc Santhoff <m.santh...@web.de> _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal