Wrapper library: Difference between revisions
Submitting first revision |
No edit summary |
||
Line 1: | Line 1: | ||
In computer programming, a [[Library_(computing)|library]] is a collection of subroutines or classes used to develop software. Libraries expose interfaces which clients of the library use to execute library routines. |
In computer programming, a [[Library_(computing)|library]] is a collection of subroutines or classes used to develop software. Libraries expose interfaces which clients of the library use to execute library routines. |
||
Wrapper libraries consist of a thin layer of code which translates a library's existing interface into a compatible interface. This is done for several reasons: |
'''Wrapper libraries''' consist of a thin layer of code which translates a library's existing interface into a compatible interface. This is done for several reasons: |
||
*To refine a poorly designed or complicated interface. |
*To refine a poorly designed or complicated interface. |
Revision as of 19:58, 22 February 2009
In computer programming, a library is a collection of subroutines or classes used to develop software. Libraries expose interfaces which clients of the library use to execute library routines.
Wrapper libraries consist of a thin layer of code which translates a library's existing interface into a compatible interface. This is done for several reasons:
- To refine a poorly designed or complicated interface.
- Allow code to work together which otherwise cannot.
- Enable cross language and/or run-time interoperability.
Wrapper libraries are implemented using the Adapter, Facade, and to a lesser extent, Proxy design patterns.
Structure / Implementaiton
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 language/run-time interoperability is a consideration.
With that in mind, the following provides a general illustration of a common wrapper library implementation. In this example, a C++ interface acts as a "wrapper" around a C-language interface.
C Interface
int pthread_mutex_init(pthread_mutex_t * mutex , pthread_mutexattr_t * attr);
int pthread_mutex_destroy (pthread_mutex_t * mutex);
int pthread_mutex_lock (pthread_mutex_t * mutex );
int pthread_mutex_unlock (pthread_mutex_t * mutex );
C++ Wrapper
class Mutex
{
public:
pthread_mutex_t mutex;
Mutex() : mutex(0)
{
pthread_mutex_init(&mutex, 0);
}
~Mutex()
{
pthread_mutex_destroy(&mutex);
}
private:
friend class Lock;
void lock()
{
pthread_mutex_lock(&mutex);
}
void unlock()
{
pthread_mutex_unlock(&mutex);
}
};
class Lock
{
Mutex& mutex;
public:
Lock(Mutex& mutex):mutex(mutex){mutex.lock();}
~Lock(Mutex& mutex):mutex(mutex){mutex.unlock();}
};
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 RAII (Resource Acquisition is Initialization) in the new Mutex and Lock classes to ensure Mutexs 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 which are part of the boost::thread library.
Cross Language/Run-time Interoperability
Some wrapper libraries exist to act as a bridge between a client application and a library written using an incompatible technology. Some languages provide methods to facilitate this need:
Java Native Interface
Built-in Modules (Python)
Extending Python with C or C++
Managed Extensions (C#)
Existing Wrapper Libraries
Some examples of existing wrapper libraries: