HOME

TheInfoList



OR:

Wrapper libraries (or library wrappers) consist of a thin layer of code (a "
shim Shim may refer to: * Shim (spacer), a thin and often tapered or wedged piece of material ** CPU shim, a spacer for a computer heat sink ** Shim (fencing), a device used in the sport fencing ** Shim (lock pick), a tool used to bypass padlocks * Sh ...
") which translates a
library A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vi ...
'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 (e.g. incompatible data formats) * Enable cross language and/or runtime interoperability Wrapper libraries can be implemented using the
adapter An adapter or adaptor is a device that converts attributes of one electrical device or system to those of an otherwise incompatible device or system. Some modify power or signal attributes, while others merely adapt the physical form of one c ...
,
façade A façade () (also written facade) is generally the front part or exterior of a building. It is a loan word from the French (), which means ' frontage' or ' face'. In architecture, the façade of a building is often the most important aspect ...
, and to a lesser extent,
proxy Proxy may refer to: * Proxy or agent (law), a substitute authorized to act for another entity or a document which authorizes the agent so to act * Proxy (climate), a measured variable used to infer the value of a variable of interest in climate re ...
design patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a forewo ...
.


Structure and implementation

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.


Example

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 ; class Lock ; 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 and classes to ensure s are eventually unlocked and objects are automatically released. The above code closely mimics the implementation of and which are part of th
boost::thread
library.


Driver wrappers

{{Further, Driver wrapper


Cross-language/runtime interoperability

Some wrapper libraries exist to act as a bridge between a client application and a library written using an incompatible technology. For instance, a
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
application may need to execute a
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
. However system calls are typically exposed as C library functions. To resolve this issue Java implements wrapper libraries which make these system calls callable from a Java application. In order to achieve this, languages like Java provide a mechanism called
foreign function interface A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Naming The term comes from the specification for Common Lisp, which explicit ...
that makes this possible. Some examples of these mechanisms include: * Java Native Interface (JNI) * Java Native Access (JNA)
A foreign function library for Python
* Managed Extensions * SWIG (Simplified Wrapper and Interface Generator)


Existing wrapper libraries

Some examples of existing wrapper libraries:
Pthreads for WIN32

OpenGL Bindings for Python

MySQL++

JavaCV


See also

*
Wrapper function A wrapper function is a function (another word for a ''subroutine'') in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation. Wrapper functions are u ...
*
Wrapper pattern In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often ...
*
Glue code Adhesive, also known as glue, cement, mucilage, or paste, is any non-metallic substance applied to one or both surfaces of two separate items that binds them together and resists their separation. The use of adhesives offers certain advant ...
Computer libraries