www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Interfacing with C++

reply infinityplusb <brian infinityplusb.com> writes:
Hi all

I'm looking to try and write an interface to C++, but given I'm a 
casual dabbler in D, it's slightly beyond my current ability in 
terms of both C++ and D!

As a leg up, how would one translate something like this from C++ 
to D?

`typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, 
void* userdata );`

 From my basic understanding I assumed something like:

`alias CvCmpFunc = Typedef!(int) ; `

but then I'm stuck as to where the rest of the parameters would 
get passed?
Do I just declare it as a function instead? That wasn't my 
understanding of reading how typedefs worked, however so I'm a 
little confused.

Any help would be appreciated.

Regards
Feb 03
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 4 February 2018 at 07:54:12 UTC, infinityplusb wrote:
 Hi all

 I'm looking to try and write an interface to C++, but given I'm 
 a casual dabbler in D, it's slightly beyond my current ability 
 in terms of both C++ and D!

 As a leg up, how would one translate something like this from 
 C++ to D?

 `typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* 
 b, void* userdata );`

 From my basic understanding I assumed something like:

 `alias CvCmpFunc = Typedef!(int) ; `

 but then I'm stuck as to where the rest of the parameters would 
 get passed?
 Do I just declare it as a function instead? That wasn't my 
 understanding of reading how typedefs worked, however so I'm a 
 little confused.

 Any help would be appreciated.
First, you have to understand how CV_CDECL is defined. This is going to determine the calling convention that functions pointed to by CvCmpFunc will have. Assuming it's defined to cdecl, the standard C calling convention (which I'm guess it is) then your D definition needs to be extern(C). If it's stdcall, then you need extern(Windows). If it's empty, then you need extern(C++). Second, because this is a function pointer you're declaring, you need to use D's function pointer declaration syntax. Third, we don't need to use the Typedef template for this. alias alone is fine. So assuming CV_CDECL is cdecl, this should do it: extern(C) alias CvCmpFunc = int function(const(void)*, const(void)*, void*);
Feb 04
parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 4 February 2018 at 08:17:31 UTC, Mike Parker wrote:

 So assuming CV_CDECL is cdecl, this should do it:

 extern(C) alias CvCmpFunc = int function(const(void)*, 
 const(void)*, void*);
Assuming this is OpenCV, Looking at [1], it's cdecl only on Windows. Empty everywhere else. So since OpenCV is a C++ library these days, I guess it needs to be declared like this: version(Windows) extern(C) alias CvCmpFunc = int function(const(void)*, const(void)*, void*); else extern(C++) alias CvCmpFunc = int function(const(void)*, const(void)*, void*); Though, I'm curious why anyone would want to declare a callback in a C++ program as cdecl only on Windows and use the default C++ convention everywhere else. I suggest you dig into it and make sure that's what's intended. And good luck binding to OpenCV! [1] https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types_c.h#L68
Feb 04
next sibling parent reply infinityplusb <brian infinityplusb.com> writes:
On Sunday, 4 February 2018 at 08:33:20 UTC, Mike Parker wrote:
 On Sunday, 4 February 2018 at 08:17:31 UTC, Mike Parker wrote:

 Assuming this is OpenCV ...
