Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/range/doc/example.cpp @ 20

Last change on this file since 20 was 12, checked in by landauf, 18 years ago

added boost

File size: 3.6 KB
RevLine 
[12]1#include <boost/range.hpp>
2#include <iterator>         // for std::iterator_traits, std::distance()
3
4namespace Foo
5{
6        //
7        // Our sample UDT. A 'Pair'
8        // will work as a range when the stored
9        // elements are iterators.
10        //
11        template< class T >
12        struct Pair
13        {
14                T first, last;
15        };
16
17} // namespace 'Foo'
18
19namespace boost
20{
21        //
22        // Specialize metafunctions. We must include the range.hpp header.
23        // We must open the 'boost' namespace.
24        //
25        /*
26        template< class T >
27        struct range_value< Foo::Pair<T> >
28        {
29                typedef typename std::iterator_traits<T>::value_type type;
30        };
31        */
32
33        template< class T >
34        struct range_iterator< Foo::Pair<T> >
35        {
36                typedef T type;
37        };
38
39        template< class T >
40        struct range_const_iterator< Foo::Pair<T> >
41        {
42                //
43                // Remark: this is defined similar to 'range_iterator'
44                //         because the 'Pair' type does not distinguish
45                //         between an iterator and a const_iterator.
46                //
47                typedef T type;
48        };
49
50        /*
51    template< class T >
52        struct range_difference< Foo::Pair<T> >
53        {
54                typedef typename std::iterator_traits<T>::difference_type type;
55        };
56        */
57
58        template< class T >
59    struct range_size< Foo::Pair<T> >
60        {
61                int static_assertion[ sizeof( std::size_t ) >=
62                          sizeof( typename range_difference< Foo::Pair<T> >::type ) ];
63                typedef std::size_t type;
64        };
65
66} // namespace 'boost'
67
68namespace Foo
69{
70        //
71        // The required functions. These should be defined in
72        // the same namespace as 'Pair', in this case
73        // in namespace 'Foo'.
74        //
75
76        template< class T >
77        inline T boost_range_begin( Pair<T>& x )
78        {
79                return x.first;
80        }
81
82    template< class T >
83        inline T boost_range_begin( const Pair<T>& x )
84        {
85                return x.first;
86        }
87
88        template< class T >
89    inline T boost_range_end( Pair<T>& x )
90        {
91                return x.last;
92        }
93
94        template< class T >
95    inline T boost_range_end( const Pair<T>& x )
96        {
97                return x.last;
98        }
99
100        template< class T >
101        inline typename boost::range_size< Pair<T> >::type
102        boost_range_size( const Pair<T>& x )
103        {
104                return std::distance(x.first,x.last);
105        }
106
107} // namespace 'Foo'
108
109#include <vector>
110
111int main()
112{
113        typedef std::vector<int>::iterator  iter;
114        std::vector<int>                    vec;
115        vec.push_back( 42 );
116        Foo::Pair<iter>                     pair  = { vec.begin(), vec.end() };
117        const Foo::Pair<iter>&              cpair = pair;
118        //
119        // Notice that we call 'begin' etc with qualification.
120        //
121        iter i = boost::begin( pair );
122        iter e = boost::end( pair );
123        i      = boost::begin( cpair );
124        e      = boost::end( cpair );
125        boost::range_size< Foo::Pair<iter> >::type s = boost::size( pair );
126        s      = boost::size( cpair );
127        boost::range_const_reverse_iterator< Foo::Pair<iter> >::type
128        ri     = boost::rbegin( cpair ),
129        re         = boost::rend( cpair );
130
131        //
132        // Test metafunctions
133        //
134
135        boost::range_value< Foo::Pair<iter> >::type
136        v = *boost::begin(pair);
137
138        boost::range_difference< Foo::Pair<iter> >::type
139        d = boost::end(pair) - boost::begin(pair);
140}
141
Note: See TracBrowser for help on using the repository browser.