Create D binding for C struct containing templated class

evilrat evilrat666 at gmail.com
Fri Jun 29 01:38:23 UTC 2018


On Thursday, 28 June 2018 at 18:43:11 UTC, Andre Pany wrote:
> Hi,
>
> I try to write a binding for the GRPC core. There is a struct 
> which has some ugly fields:
> (https://github.com/grpc/grpc/blob/master/src/core/lib/surface/call.cc#L229)
>
> struct grpc_call {
>   gpr_refcount ext_ref;
>   gpr_arena* arena;
>   ...
>   
> grpc_core::ManualConstructor<grpc_core::SliceBufferByteStream> 
> sending_stream;
>   grpc_core::OrphanablePtr<grpc_core::ByteStream> 
> receiving_stream;
>
>
> ManualConstructor is defined as:
> https://github.com/grpc/grpc/blob/50cecca24d4826dea4595b514a332fca5783074b/src/core/lib/gprpp/manual_constructor.h#L168
>
> template <typename Type>
> class ManualConstructor {
>  public:
>   Type* get() { return reinterpret_cast<Type*>(&space_); }
>   const Type* get() const { return reinterpret_cast<const 
> Type*>(&space_); }
>
>
> My C / C++ knowledge is very limited and I also do not know how 
> much D matured regarding C / C++ integration. Is it possible to 
> write a binding in D for these constructs or isn't it supported 
> by D?
>
> Kind regards
> André

No, unfortunately D cannot interface with C templates, only C++ 
templates. But just by coincidence this is C++ source code (and 
so the templates).


So it is possible but there is few catches.
- template functions/methods could be inlined by compiler, which 
means you may (or may not) have to implement it in D (either 
copy-paste and adapt, or make own ABI compatible implementation)

- most of the methods in this particular template is purely C++ 
specific convenience helpers, all of this isn't strictly needed 
in D, just for example operator-> could simply be replaced with 
alias this.

- the only data member in this template is the 
std::aligned_storage, which you can probably ignore completely 
and instead just use fixed-size ubyte[] array with align, again 
just make struct with alias this and align its content same way 
as aligned_storage does.


And much more-more other things to keep in mind.


But the most important thing, are you absolutely sure this is a 
part of the public API and not the implementation specific 
internals? Because normally you don't #include anything other 
than .h* files, this is a source file, and it is not even sits in 
the include folder.


More information about the Digitalmars-d-learn mailing list