Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/algorithm/string/doc/concept.xml @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 7.9 KB
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
3"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
4
5<!-- Copyright (c) 2002-2006 Pavol Droba.
6     Subject to the Boost Software License, Version 1.0.
7     (See accompanying file LICENSE-1.0 or  http://www.boost.org/LICENSE-1.0)
8-->
9
10
11<section id="string_algo.concept" last-revision="$Date: 2006/08/16 07:10:47 $">
12    <title>Concepts</title>
13
14    <using-namespace name="boost"/>
15    <using-namespace name="boost::algorithm"/>
16
17    <section>   
18        <title>Definitions</title>
19       
20        <table>
21            <title>Notation</title>
22            <tgroup cols="2" align="left">
23                <tbody>
24                    <row>
25                        <entry><code>F</code></entry>
26                        <entry>A type that is a model of Finder</entry>
27                    </row>
28                    <row>
29                        <entry><code>Fmt</code></entry>
30                        <entry>A type that is a model of Formatter</entry>
31                    </row>
32                    <row>
33                        <entry><code>Iter</code></entry>
34                        <entry>
35                            Iterator Type
36                        </entry>
37                    </row>
38                    <row>
39                        <entry><code>f</code></entry>
40                        <entry>Object of type <code>F</code></entry>
41                    </row>
42                    <row>
43                        <entry><code>fmt</code></entry>
44                        <entry>Object of type <code>Fmt</code></entry>
45                    </row>
46                    <row>
47                        <entry><code>i,j</code></entry>
48                        <entry>Objects of type <code>Iter</code></entry>
49                    </row>
50                    </tbody>
51            </tgroup>
52        </table>
53    </section>
54
55    <section id="string_algo.finder_concept">
56        <title>Finder Concept</title>
57
58        <para>
59            Finder is a functor which searches for an arbitrary part of a container.
60            The result of the search is given as an <classname>iterator_range</classname> 
61            delimiting the selected part.
62        </para>
63
64        <table>             
65            <title>Valid Expressions</title>
66            <tgroup cols="3" align="left">
67                <thead>
68                    <row>   
69                        <entry>Expression</entry>
70                        <entry>Return Type</entry>
71                        <entry>Effects</entry>
72                    </row>
73                </thead>
74                <tbody>
75                    <row>
76                        <entry><code>f(i,j)</code></entry>
77                        <entry>Convertible to <code>iterator_range&lt;Iter&gt;</code></entry>
78                        <entry>Perform the search on the interval [i,j) and returns the result of the search</entry>
79                    </row>
80                </tbody>
81            </tgroup>
82        </table>
83
84        <para>
85            Various algorithms need to perform a search in a container and a Finder is a generalization of such
86            search operations that allows algorithms to abstract from searching. For instance, generic replace
87            algorithms can replace any part of the input, and the Finder is used to select the desired one.
88        </para>
89        <para>
90            Note, that it is only required that the finder works with a particular iterator type. However,
91            a Finder operation can be defined as a template, allowing the Finder to work with any iterator.
92        </para>
93        <para>
94            <emphasis role="bold">Examples</emphasis>
95        </para>
96        <para> 
97            <itemizedlist>
98                <listitem>
99                    Finder implemented as a class. This Finder always returns the whole input as a match. <code>operator()</code>
100                    is templated, so that the finder can be used on any iterator type.
101                   
102                    <programlisting>
103struct simple_finder
104{
105    template&lt;typename ForwardIteratorT&gt;
106    boost::iterator_range&lt;ForwardIterator&gt; operator()(
107        ForwardIteratorT Begin,
108        ForwardIteratorT End )
109    {
110        return boost::make_range( Begin, End );
111    }
112};
113        </programlisting>
114                </listitem>
115                <listitem>
116                    Function Finder. Finder can be any function object. That is, any ordinary function with the
117                    required signature can be used as well. However, such a function can be used only for
118                    a specific iterator type.
119                   
120                    <programlisting>
121boost::iterator_range&lt;std::string&gt; simple_finder(
122    std::string::const_iterator Begin,
123    std::string::const_iterator End )
124{
125    return boost::make_range( Begin, End );
126}
127        </programlisting>
128                </listitem>
129            </itemizedlist>
130        </para> 
131    </section>
132    <section id="string_algo.formatter_concept">
133        <title>Formatter concept</title>
134
135        <para>
136            Formatters are used by <link linkend="string_algo.replace">replace algorithms</link>.
137            They are used in close combination with finders.
138            A formatter is a functor, which takes a result from a Finder operation and transforms it in a specific way.
139            The operation of the formatter can use additional information provided by a specific finder,
140            for example <functionname>regex_formatter()</functionname> uses the match information from
141            <functionname>regex_finder()</functionname> to format the result of formatter operation.
142        </para>
143   
144        <table>
145            <title>Valid Expressions</title>
146            <tgroup cols="3" align="left">
147                <thead>
148                    <row>   
149                        <entry>Expression</entry>
150                        <entry>Return Type</entry>
151                        <entry>Effects</entry>
152                    </row>
153                </thead>
154                <tbody>
155                   <row>
156                        <entry><code>fmt(f(i,j))</code></entry>
157                        <entry>A container type, accessible using container traits</entry>
158                        <entry>Formats the result of the finder operation</entry>
159                    </row>
160                </tbody>
161            </tgroup>
162        </table>
163
164        <para>
165            Similarly to finders, formatters generalize format operations. When a finder is used to
166            select a part of the input, formatter takes this selection and performs some formating
167            on it. Algorithms can abstract from formating using a formatter.
168        </para>
169        <para>
170            <emphasis role="bold">Examples</emphasis>
171        </para>
172        <para> 
173            <itemizedlist>
174                <listitem>
175                    Formatter implemented as a class. This Formatter does not perform any formating and
176                    returns the match, repackaged. <code>operator()</code>
177                    is templated, so that the Formatter can be used on any Finder type.
178                   
179                    <programlisting>
180struct simple_formatter
181{
182    template&lt;typename FindResultT&gt;
183    std::string operator()( const FindResultT&amp; Match )
184    {
185        std::string Temp( Match.begin(), Match.end() );
186        return Temp;
187    }
188};
189                </programlisting>
190                </listitem>
191                <listitem>
192                    Function Formatter. Similarly to Finder, Formatter can be any function object.
193                    However, as a function, it can be used only with a specific Finder type.
194                 
195                    <programlisting>
196std::string simple_formatter( boost::iterator_range&lt;std::string::const_iterator&gt;&amp; Match )
197{
198    std::string Temp( Match.begin(), Match.end() );
199    return Temp;
200}
201                    </programlisting>
202                </listitem>
203            </itemizedlist>
204        </para> 
205     </section>
206</section>
Note: See TracBrowser for help on using the repository browser.