Wrapper function

This is an old revision of this page, as edited by Wykypydya (talk | contribs) at 16:34, 9 June 2010 (Library functions and system calls: Incorrect usage of "system call"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A wrapper function is a function in a computer program whose main purpose is to call a second function[1] with little or no additional computation. This is also known as method delegation. 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 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.[2] 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.

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 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.[3]

Library functions and system calls

Many library functions such as in the C Standard Library act as an interface (abstraction) for system calls. 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.[citation needed]

See also

Adapter pattern

References

  1. ^ Reselman, Bob (1998). Using Visual Basic 6. Que. p. 446. ISBN 078971633X, 9780789716330. {{cite book}}: Check |isbn= value: invalid character (help); Unknown parameter |coauthors= ignored (|author= suggested) (help)
  2. ^ Stevens, Richard (2003). UNIX Network Programming. Addison-Wesley. pp. 5–6, 29. ISBN 0131411551, 9780131411555. {{cite book}}: Check |isbn= value: invalid character (help); Unknown parameter |coauthors= ignored (|author= suggested) (help)
  3. ^ The Java Tutorials