Write C++ wrappers of C code automatically

Discussion in 'C++' started by Joakim Hove, Apr 14, 2010.

  1. Joakim Hove

    Joakim Hove Guest

    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
     
    Joakim Hove, Apr 14, 2010
    #1
    1. Advertising

  2. Joakim Hove

    Joakim Hove Guest

    > 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
     
    Joakim Hove, Apr 15, 2010
    #2
    1. Advertising

  3. On 15 avr, 09:17, Joakim Hove <> wrote:
    > > Just use the library as is. Wrapping it in C++ code doesn't do much but
    > > change the syntax and that shouldn't matter.


    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).

    --
    Michael
     
    Michael Doubez, Apr 15, 2010
    #3
  4. Joakim Hove

    Öö Tiib Guest

    On Apr 15, 10:17 am, Joakim Hove <> wrote:
    > 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.
     
    Öö Tiib, Apr 15, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DreiJane
    Replies:
    0
    Views:
    332
    DreiJane
    Nov 23, 2009
  2. DreiJane
    Replies:
    0
    Views:
    264
    DreiJane
    Nov 23, 2009
  3. DreiJane
    Replies:
    0
    Views:
    282
    DreiJane
    Nov 23, 2009
  4. DreiJane
    Replies:
    0
    Views:
    238
    DreiJane
    Nov 23, 2009
  5. Joakim Hove

    Write C++ wrappers of C code automatically

    Joakim Hove, Apr 14, 2010, in forum: C Programming
    Replies:
    5
    Views:
    392
    Öö Tiib
    Apr 15, 2010
Loading...

Share This Page