On 01/23/2013 08:33 AM, Sarath Kumar wrote:

> Can someone please tell me the right way to pass an opaque object
> between module's functions.

I am assuming that you are interfacing with a C library. That library must have a D binding file. I am assuming that it is your libA.d:

// libA.d
module libA;

extern (C)
{
        struct Opaque;

        Opaque* getObject();

        void doSomething(Opaque *);
}

There is also a library that implements the struct and the functions. Although you would have it in the C library, here is a D module to imitate it:

// libA_impl.d
// These are presumably defined in a C library. Simply imitating it with this
// D module
extern (C)
{
    struct Opaque
    {
        int i;
        double d;
    }

    Opaque* getObject()
    {
        return new Opaque(42, 1.5);
    }

    void doSomething(Opaque *)
    {}
}

Here is the D module that makes use of that library by importing its D binding:

// libB.d
module libB;

import libA;

void doAction(Opaque* o)
{
    doSomething(o);
}

void main()
{
    Opaque* o = getObject();
    doAction(o);
}

Here is how to build the program:

$ dmd libA.d libB.d libA_impl.d -ofmy_prog

Note that normally libA_impl.d would not be included. Instead, the C library would be used. For example, if we are talking about a libmy_c_lib.a:

$ dmd libA.d libB.d -L-lmy_c_lib -ofmy_prog

Yes, with the confusing -L in there. ;)

Ali

Reply via email to