Auto-generating C header file from C++ header?

B

Bit byte

I am having to provide a C interface to (quite a few) C++ classes.
Is there a way that I can automate this process?

example :

class X
{
public:

X();

void f();
};

will produce this C header :

// C interface

#ifdef __cplusplus
extern "C" {
#endif

// Type-safe, opaque pointer.
typedef struct C_X_* X_Handle;


X_Handle CreateX();
void DestroyX(X_Handle x);

void X_f(X_Handle x);

#ifdef __cplusplus
}
#endif


I can then (manually) implement this:

// C++ implementation

X_Handle CreateX()
{
return reinterpret_cast<X_Handle>(new X);
}

void DestroyX(X_Handle x)
{
delete reinterpret_cast<X*>(x);
}

void X_f(X_Handle x)
{
reinterpret_cast<X*>(x)->f();
}
 
B

benben

Not that I know what can help you, but I'm just wondering how you would
translate private function, template, virtual function etc...

Seems to me a very complicated task :) Good luck!

Regards,
Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top