Wrapper function explained

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[1] or a system call with little or no additional computation. Wrapper functions simplify writing computer programs by abstracting the details of a subroutine's implementation.

Purpose

Wrapper functions are a means of delegation and can be used for a number of purposes.

Programming convenience

Wrapper functions simplify writing computer programs. For example, the MouseAdapter and similar classes in the Java AWT library demonstrate this.[2] They 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

See main article: Adapter pattern. 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.[3] 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 it 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, act as interfaces for abstraction of system calls. The fork and execve functions in glibc are examples of this. They call the lower-level fork and execve 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.[4] [5]

Helper function

A helper function is a function which groups parts of computation by assigning descriptive names and allowing for the reuse of the computations.[6] 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 are used in translation of instructions from one instruction set into another.[7]

See also

Notes and References

  1. Book: Reselman, Bob . Using Visual Basic 6 . Peasley, Richard . Pruchniak, Wayne . 1998 . Que . 9780789716330 . 446 .
  2. http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html The Java Tutorials
  3. Book: Stevens, Richard . UNIX Network Programming . Fenner, Bill . Rudoff . Andrew M. . 2003 . Addison-Wesley . 9780131411555 . 5–6, 29 .
  4. Web site: syscalls(2) - Linux manual page. man7.org. 2020-04-25.
  5. Web site: System Calls (The GNU C Library). www.gnu.org. 2020-04-25.
  6. Web site: CS 1101: Helper functions. https://web.archive.org/web/20210507033146/https://web.cs.wpi.edu/~cs1101/a05/Docs/creating-helpers.html. 7 May 2021. Kathi. Fisler. Worcester Polytechnic Institute. 2005. 17 November 2021. live.
  7. Helper function inlining in dynamic binary translation. CC: Compiler Construction. Association for Computing Machinery. Wenwen. Wang. 10.1145/3446804.3446851. 107. New York, United States. 978-1-4503-8325-7. 3 March 2021.