Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/range/doc/example.cpp @ 69

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

updated boost from 1_33_1 to 1_34_1

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