Wrapper function
Encyclopedia
A wrapper function is a function in a computer program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 whose main purpose is to call a second function with little or no additional computation. This is also known as method delegation
Delegation (programming)
In object-oriented programming, there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement...

. Wrapper functions can be used for a number of purposes.

Adapting class/object interfaces

Wrapper functions can be used to adapt 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
Debugging
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected. Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge...

 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 replaced 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
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 that does not support multiple inheritance
Multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....

 of base classes, wrapper functions can be used to simulate it. Below is an example of part of a class that "inherits" from LinkedList and HashSet.


public class StackSet implements Stack, Set {

private LinkedList stack;
private HashSet set;

public boolean push(Object o) {
if (set.add(o)) then return stack.push(o);
else return false;
}

public Object pop {
Object o = stack.pop;
set.remove(o);
return o;
}

public boolean contains(Object o) {
return set.contains(o);
}

}

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 library.

Library functions and system calls

Many library functions such as in the C Standard Library
C standard library
The C Standard Library is the standard library for the programming language C, as specified in the ANSI C standard.. It was developed at the same time as the C POSIX library, which is basically a superset of it...

 act as an interface (abstraction) for system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

s. For example, "fork" and "execve" are GLIBC functions that call the "fork" and "execve" system calls, respectively. This often leads to incorrect use of the terms "system call" and "syscall" to refer to the library calls that are wrappers for the actual system calls with the same name.

See also

  • Adapter pattern
    Adapter pattern
    In computer programming, the adapter pattern is a design pattern that translates one interface for a class into a compatible interface...

  • Language binding
    Language binding
    In computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...

     wrapper to another language
  • SWIG
    SWIG
    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 languages like C#, Java, Modula-3, Objective Caml, Octave, and Scheme...

    automatic wrapper generator
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK