cpp_binder, a not-yet-useful tool for generating C++ bindings

ZombineDev via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Sep 21 02:45:00 PDT 2015


On Monday, 21 September 2015 at 07:45:48 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 21 September 2015 at 04:22:30 UTC, Paul O'Neil wrote:
>> I hope that this post will spur discussion / decisions / 
>> action binding C++ libraries into D.  I think the language 
>> capabilities (e.g. extern(C++, namespace)) get really far and 
>> that the next big push needs to be on binding real libraries 
>> and tools to help.
>
> How do you deal with overloading of *, &, &&, const *, const& 
> etc?

I guess that the only thing that doesn't map directly to D is &&. 
The others look on the D side like this:
C++      -> D
T*       -> T*
T&       -> ref T
const T* -> const T*
const T& -> const ref T

I think that T&& actually looks like the T& calling convention 
ABI-wise. The difference being that T&& functions are mangled 
differently so that you can overload them. The challenge is to be 
able to select them from the overload set. Perhaps the path of 
least resistance (for now) is to generate a .cpp (not a 
header-only library) containing a wrapper for every function that 
takes T&&. For example:

// C++ wrapper:

// Wrapper for `vector<T>::push_back( T&& value )`

void push_back_from_rvalue(vector<T>& this_, T& to_be_moved)
{
     this_.push_back(std::move(to_be_moved));
}

// D side:

// Let's say we want to use some move-only type
// like std::thread and push it into a std::vector:
auto my_vec = stdcpp.vector.vector!(stdcpp.Thread);

// The case with rvalue temporary:
// my_vec.push_back_from_rvalue(Thread(&theadFunc));
// ^ creates a temporary Thread object
// and moves it into the vector::push_back call stack.
// Implemented like this:
// (invisible to the user:)
Thread __rvalue_tmp = Thread(&theadFunc);
my_vec.push_back_from_rvalue(__tmp);

The case with lvalue whose ownership is transferred:
auto lvalue_thread_obj = Thread(&theadFunc);

// lvalue_thread_obj is passed by mutable reference
my_vec.push_back_from_rvalue(lvalue_thread_obj);
// (invisible to the user:)
lvalue_thread_obj.__cpp_dtor();
// Unsafe to use lvalue_thread_obj from now on...





More information about the Digitalmars-d-announce mailing list