Thread overview
Interfacing with C++
Feb 04, 2018
infinityplusb
Feb 04, 2018
Mike Parker
Feb 04, 2018
Mike Parker
Feb 04, 2018
infinityplusb
Feb 04, 2018
Seb
Feb 04, 2018
rjframe
Feb 04, 2018
Timothee Cour
Feb 05, 2018
Kagamin
Feb 05, 2018
Timothee Cour
Feb 04, 2018
rikki cattermole
February 04, 2018
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
February 04, 2018
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*);
February 04, 2018
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);
February 04, 2018
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
February 04, 2018
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 ...
February 04, 2018
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
February 04, 2018
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.
February 04, 2018
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.
February 05, 2018
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
February 05, 2018
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