Wrapper library
Wrapper library
Main page

Wrapper library

logo
Community Hub0 subscribers
What are your thoughts?
Be the first to start a discussion here.
Be the first to start a discussion here.
Wrapper library

Wrapper libraries (or library wrappers) consist of a thin layer of code (a "shim") which translates a library's existing interface into a compatible interface. This is done for several reasons:

Wrapper libraries can be implemented using the adapter, façade, and to a lesser extent, proxy design patterns.

The specific way in which a wrapper library is implemented is highly specific to the environment it is being written in and the scenarios which it intends to address. This is especially true in the case when cross-language/runtime interoperability is a consideration.

The following provides a general illustration of a common wrapper library implementation over a C POSIX library header <pthread.h> (for POSIX threads, or "pthreads"). In this example, a C++ interface acts as a "wrapper" around a C interface.

In <pthread.h>:

Wrapping <pthread.h> with PosixThread.cppm:

The original C interface can be regarded as error prone, particularly in the case where users of the library forget to unlock an already locked mutex. The new interface effectively utilizes resource acquisition is initialization (RAII) in the new org::posix::ThreadMutex and org::posix::ThreadLock classes to ensure org::posix::ThreadMutexs are eventually unlocked and pthread_mutex_t objects are automatically released.

The above code closely mimics the implementation of boost::scoped_lock and boost::mutex classes from Boost which are part of the Boost.Thread library.

See all
User Avatar
No comments yet.