it is, everyone keeps saying writing bindings in D is super easy ... I feel this is a slight simplification. :(
 version(Windows)
     extern(C) alias CvCmpFunc = int function(const(void)*, 
 const(void)*, void*);
 else
     extern(C++) alias CvCmpFunc = int function(const(void)*, 
 const(void)*, void*);
Sounds easy enough.
 Though, I'm curious why anyone would want to declare a callback 
 in a C++ program as cdecl only on Windows and use the default 
 C++ convention everywhere else. I suggest you dig into it and 
 make sure that's what's intended.
 And good luck binding to OpenCV!
Thanks, I feel I'm going to need it ...
Feb 04
parent Seb <seb wilzba.ch> writes:
On Sunday, 4 February 2018 at 10:42:22 UTC, infinityplusb wrote:
 On Sunday, 4 February 2018 at 08:33:20 UTC, Mike Parker wrote:
 [...]
it is, everyone keeps saying writing bindings in D is super easy ... I feel this is a slight simplification. :(
 [...]
Sounds easy enough.
 [...]
 [...]
Thanks, I feel I'm going to need it ...
For C headers, you can use dstep to automatically generate the respective D header file: https://github.com/jacob-carlborg/dstep
Feb 04
prev sibling next sibling parent reply rjframe <dlang ryanjframe.com> writes:
On Sun, 04 Feb 2018 08:33:20 +0000, Mike Parker wrote:

 Though, I'm curious why anyone would want to declare a callback in a C++
 program as cdecl only on Windows and use the default C++
 convention everywhere else. I suggest you dig into it and make sure
 that's what's intended. And good luck binding to OpenCV!
My guess would be that it's to prevent name mangling of functions in a generated DLL and the need for a DEF file.
Feb 04
parent Timothee Cour <thelastmammoth gmail.com> writes:
Calypso (https://github.com/Syniurge/Calypso/) is the most promising
way to interface with C++, it requires 0 bindings and understands all
of C++ (templates etc); there are some caveats/kinks that are being
ironed out (and any help is welcome).


On Sun, Feb 4, 2018 at 4:37 AM, rjframe via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 On Sun, 04 Feb 2018 08:33:20 +0000, Mike Parker wrote:

 Though, I'm curious why anyone would want to declare a callback in a C++
 program as cdecl only on Windows and use the default C++
 convention everywhere else. I suggest you dig into it and make sure
 that's what's intended. And good luck binding to OpenCV!
My guess would be that it's to prevent name mangling of functions in a generated DLL and the need for a DEF file.
Feb 04
prev sibling parent reply Kagamin <spam here.lot> writes:
On Sunday, 4 February 2018 at 08:33:20 UTC, Mike Parker wrote:
 Though, I'm curious why anyone would want to declare a callback 
 in a C++ program as cdecl only on Windows and use the default 
 C++ convention everywhere else. I suggest you dig into it and 
 make sure that's what's intended. And good luck binding to 
 OpenCV!


 [1] 
 https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types_c.h#L68
No, it's C everywhere, see https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types_c.h#L1774
Feb 05
parent Timothee Cour <thelastmammoth gmail.com> writes:
https://github.com/opencv/opencv/issues/6585#issuecomment-221842441
snip:

 "C-API" is not supported and should be removed totally (but we have a lack of
resources to port this legacy C code to C++, so some of this code still exists
right now). Also huge part of fresh OpenCV functionality is missing in "C-API".
There is no plan to fix this in OpenCV directly. http://answers.opencv.org/question/17546/opencv-will-drop-c-api-support-soon/ snip:
 Shervin is right, the C API is not developed for a long time. All the new
stuff has the C++ API, and it is not backported to the C. So, C API becomes
obsolete and causes pain in the neck, since it should be maintained
I recommend trying out Calypso and helping ironing out any bugs you may find. On Mon, Feb 5, 2018 at 3:15 AM, Kagamin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Sunday, 4 February 2018 at 08:33:20 UTC, Mike Parker wrote:
 Though, I'm curious why anyone would want to declare a callback in a C++
 program as cdecl only on Windows and use the default C++ convention
 everywhere else. I suggest you dig into it and make sure that's what's
 intended. And good luck binding to OpenCV!


 [1]
 https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types_c.h#L68
No, it's C everywhere, see https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types_c.h#L1774
Feb 05
prev sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 04/02/2018 7:54 AM, infinityplusb wrote:
 Hi all
 
 I'm looking to try and write an interface to C++, but given I'm a casual 
 dabbler in D, it's slightly beyond my current ability in terms of both 
 C++ and D!
 
 As a leg up, how would one translate something like this from C++ to D?
 
 `typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* 
 userdata );`
 
  From my basic understanding I assumed something like:
 
 `alias CvCmpFunc = Typedef!(int) ; `
 
 but then I'm stuck as to where the rest of the parameters would get passed?
 Do I just declare it as a function instead? That wasn't my understanding 
 of reading how typedefs worked, however so I'm a little confused.
 
 Any help would be appreciated.
 
 Regards
alias CvCmpFunc = extern(C /* I think */) int function(const void* a, const void* b, void* userdata);
Feb 04