Thread overview
Create D binding for C struct containing templated class
Jun 28, 2018
Andre Pany
Jun 29, 2018
evilrat
Jun 29, 2018
Andre Pany
Jun 29, 2018
evilrat
June 28, 2018
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é


June 29, 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.
June 29, 2018
On Friday, 29 June 2018 at 01:38:23 UTC, evilrat wrote:
> On Thursday, 28 June 2018 at 18:43:11 UTC, Andre Pany wrote:
>> [...]
>
> 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.

Thanks a lot for the great help.
You are right, until now I haven't looked at the include folder, I thought the "surface" folder is the folder with the public api. But it seems also in the include folder, the header files contains references to the grpc_call struct.
https://github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/grpc_types.h#L70

Kind regards
André
June 29, 2018
On Friday, 29 June 2018 at 06:04:10 UTC, Andre Pany wrote:
>
> Thanks a lot for the great help.
> You are right, until now I haven't looked at the include folder, I thought the "surface" folder is the folder with the public api. But it seems also in the include folder, the header files contains references to the grpc_call struct.
> https://github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/grpc_types.h#L70


It just declares new type(like forward declaration), so you should be able to do the same thing, or declare the actual struct with no members:
   struct grpc_call{}