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();
}
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();
}