Search code examples
c++craii

How should I wrap C libraries into C++


Typical C libraries look like this:

foo_t * foo_open();
int     foo_query( foo_t * );
int     foo_close( foo_t * );

I can see two ways of wrapping those into sleak RAII structures. I could either create a class and wrap every C function:

class foo
{
public:
   foo(): m_impl( foo_open() ) { }
  ~foo() noexcept { foo_close( m_impl ); }
   int query() { return foo_query( m_impl ) };
};

Or I could use a smart pointer with a custom destructor:

class foo_destructor
{ 
public:
    void operator()( foo_t * const obj ) noexcept
    {
        foo_close( obj ); 
    } 
};

typedef std::unique_ptr< foo_t, foo_destructor > foo_ptr;

and use the C interface directly.

int main()
{
    foo_ptr my_foo( foo_open() );
    foo_query( my_foo.get() );
}

Right now, I am using the second solution, but mostly because I am too lazy to write all the member functions. Is there a reason why one method should really be preferred to the other?


Solution

  • The first approach is the more C++ way of doing things. All the functions are grouped together into one logical unit (the class), you've encapsulated the data so that RAII prevents resource leaks and you've managed to drop the foo_ prefix, too!