Wrapper library
   HOME

TheInfoList



OR:

Wrapper libraries (or library wrappers) consist of a thin layer of code (a " shim") which translates a
library A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
'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 co ...
,
façade A façade or facade (; ) is generally the front part or exterior of a building. It is a loanword from the French language, French (), which means "frontage" or "face". In architecture, the façade of a building is often the most important asp ...
, and to a lesser extent, proxy
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 fore ...
.


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 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
resource acquisition is initialization Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented, statically typed programming languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tie ...
(RAII) 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 is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
application may need to execute a
system call In computing, a system call (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, accessing a hard disk drive ...
. 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 or compiled in another one. An FFI is often used in contexts where calls are made into a bin ...
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
* WineD3D


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 sim ...
* Wrapper pattern *
Glue code In computer programming, glue code is code that allows components to interoperate that otherwise are incompatible. The adapter pattern describes glue code as a software design pattern. Glue code describes language bindings or foreign function ...
Computer libraries