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:
The FBB::FnWrap2c template class has the following template parameters:
If no generic algorithm would have been used, but a local implementation of the generic algorithm would have been used instead, then the called function would have received certain arguments. The local context struct is a replacement of such a function's parameter list, mimicking the function's parameter list in the struct definition. The function will now receive a `standardized' parameter list, defined by the local context struct. The type of the defined struct as specified in the parameterlist of the function whose address is passed to FnWrap2c's constructor should be specified for Context. E.g., LocalStruct &.
When a non-const reference or pointer is specified, the function may modify the struct's value fields identically to the situation where the field's values are passed to the function as reference parameters.
Local context structs may also be generated using the FBB::LC local Context Struct generating template class. Using a template-generated local context struct reduces namespace and class-pollution: the software engineer does not have to put any effort in finding an appropriate struct name and doesn't even have to enter the struct's definition in, e.g., a class header. The template takes care of the definition an declaration of the proper type which will thus be available to classes, member functions and free functions. Cf. the lc(3bobcat) for details.
When the function pointed to by fun is called from FBB::FnWrap2c::operator()(), it receives the latter function's arguments as its first two arguments and the local context struct as its third argument. With (STL) generic algorithms, the template parameters Type1 and Type2 must define the data type to which iterators eventually point.
Hint: In situations where no context other than the class Class to which the class' (static) member function belongs must be used `Class &obj' (or a (const) pointer) can be specified as the context parameter, passing, e.g., *this as the context. The static member function may then call any of the non-static member functions of the class Class using the normal syntax (e.g., obj.member(argument) if the static function defines as its second parameter Class &obj).
The following member function will call the function that's passed to FBB::FnWrap2c's constructor. See the example below.
The class defines three types, which are used by generic algorithms:
// accumulating strings from a vector to one big string, using
// `accumulate'
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <bobcat/fnwrap2c>
using namespace std;
using namespace FBB;
class Strings
{
vector<string> d_vs;
public:
Strings()
{
d_vs.push_back("one");
d_vs.push_back("two");
d_vs.push_back("three");
}
void display(ostream &out) const
{
SContext c = {1, out};
cout << "On Exit: " <<
accumulate(
d_vs.begin(), d_vs.end(),
string("HI"),
FnWrap2c<string const &, string const &,
SContext &, string>(&show, c)
) <<
endl;
}
private:
struct SContext
{
size_t nr;
ostream &out;
};
static string show(string const &str1,
string const &str2,
SContext &c)
{
c.out << c.nr++ << " " << str1 << " " << str2 <<
endl;
return str1 + " " + str2;
}
};
int main()
{
Strings s;
s.display(cout);
}
After compilation and linking, simply call the program without any
arguments.
Caveat: the template parameter specifying the type of the local context struct should probably not be specified as a value type as this will result in copying the local context struct for each call of the FNWrap2c object or of the function that it is provided with, making it impossible for the algorithms to modify value- or pointer-fields of the outermost local context struct. Instead, the template type specifying the type of the local context struct should be specified as a pointer or reference template type when instantiating the FnWrap1c object.