FBB::FnWrap1c(3) Configurabale unary argument wrapper template class

SYNOPSIS

#include <bobcat/fnwrap1c>

DESCRIPTION

The FBB::FnWrap1c class is a configurable unary argument context wrapper template class. Its primary use is in combination with the generic algorithms from the standard template libray. The called function expects a local context struct which is used to pass arguments to the function called by the generic algorithm which are available in the local context of the called generic algorithm. The local context struct can be either a const or non-const struct.

The called 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 FBB::FnWrap1c 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::FnWrap1c template class has the following template parameters:

  • Type: the type of the argument passed to FBB::FnWrap1c'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).
  • Context: the local context struct. This struct is a local struct, an object of which could have been defined immediatey before applying the generic algorithm. The local context struct object may specify values, references or pointers to entities that are available in the local context where the generic algorithm is called.
  • 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 FnWrap1c'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.
  • ReturnType: the ReturnType is by default defined as void. By specifying another type, the FBB::FnWrap1c object's operator()() function will return the called function's return value as a value of the specified type. E.g, by specifying a bool ReturnType, the FBB::FnWrap1c 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.

INHERITS FROM

-

CONSTRUCTOR

o
FnWrap1c<Type, Context [, ReturnType = void]> (ReturnType (*fun)(Type, Context), Context context):
This constructor expects two arguments: the address of a function to call from within its operator()() member, and a local context struct which is passed to the called function as its second argument.
When the function pointed to by fun is called from FBB::FnWrap1c::operator()(), it receives the latter function's argument as its first argument and the local context struct as its second argument. With (STL) generic algorithms the template parameter Type must define the data type to which iterators (as specified in, e.g., std::for_each()) 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).

OVERLOADED OPERATOR

The following member function will call the function that's passed to FBB::FnWrap1c's constructor. See the example below.

  • ReturnType operator()(Type param) const:
    This function is called by generic algorithms, receiving the dereferenced iterator that is managed by the generic algorithm as its argument (so, the iterator may points to modifiable Type objects). This function calls the function specified at FBB::FnWrap1c's constructor, passing its parameter value to that function as its first argument, and the local context as its second argument.

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.

EXAMPLE

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <bobcat/fnwrap1c>
    
    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("");
                d_vs.push_back("three");
            }
    
            void display(ostream &out) const
            {
                SContext c = {1, out};
                find_if(d_vs.begin(), d_vs.end(),
                    FnWrap1c<string const &, SContext &, bool>
                         (&Strings::untilEmpty, c));
            }
    
        private:
            struct SContext
            {
                size_t nr;
                ostream &out;
            };
    
            static bool untilEmpty(string const &str, SContext &c)
            {
                if (str.empty())
                    return true;        // stop
    
                c.out << c.nr++ << " " << str << endl;
                return false;           // don't stop
            }
    };
    
    int main()
    {
        Strings s;
    
        s.display(cout);
    }
        
After compilation and linking, simply call the program without any arguments.

FILES

bobcat/fnwrap1c - defines the class interface

BUGS

The fnwrap(3bobcat) function wrapper is easier to use an provides this class's functionality.

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 FNWrap1c 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.

DISTRIBUTION FILES

  • bobcat_2.08.01-x.dsc: detached signature;
  • bobcat_2.08.01-x.tar.gz: source archive;
  • bobcat_2.08.01-x_i386.changes: change log;
  • libbobcat1_2.08.01-x_*.deb: debian package holding the libraries;
  • libbobcat1-dev_2.08.01-x_*.deb: debian package holding the libraries, headers and manual pages;
  • 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 ([email protected]).