FBB::FnWrap1 - STL unary argument wrapper template class
SYNOPSIS
#include <bobcat/fnwrap1>
DESCRIPTION
The FBB::FnWrap1 class is a configurable unary 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:
There is no introduced uncertainty about the const-ness of the
callled function, as static member functions do not support
a const modifier;
The passed function can also be a free (global) function to which a
local context is passed;
The passed function can be a static member function of the class
using the generic algorithm to which the c() object is passed. By passing
the calling object in the function's local context, the function may directly
access the calling object's members.
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.
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::FnWrap1 template class has the following template parameters:
Type: the type of the argument passed to FBB::FnWrap1's
operator()() function. Specify the type as the type of the parameter of
the function whose address is passed to the constructor (i.e., specify a plain
value or a (const) pointer or reference).
ReturnType: the ReturnType is by default defined as void. By
specifying another type, the FBB::FnWrap1 object's operator()()
function will return the called function's return value as a value of the
specified type. E.g, by specifying a boolReturnType, the
FBB::FnWrap1 object may be used as a Unary Predicate. Alternatively,
pointers or references may be specified as return values.
NAMESPACE
FBB
All constructors, members, operators and manipulators, mentioned in this
man-page, are defined in the namespace FBB.
This constructor expects one argument: the address of a function to
call from within its operator()() member. The function receives
FBB::FnWrap1::operator()()'s argument as its argument. When
using (STL) generic algorithms, the template parameter type Type
is the data type to which iterators (as specified in, e.g.,
std::for_each()) eventually point.
OVERLOADED OPERATOR
In normal situations the following member function will call the function
that is passed to FBB::FnWrap1's constructor. See the example below.
ReturnType operator()(Type param) const:
This function is called by the generic algorithms, receiving the
dereferenced iterator that is managed by generic algorithms as its
argument (e.g., the iterator points to modifiable Type objects).
TYPEDEFS
The class defines two types, which are used by generic algorithms:
argument_type: , a synonym for the basic type specified with the
Type template parameter. E.g., if Type is specified as std::string
const * then argument_type will be std::string;
result_type: , a synonym for the basic type specified with the
ReturnType template parameter.
EXAMPLES
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <bobcat/fnwrap1>
using namespace std;
using namespace FBB;
bool cmp(string &actual) // modifies `actual'
{
cout << "Saw " << actual << endl;
bool ret = actual == "hello";
actual += ".";
return ret;
}
class X
{
public:
// merely inspects `actual'
static bool cmp(string const &actual)
{
cout << "Saw " << actual << endl;
return actual == "hello";
}
};
int main(int argc, char **argv)
{
vector<string> vs(argv, argv + argc);
vector<string>::iterator it;
it = find_if(vs.begin(), vs.end(),
FnWrap1<string &, bool>(&cmp));
if (it != vs.end())
cout << "At " << *it << endl;
cout << endl;
it = find_if(vs.begin(), vs.end(),
FnWrap1<string const &, bool>(&X::cmp));
if (it != vs.end())
cout << "At " << *it << endl;
}
After compilation and linking, the program could be called as follows: