Re: Create D binding for C struct containing templated class

2018-06-28 Thread evilrat via Digitalmars-d-learn

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{}



Re: Create D binding for C struct containing templated class

2018-06-28 Thread Andre Pany via Digitalmars-d-learn

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é


Re: Create D binding for C struct containing templated class

2018-06-28 Thread evilrat via Digitalmars-d-learn

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 
sending_stream;
  grpc_core::OrphanablePtr 
receiving_stream;



ManualConstructor is defined as:
https://github.com/grpc/grpc/blob/50cecca24d4826dea4595b514a332fca5783074b/src/core/lib/gprpp/manual_constructor.h#L168

template 
class ManualConstructor {
 public:
  Type* get() { return reinterpret_cast(&space_); }
  const Type* get() const { return reinterpret_castType*>(&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.


Create D binding for C struct containing templated class

2018-06-28 Thread Andre Pany via Digitalmars-d-learn

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 
sending_stream;
  grpc_core::OrphanablePtr 
receiving_stream;



ManualConstructor is defined as:
https://github.com/grpc/grpc/blob/50cecca24d4826dea4595b514a332fca5783074b/src/core/lib/gprpp/manual_constructor.h#L168

template 
class ManualConstructor {
 public:
  Type* get() { return reinterpret_cast(&space_); }
  const Type* get() const { return reinterpret_castType*>(&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é