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 used to make writing computer programs easier by abstracting away the details of a subroutine's underlying implementation.
Purpose
Wrapper functions are a means of
delegation
Delegation is the assignment of authority to another person (normally from a manager to a subordinate) to carry out specific activities. It is the process of distributing and entrusting work to another person,Schermerhorn, J., Davidson, P., Poole ...
and can be used for a number of purposes.
Programming convenience
Wrapper functions can be used to make writing computer programs easier. An example of this is the
MouseAdapter
and similar classes in the
Java AWT
The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-interface widget toolkit, preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphica ...
library.
Wrapper functions are useful in the development of applications that use third-party library functions. A wrapper can be written for each of the third party functions and used in the native application. In case the third party functions change or are updated, only the wrappers in the native application need to be modified as opposed to changing all instances of third party functions in the native application.
Adapting class/object interfaces
Wrapper functions can be used to
adapt
ADAPT (formerly American Disabled for Attendant Programs Today) is a United States grassroots disability rights organization with chapters in 30 states and Washington, D.C. They use nonviolent direct action in order to bring about disability just ...
an existing class or object to have a different interface. This is especially useful when using existing library code.
Code testing
Wrapper functions can be used to write
error checking routines for pre-existing system functions without increasing the length of a code by a large amount by repeating the same error check for each call to the function. All calls to the original function can be replaces with calls to the wrapper, allowing the programmer to forget about error checking once the wrapper is written.
A test driver is a kind of wrapper function that exercises a code module, typically calling it repeatedly, with different settings or parameters, in order to rigorously pursue each possible path. It is not deliverable code, but is not throwaway code either, being typically retained for use in
regression testing.
An interface adaptor is a kind of wrapper function that simplifies, tailors, or amplifies the interface to a code module, with the intent of making it more intelligible or relevant to the user. It may rename parameters, combine parameters, set defaults for parameters, and the like.
Multiple inheritance
In a
programming language that does not support
multiple inheritance of base classes, wrapper functions can be used to simulate it. Below is an example of part of a
Java class that "inherits" from LinkedList and HashSet. See
Method for further implementation details.
public class Test implements LinkedList, HashSet
Library functions and system calls
Many
library functions, such as those in the
C Standard Library
The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard.ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it wa ...
, act as
interfaces for
abstraction of
system calls. The ''fork'' and ''execve'' functions in
glibc
The GNU C Library, commonly known as glibc, is the GNU Project's implementation of the C standard library. Despite its name, it now also directly supports C++ (and, indirectly, other programming languages). It was started in the 1980s by ...
are examples of this. They call the
lower-level ''
fork'' and ''
execve
In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process (computing), process, replacing the previous executable. This act is also referred to as an overlay. It is e ...
'' system calls, respectively.
This may lead to incorrectly using the terms "system call" and "syscall" to refer to higher-level library calls rather than the similarly named system calls, which they wrap.
Helper function
A helper function is a function which groups parts of
computation
Computation is any type of arithmetic or non-arithmetic calculation that follows a well-defined model (e.g., an algorithm).
Mechanical or electronic devices (or, historically, people) that perform computations are known as ''computers''. An es ...
by assigning descriptive names and allowing for the reuse of the computations. Although not all wrappers are helper functions, all helper functions are wrappers, and a notable use of helper functions—grouping frequently utilized operations—is in
dynamic binary translation, in which helper functions of a particular
architecture is used in translation of
instructions
Instruction or instructions may refer to:
Computing
* Instruction, one operation of a processor within a computer architecture instruction set
* Computer program, a collection of instructions
Music
* Instruction (band), a 2002 rock band from Ne ...
from one
instruction set
In computer science, an instruction set architecture (ISA), also called computer architecture, is an abstract model of a computer. A device that executes instructions described by that ISA, such as a central processing unit (CPU), is called an ' ...
into another.
See also
*
Wrapper library
*
Driver wrapper
*
Adapter pattern
*
Decorator pattern
*
Delegation (programming)
*
Forwarding (object-oriented programming)
*
Language binding wrapper to another language
*
SWIG
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other language ...
automatic wrapper generator
*
Nested function
*
Partial application
References
{{Reflist
Articles with example Java code
Subroutines