Content deleted Content added
m Removed Category:Programming constructs (using HotCat) |
m Bot: http → https |
||
(91 intermediate revisions by 60 users not shown) | |||
Line 1:
{{Short description|Function whose job is only to call another subroutine}}
A '''wrapper function''' is a [[function (computing)|function]] in a [[computer program]] whose main purpose is to call a second function<ref>{{cite book |title=Using Visual Basic 6 |last=Reselman |first=Bob |coauthors=Peasley, Richard; Pruchniak, Wayne |year=1998 |publisher=Que |isbn=078971633X, 9780789716330 |page=446 |url=http://books.google.co.uk/books?id=X5ZQAAAAMAAJ&client=firefox-a&pgis=1 }}</ref> with little or no additional computation. This is also known as [[Delegation (programming)|method delegation]]. Wrapper functions can be used for a number of purposes.▼
{{About|programming structure|wrapping in graphics|Wrapping (graphics)}}
{{redirect|Wrapper method|wrapper methods in feature selection|Feature selection#Wrapper method}}
▲A '''wrapper function''' is a [[
==Adapting class/object interfaces==▼
{{Main|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.▼
==
Wrapper functions are a means of [[Delegation (programming)|delegation]] and can be used for a number of purposes.
Wrapper functions can be used to write [[debugging|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.<ref>{{cite book |title=UNIX Network Programming |last=Stevens |first=Richard |coauthors=Fenner, Bill; Rudoff; Andrew M. |year=2003 |publisher=Addison-Wesley |isbn=0131411551, 9780131411555 |pages=5–6,29 |url=http://books.google.co.uk/books?id=ptSC4LpwGA0C&client=firefox-a }}</ref> 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.▼
=== Programming convenience ===▼
==Multiple inheritance==▼
Wrapper functions
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.▼
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===
▲{{Main|Adapter pattern}}
public class StackSet implements Stack, Set {▼
▲Wrapper functions can be used to [[Adapter pattern|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 [[
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 (programming language)|Java]] class that "inherits" from LinkedList and HashSet. See [[Method (computer programming)|method]] for further implementation details.
<syntaxhighlight lang="Java">
@Override
// contains data members and data methods
}▼
</syntaxhighlight>
==Library functions and system calls==▼
Many [[Standard library|library]] functions, such as those in the [[C Standard Library]], act as [[Interface (computing)|interface]]s for [[Abstraction (computer science)|abstract]]ion of [[system call]]s. The ''fork'' and ''execve'' functions in [[glibc]] are examples of this. They call the [[low-level|lower-level]] ''[[Fork (operating system)|fork]]'' and ''[[execve]]'' system calls, respectively.
▲ return o;
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.<ref>{{Cite web|url=https://man7.org/linux/man-pages/man2/syscalls.2.html|title=syscalls(2) - Linux manual page|website=man7.org|access-date=2020-04-25}}</ref><ref>{{Cite web|url=https://www.gnu.org/software/libc/manual/html_node/System-Calls.html|title=System Calls (The GNU C Library)|website=www.gnu.org|access-date=2020-04-25}}</ref>
==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.<ref>{{cite web|url=https://web.cs.wpi.edu/~cs1101/a05/Docs/creating-helpers.html|archive-url=https://web.archive.org/web/20210507033146/https://web.cs.wpi.edu/~cs1101/a05/Docs/creating-helpers.html|archive-date=7 May 2021|title=CS 1101: Helper functions|first=Kathi|last=Fisler|publisher=[[Worcester Polytechnic Institute]]|year=2005|access-date=17 November 2021|url-status=live}}</ref> 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 [[Binary_translation#Dynamic_binary_translation|dynamic binary translation]], in which helper functions of a particular [[Computer architecture|architecture]] are used in translation of [[Machine code|instructions]] from one [[Instruction set architecture|instruction set]] into another.<ref>{{cite conference|title=Helper function inlining in dynamic binary translation|conference= CC: Compiler Construction|publisher=[[Association for Computing Machinery]]|first=Wenwen|last=Wang|doi=10.1145/3446804.3446851|page=107|url=https://dl.acm.org/doi/10.1145/3446804.3446851|___location=New York, United States|isbn=978-1-4503-8325-7|date=3 March 2021|url-access=subscription}}</ref>
▲==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.<ref>[http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html The Java Tutorials]</ref>
▲==Library functions and system calls==
==See also==
*[[
*[[Driver wrapper]]
*[[Adapter pattern]]
*[[Decorator pattern]]
*[[Delegation (programming)]]
*[[Forwarding (object-oriented programming)]]
*[[Language binding]] wrapper to another language
*[[SWIG]] automatic wrapper generator
*[[Nested function]]
*[[Partial application]]
==References==
|