Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Concepts

Definitions
Finder Concept
Formatter concept

Definitions

Table 2.13. Notation

F A type that is a model of Finder
Fmt A type that is a model of Formatter
Iter Iterator Type
f Object of type F
fmt Object of type Fmt
i,j Objects of type Iter

Finder Concept

Finder is a functor which searches for an arbitrary part of a container. The result of the search is given as an iterator_range delimiting the selected part.

Table 2.14. Valid Expressions

Expression Return Type Effects
f(i,j) Convertible to iterator_range<Iter> Perform the search on the interval [i,j) and returns the result of the search

Various algorithms need to perform a search in a container and a Finder is a generalization of such search operations that allows algorithms to abstract from searching. For instance, generic replace algorithms can replace any part of the input, and the Finder is used to select the desired one.

Note, that it is only required that the finder works with a particular iterator type. However, a Finder operation can be defined as a template, allowing the Finder to work with any iterator.

Examples

  • Finder implemented as a class. This Finder always returns the whole input as a match. operator() is templated, so that the finder can be used on any iterator type.
    struct simple_finder
    {
        template<typename ForwardIteratorT>
        boost::iterator_range<ForwardIteratorT> operator()(
            ForwardIteratorT Begin,
            ForwardIteratorT End )
        {
            return boost::make_range( Begin, End );
        }
    };
            
  • Function Finder. Finder can be any function object. That is, any ordinary function with the required signature can be used as well. However, such a function can be used only for a specific iterator type.
    boost::iterator_range<std::string> simple_finder(
        std::string::const_iterator Begin,
        std::string::const_iterator End )
    {
        return boost::make_range( Begin, End );
    }
            

Formatter concept

Formatters are used by replace algorithms. They are used in close combination with finders. A formatter is a functor, which takes a result from a Finder operation and transforms it in a specific way. The operation of the formatter can use additional information provided by a specific finder, for example regex_formatter() uses the match information from regex_finder() to format the result of formatter operation.

Table 2.15. Valid Expressions

Expression Return Type Effects
fmt(f(i,j)) A container type, accessible using container traits Formats the result of the finder operation

Similarly to finders, formatters generalize format operations. When a finder is used to select a part of the input, formatter takes this selection and performs some formatting on it. Algorithms can abstract from formatting using a formatter.

Examples

  • Formatter implemented as a class. This Formatter does not perform any formatting and returns the match, repackaged. operator() is templated, so that the Formatter can be used on any Finder type.
    struct simple_formatter
    {
        template<typename FindResultT>
        std::string operator()( const FindResultT& Match )
        {
            std::string Temp( Match.begin(), Match.end() );
            return Temp;
        }
    };
                    
  • Function Formatter. Similarly to Finder, Formatter can be any function object. However, as a function, it can be used only with a specific Finder type.
    std::string simple_formatter( boost::iterator_range<std::string::const_iterator>& Match )
    {
        std::string Temp( Match.begin(), Match.end() );
        return Temp;
    }
                        


PrevUpHomeNext