Forum Stats

  • 3,815,264 Users
  • 2,258,987 Discussions
  • 7,893,019 Comments

Discussions

Binding a C library, thin or thick (object-oriented)?

3627710
3627710 Member Posts: 2
edited Feb 1, 2018 2:26PM in Java Programming

I have written a C library with *_init(void) and *_destroy(void) functions. They are to be called once during program lifetime. Yes, I know this is not object-oriented, but these function install signal handlers and thus are inherently global due Unix misdesign not allowing to easily enough chain signals, so that signals are global objects.

Now I am going to write a Java wrapper around this C library.

My question: Should I make an object, whose constructor calls *_init() and finalize() calls *_destroy()?

Or should I make just functional (non-OO) wrappers around these functions?

I think that I should call *_destroy() from finalize block. But with OO interface I can not worry that destroy will be called (anywhere with the lifetime of my application). So with OO it is a little easier.

Is it worth to use OO here?

Answers

  • Unknown
    edited Jan 28, 2018 5:47PM
    I have written a C library with *_init(void) and *_destroy(void)  functions. They are to be called once during program lifetime. 

    Then call init when the program starts and destroy when the program exits.

    Yes, I know this is not object-oriented, but these function install signal handlers and thus are inherently global due Unix misdesign not allowing to easily enough chain signals, so that signals are global objects.Now I am going to write a Java wrapper around this C library.My question: Should I make an object, whose constructor calls *_init() and finalize() calls *_destroy()?Or should I make just functional (non-OO) wrappers around these functions?I think that I should call *_destroy() from finalize block. But with OO interface I can not worry that destroy will be called (anywhere with the lifetime of my application). So with OO it is a little easier.Is it worth to use OO here?

    I fail to see what OO, objects or 'finalize' blocks have to do with anything.

  • 836548
    836548 Member Posts: 286
    edited Feb 1, 2018 9:24AM
    b514d7dc-3e89-45e0-bdb0-e75a565bf33d wrote:I have written a C library with *_init(void) and *_destroy(void) functions. They are to be called once during program lifetime. Yes, I know this is not object-oriented, but these function install signal handlers and thus are inherently global due Unix misdesign not allowing to easily enough chain signals, so that signals are global objects.Now I am going to write a Java wrapper around this C library.My question: Should I make an object, whose constructor calls *_init() and finalize() calls *_destroy()?Or should I make just functional (non-OO) wrappers around these functions?I think that I should call *_destroy() from finalize block. But with OO interface I can not worry that destroy will be called (anywhere with the lifetime of my application). So with OO it is a little easier.Is it worth to use OO here?

    i don't think OO is required here. it would help , if you mention your requirement, and why you think OO will be a value add.

    i nothing specific, then simple function would work.

  • 3627710
    3627710 Member Posts: 2
    edited Feb 1, 2018 11:49AM

    OO would help to surely call the destructor function. Without OO, I need to use try ... finalize block to ensure the *_destroy() function is called.

  • Unknown
    edited Feb 1, 2018 2:26PM
    OO would help to surely call the destructor function. Without OO, I need to use try ... finalize block to ensure the *_destroy() function is called.

    No - you don't - not if what you told us is true.

    They are to be called once during program lifetime.

    If they need to be called once then do what I said:

    Then call init when the program starts and destroy when the program exits.
This discussion has been closed.