www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ / const class pointer signature / unable to find correct D syntax

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
I have a static C++ and can't make it to get a correct binding for one 
function:

DMD: public: unsigned int __cdecl b2d::Context2D::_begin(class 
b2d::Image & __ptr64,class b2d::Context2D::InitParams const * __ptr64 
const) __ptr64
LIB: public: unsigned int __cdecl b2d::Context2D::_begin(class 
b2d::Image & __ptr64,class b2d::Context2D::InitParams const * __ptr64) 
__ptr64

So I somehow get some more const from D. This is the code I used:

    final uint _begin(ref Image image, const(InitParams) initParams);

Which creates a const pointer to a const class signature. But it should 
only be a const pointer. Any idea how to solve this?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
May 04 2018
parent reply Uknown <sireeshkodali1 gmail.com> writes:
On Friday, 4 May 2018 at 07:49:02 UTC, Robert M. Münch wrote:
 I have a static C++ and can't make it to get a correct binding 
 for one function:

 DMD: public: unsigned int __cdecl b2d::Context2D::_begin(class 
 b2d::Image & __ptr64,class b2d::Context2D::InitParams const * 
 __ptr64 const) __ptr64
 LIB: public: unsigned int __cdecl b2d::Context2D::_begin(class 
 b2d::Image & __ptr64,class b2d::Context2D::InitParams const * 
 __ptr64) __ptr64

 So I somehow get some more const from D. This is the code I 
 used:

    final uint _begin(ref Image image, const(InitParams) 
 initParams);

 Which creates a const pointer to a const class signature. But 
 it should only be a const pointer. Any idea how to solve this?
The problem is that const in D is transitive. That means T * const from C++ is not expressible in D. Any reference through a const becomes const. To use it, IMO your best bet is to make a wrapper function on the C++ side like this: unsigned int __cdecl b2d::Context2D::_begin(class b2d::Image & im,class b2d::Context2D::InitParams const * const InitParams) { return //the call to the original c++ function } Alternatively you can use dpp, or dstep or some similar tool to try and let the tool create bindings. As a last ditch, you can force the mangle to match by using pragma(mangle, ...) like this: pragma(mangle, _ZactualMangleFromC++Compiler) final uint _begin(ref Image image, const(InitParams) initParams);
May 04 2018
parent Rubn <where is.this> writes:
On Friday, 4 May 2018 at 07:57:26 UTC, Uknown wrote:
 On Friday, 4 May 2018 at 07:49:02 UTC, Robert M. Münch wrote:
 I have a static C++ and can't make it to get a correct binding 
 for one function:

 DMD: public: unsigned int __cdecl b2d::Context2D::_begin(class 
 b2d::Image & __ptr64,class b2d::Context2D::InitParams const * 
 __ptr64 const) __ptr64
 LIB: public: unsigned int __cdecl b2d::Context2D::_begin(class 
 b2d::Image & __ptr64,class b2d::Context2D::InitParams const * 
 __ptr64) __ptr64

 So I somehow get some more const from D. This is the code I 
 used:

    final uint _begin(ref Image image, const(InitParams) 
 initParams);

 Which creates a const pointer to a const class signature. But 
 it should only be a const pointer. Any idea how to solve this?
The problem is that const in D is transitive. That means T * const from C++ is not expressible in D. Any reference through a const becomes const. To use it, IMO your best bet is to make a wrapper function on the C++ side like this: unsigned int __cdecl b2d::Context2D::_begin(class b2d::Image & im,class b2d::Context2D::InitParams const * const InitParams) { return //the call to the original c++ function } Alternatively you can use dpp, or dstep or some similar tool to try and let the tool create bindings. As a last ditch, you can force the mangle to match by using pragma(mangle, ...) like this: pragma(mangle, _ZactualMangleFromC++Compiler) final uint _begin(ref Image image, const(InitParams) initParams);
Using pragma(mangle) isn't really a solution though, unless you only plan on using one platform/compiler. C++ doesn't have a single standard name mangling. You would need to get the name mangling for every compiler you plan to use then use a version define fore each. Which isn't really practical for how many bindings you would need to do that for. The easiest solution is to just write C bindings for the C++ library then use those instead from D.
May 06 2018