fnwrap23bobcat
Stl binary argument wrapper template class
Usage
#include <bobcat/fnwrap2>
Overview
The FBB::FnWrap2 class is a configurable binary argument wrapper template class. Its primary use is in combination with the generic algorithms of the standard template libray.
The callled function itself may be specified as one of the constructor’s arguments. It must be a (static member) function. Using a (static member) function has various advantages, especially with the FnWrap?c classes to which a local context can be passed:
- o
-
There is no introduced uncertainty about the
const-ness of the callled function, as static member functions do not support aconstmodifier;
- o
-
The passed function can also be a free (global) function to which a local context is passed;
- o
-
The passed function can be a static member function of the class using the generic algorithm to which the
FBB::FnWrap2object is passed. By passing the calling object in the function’s local context, the function 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.
The FBB::FnWrap2 template class has the following template parameters:
- o
-
Type1: the type of the first argument passed to
FBB::FnWrap2’soperator()()function. Specify the type as the type of the first parameter of the function whose address is passed to the constructor (i.e., specify a plain value or a (const) pointer or reference).
- o
-
Type2: the type of the second argument passed to
FBB::FnWrap2’soperator()()function. Specify the type as the type of the second parameter of the function whose address is passed to the constructor (i.e., specify a plain value or a (const) pointer or reference).
- o
-
ReturnType: the
ReturnTypeis by default defined as void. By specifying another type, theFBB::FnWrap2object’soperator()()function will return the called function’s return value as a value of the specified type. E.g, by specifying a boolReturnType, theFBB::FnWrap2object may be used as a Unary Predicate. Alternatively, pointers or references may be specified as return values.
Namespace
FBB
Inherits from
-
Constructor
- o
-
FnWrap2<Type1, Type2 [, Returntype = void]>(ReturnType (*fun)(Type1, Type2)):
This constructor expects one argument: the address of a function to call from within itsoperator()()member. The function receivesFBB::FnWrap2::operator()()’s arguments as its arguments. When using (STL) generic algorithms, the template parameter types Type1 and Type2 are the types to which iterators eventually point.
Overloaded operator
In normal situations the following member function will call the function that is passed to FBB::FnWrap2’s constructor. See the example below.
- o
-
ReturnType operator()(Type1 param, Type2 param) const:
This function is called by the generic algorithms, receiving the dereferenced iterators that are managed by generic algorithms as its arguments (e.g., the iterator points to modifiable Type1 and Type2 objects).
Typedefs
The class defines three types, which are used by generic algorithms:
- o
-
first_argument_type:
, a synonym for the basic type specified with the Type1 template parameter. E.g., if Type1 is specified as std::string const * then first_argument_type will be std::string;
- o
-
second_argument_type:
, a synonym for the basic type specified with the Type2 template parameter.
- o
-
result_type:
, a synonym for the basic type specified with theReturnTypetemplate parameter.
Examples
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <bobcat/fnwrap2>
using namespace std;
using namespace FBB;
bool cmp(string &actual, string const &target)
{
cout << "Saw " << actual << endl;
bool ret = actual == target;
actual += ".";
return ret;
}
class X
{
public:
static bool cmp(string const &actual, string const &target)
{
cout << "Saw " << actual << endl;
return actual == target;
}
};
int main(int argc, char **argv)
{
vector<string> vs(argv, argv + argc);
find_if(
vs.begin(), vs.end(),
bind2nd(
FnWrap2<string &, string const &, bool>(&cmp),
string("hello")
)
);
cout << endl;
find_if(
vs.begin(), vs.end(),
bind2nd(
FnWrap2<string const &, string const &, bool>(&X::cmp),
string("hello")
)
);
}
After compilation and linking, the program could be called as follows:
a.out one two hello three hello four
Files
bobcat/fnwrap2 - defines the class interface
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’.
Releases
2 releases since Debian 5.0 (Lenny); the text changed 1 time.