Using core.reflect to rewrite code

Stefan Koch uplink.coder at googlemail.com
Tue Aug 10 23:51:29 UTC 2021


Good evening everyone,

before going to bed I have been playing with my new project again.

This time I wanted to generate a D wrapper struct.

core.reflect can be used to transform

```D
extern (C)
{
     struct FwdCType;
     void setText(FwdCType* ctx, const char* text);
     void setInt(FwdCType* c, const int value);

     int unrelated_function();

     ubyte* getBlob(const FwdCType* ctx);
}
```

into

```D
struct FwdCTypeWrapper {
     FwdCType* ctx;

     void setText (const(char*) text) {
         setText(ctx, text);
     }

     void setInt (const(int) value) {
         setInt(ctx, value);
     }

     ubyte* getBlob () {
         getBlob(ctx);
     }

}
```
Note that the wrapping generation was smart enough to omit the 
unrelated function because no pointer to the context struct was 
detected in the signature.

The code that does this is just over 80 lines of code (omitting 
the boilerplate).
and it can be seen here:
https://gist.github.com/UplinkCoder/93cb06e4921ab4c96752c6325e03e42d


More information about the Digitalmars-d mailing list