Write C++ wrappers of C code automatically

J

Joakim Hove

Hello,

I have a quite large library of datastructures implemented in C. All
the datastructures are implemented like this:

/***************************************/

//header file:

typedef struct my_struct my_type;

//implementation

struct my_struct {
// fields in the struct
}

my_type * my_alloc( /* input_args */ ) {
// Create and initialize a my_struct instance.
}

void my_free( my_type * instance ) {
// Free the instance along with all it's fields.
}

// And then various set/get/do_whatever functions

/**************************/

I.e. everything is implemented through opaque pointers, and all
manipulations are based on designated functions which take a pointer
instance as the first argument.

Now - I would like to access this functionality from a C++ program as
well, i.e. I would like to create C++ classes which encapsulate the C
functionality. I have done this manually for some "classes" and that
works OK, but I would strongly like to automate the process. I have
considered writing some Python code to do it, but maybe there is a
better way?

Any tips on how to do this?

Joakim
 
J

Joakim Hove

Just use the library as is. Wrapping it in C++ code doesn't do much but
change the syntax and that shouldn't matter.– Skjul sitert tekst –

I realize that; myself I am perfectly using the C code, however there
are some people in my organisation with a love/hate relationship with
C/C++ and I am trying to cater to their "needs". So - it is actually
the syntax change (and some maintainance overhead....) I am after.

Regards

Joakim
 
M

Michael Doubez

It is not totally true, automatic call of alloc/free function is IMO
already an improvement.
– Skjul sitert tekst –

I realize that; myself I am perfectly using the C code, however there
are some people in my organisation with a love/hate relationship with
C/C++ and I am trying to cater to their "needs". So - it is actually
the syntax change (and some maintainance overhead....) I am after.

A script parsing the headers and generating c++ class could get you a
long way (eventually with meta-informations).
 
E

Ersek, Laszlo

It is not totally true, automatic call of alloc/free function is IMO
already an improvement.

Exactly. It's perfectly sensible from C++ programmers already using
exceptions and relying on destructors to expect *all* libraries to conform
to this style. Otherwise they'll have to mix "traditional" error handling
with exceptions, which is worse than any of them alone.

IMO, this hurts most when trying to do UNIX(R) systems programming in
"idiomatic" C++.

("Newsgroups: comp.lang.c++" snipped, because I'm not subscribed to it.)

Cheers,
lacos
 
Ö

Öö Tiib

I realize that; myself I am perfectly using the C code, however there
are some people in my organisation with a love/hate relationship with
C/C++ and I am trying to cater to their "needs". So - it is actually
the syntax change (and some maintainance overhead....) I am after.

All added 'value' is about interface syntax change? Then you are lucky
who is getting paid for doing questionable things. C is fine language
and such interfaces are pretty readable.

Only value that can be added to good C library interface is to enwrap
that my_struct* into proper smart pointer, my_alloc() into factory
function that produces such smart pointer and ... done.

Usually it is C library itself that is refactored into C++ to
introduce polymorphism instead of overly long switch-case chains and
exceptions instead of overly massive return value handling code. These
two things tend to make legacy C hard to maintain.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top