---
name: "fnwrap"
section: "3bobcat"
platform: "linux"
package: "libbobcat-dev"
version: "2.08.01-1"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/fnwrap"
---

# fnwrap(3bobcat)

Generic configurable context function wrapper class

## NAME

FnWrap - Generic configurable context function wrapper class

## SYNOPSIS

**#include \<bobcat/fnwrap\>**\

## DESCRIPTION

The **FBB::FnWrap** class contains two static members: *unary* and *binary*. The *unary* function returns a unary functor that is ordinarily called from generic algorithms of the standard template libray expecting a unary functor or predicate.  The *binary* function returns a binary functor that is ordinarily called from generic algorithms of the standard template libray expecting a binary functor or predicate.

The *unary* and *binary* functions expect the name of a (static or free) function that will be called from the functor’s function operator. The arguments received by the functor’s function operator are forwarded to the static or free function that is called by the functor’s function operator.

Any additional arguments that are passed to *unary* or *binary* are forwarded to the function that is called by the functor’s function operator. This allows users of *FnWrap* to pass a *local context* to the function that is indirectly called by a generic algorithm.

The number and types of arguments are determined by the parameter list of the function that is called by the functor’s function operator. If that former  function, in addition to parameters matching the types of the arguments provided by the generic algorithm also defines, e.g., an *int* and *std::string &* parameter then the *FnWrap* member functions must be called with the address of the function to call, with an *int* argument and with a *std::string* *lvalue*.

The type of the return value of the function whose address is passed to the *FnWrap* members will be the return type of the functor’s function call operator. So if the generic algorithm expects a predicate function the function called by the functor’s function call operator should return a *bool* value.

The called function must be a static member or free function. Using a static member or free function has several advantages over calling a non-static  class member function:

- o

  No object for which the member function will be called needs to be provided;

- o

  There is no introduced uncertainty about the **const**-ness of the callled function, as static member functions and free functions do not support **const** modifiers;

- o

  The called function can be a static member function of the class using the generic algorithm to which the **FnWrap** object is passed. By specifying the calling object as one of the arguments of the *FnWrap* function, the called function will receive this object as well and may directly access the calling object’s members;

- o

  The passed function can be a static member function of the class whose objects are passed to the function via the generic template function’s iterator parameters. In that case the function may directly access the passed object’s members.

- o

  Since no object is involved in calling the static function, no ambiguity can arise as to whether an object reference or an object pointer should be used in calling the function: static (member) functions may be called without using objects.

## NAMESPACE

**FBB**\
 All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace **FBB**.

## INHERITS FROM

\-

## STATIC MEMBERS

- o

  **Functor unary(Function \[, Arguments\])**:\
   *Functor* represents the (unary) functor that can be used by generic algorithms;\
   *Function* is the name of a static member or free function that will be called from the *Functor*’s function operator. The type of its  first argument must match the type of the argument received by the functor’s function call operator. Any additional types must match, in type and number, the additional arguments that are passed to *unary*. *Function*’s return type will be the return type of *Functor*’s function call operator. If a value is returned by *Function* it will be returned by *Functor*’s function call operator.

-

  - o

  **Functor binary(Function \[, Arguments\])**:\
   *Functor* represents the (binary) functor that can be used by generic algorithms;\
   *Function* is the name of a static member or free function that will be called from the *Functor*’s function operator. The types of its first two arguments must match the types of the first two arguments received by the functor’s function call operator. Any additional types must match, in type and number, the additional arguments that are passed to *binary*. *Function*’s return type will be the return type of *Functor*’s function call operator. If a value is returned by *Function* it will be returned by *Functor*’s function call operator.

## TYPEDEFS

The functors defines types, that are used by generic algorithms:

- o

  **first\_argument\_type**:\
   , a synonym for the basic type of the argument received by the unary functor’s function call operator and of the first argument received by the binary functor’s function call operator. E.g., if the actual type is *std::string const \** then *argument\_type* will be *std::string*;

- o

  **second\_argument\_type**:\
   , a synonym for the basic type of the second argument received by the binary functor’s function call operator.

- o

  **result\_type**:\
   , a synonym for the basic type of the return type of the  functor’s function call operator.

## EXAMPLES

```
    // accumulating strings from a vector to one big string, using
    // `accumulate’
    #include <iostream>
    #include <numeric>
    #include <string>
    #include <vector>
    #include <bobcat/fnwrap>
    using namespace std;
    using namespace FBB;
    class Strings
    {
        vector<string> d_vs;
        public:
            Strings()
            :
                d_vs({"one", "two", "three"})
            {}
            void display(ostream &out) const
            {
                size_t count = 0;
                cout << "On Exit: " <<
                    accumulate(
                        d_vs.begin(), d_vs.end(),
                        string("HI"),
                        FnWrap::binary(show, count, out)) << ’\n’;
            }
        private:
            static string show(string const &str1,
                                    string const &str2,
                                    size_t &nr, ostream &out)
            {
                out << ++nr << " " << str1 << " " << str2 << ’\n’;
                return str1 + " " + str2;
            }
    };
    int main()
    {
        Strings s;
        s.display(cout);
    }
```

After compilation and linking, simply call the program without any arguments.

## FILES

*bobcat/fnwrap2c* - defines the class interface

## SEE ALSO

[**bobcat**(7)](/bobcat/7.md), [**fnwrap1**(3bobcat)](/fnwrap1/3bobcat.md), [**fnwrap2**(3bobcat)](/fnwrap2/3bobcat.md),  [**fnwrap2**(3bobcat)](/fnwrap2/3bobcat.md), [**foreach**(3bobcat)](/foreach/3bobcat.md), [**lc**(3bobcat)](/lc/3bobcat.md),  [**repeat**(3bobcat)](/repeat/3bobcat.md)

## BUGS

None Reported.

## DISTRIBUTION FILES

- o

  *bobcat\_2.08.01-x.dsc*: detached signature;

- o

  *bobcat\_2.08.01-x.tar.gz*: source archive;

- o

  *bobcat\_2.08.01-x\_i386.changes*: change log;

- o

  *libbobcat1\_2.08.01-x\_\*.deb*: debian package holding the libraries;

- o

  *libbobcat1-dev\_2.08.01-x\_\*.deb*: debian package holding the libraries, headers and manual pages;

- o

  *http://sourceforge.net/projects/bobcat*: public archive location;

## BOBCAT

Bobcat is an acronym of \`Brokken’s Own Base Classes And Templates’.

## COPYRIGHT

This is free software, distributed under the terms of the  GNU General Public License (GPL).

## AUTHOR

Frank B. Brokken (**f.b.brokken@rug.nl**).
