1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
[/
Copyright 2010 Neil Groves
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
/]
[section:extending Extending the library]
[section:method_1 Method 1: provide member functions and nested types]
This procedure assumes that you have control over the types that should be made conformant to a Range concept. If not, see [link range.reference.extending.method_2 method 2].
The primary templates in this library are implemented such that standard containers will work automatically and so will __boost_array__. Below is given an overview of which member functions and member types a class must specify to be useable as a certain Range concept.
[table
[[Member function] [Related concept ]]
[[`begin()` ] [__single_pass_range__]]
[[`end()` ] [__single_pass_range__]]
]
Notice that `rbegin()` and `rend()` member functions are not needed even though the container can support bidirectional iteration.
The required member types are:
[table
[[Member type ] [Related concept ]]
[[`iterator` ] [__single_pass_range__]]
[[`const_iterator`] [__single_pass_range__]]
]
Again one should notice that member types `reverse_iterator` and `const_reverse_iterator` are not needed.
[endsect]
[section:method_2 Method 2: provide free-standing functions and specialize metafunctions]
This procedure assumes that you cannot (or do not wish to) change the types that should be made conformant to a Range concept. If this is not true, see [link range.reference.extending.method_1 method 1].
The primary templates in this library are implemented such that certain functions are found via argument-dependent-lookup (ADL). Below is given an overview of which free-standing functions a class must specify to be useable as a certain Range concept. Let `x` be a variable (`const` or `mutable`) of the class in question.
[table
[[Function ] [Related concept ]]
[[`range_begin(x)`] [__single_pass_range__]]
[[`range_end(x)` ] [__single_pass_range__]]
[[`range_calculate_size(x)`] [ Optional. This can be used to specify a mechanism for constant-time computation of the size of a range. The default behaviour is to return `boost::end(x) - boost::begin(x)` for random access ranges, and to return `x.size()` for ranges with lesser traversal capability. This behaviour can be changed by implementing `range_calculate_size` in a manner that will be found via ADL. The ability to calculate size in O(1) is often possible even with ranges with traversal categories less than random access.]]
]
`range_begin()` and `range_end()` must be overloaded for both `const` and `mutable` reference arguments.
You must also specialize two metafunctions for your type `X`:
[table
[[Metafunction ] [Related concept ]]
[[`boost::range_mutable_iterator`] [__single_pass_range__]]
[[`boost::range_const_iterator`] [__single_pass_range__]]
]
A complete example is given here:
``
#include <boost/range.hpp>
#include <iterator> // for std::iterator_traits, std::distance()
namespace Foo
{
//
// Our sample UDT. A 'Pair'
// will work as a range when the stored
// elements are iterators.
//
template< class T >
struct Pair
{
T first, last;
};
} // namespace 'Foo'
namespace boost
{
//
// Specialize metafunctions. We must include the range.hpp header.
// We must open the 'boost' namespace.
//
template< class T >
struct range_mutable_iterator< Foo::Pair<T> >
{
typedef T type;
};
template< class T >
struct range_const_iterator< Foo::Pair<T> >
{
//
// Remark: this is defined similar to 'range_iterator'
// because the 'Pair' type does not distinguish
// between an iterator and a const_iterator.
//
typedef T type;
};
} // namespace 'boost'
namespace Foo
{
//
// The required functions. These should be defined in
// the same namespace as 'Pair', in this case
// in namespace 'Foo'.
//
template< class T >
inline T range_begin( Pair<T>& x )
{
return x.first;
}
template< class T >
inline T range_begin( const Pair<T>& x )
{
return x.first;
}
template< class T >
inline T range_end( Pair<T>& x )
{
return x.last;
}
template< class T >
inline T range_end( const Pair<T>& x )
{
return x.last;
}
} // namespace 'Foo'
#include <vector>
int main(int argc, const char* argv[])
{
typedef std::vector<int>::iterator iter;
std::vector<int> vec;
Foo::Pair<iter> pair = { vec.begin(), vec.end() };
const Foo::Pair<iter>& cpair = pair;
//
// Notice that we call 'begin' etc with qualification.
//
iter i = boost::begin( pair );
iter e = boost::end( pair );
i = boost::begin( cpair );
e = boost::end( cpair );
boost::range_difference< Foo::Pair<iter> >::type s = boost::size( pair );
s = boost::size( cpair );
boost::range_reverse_iterator< const Foo::Pair<iter> >::type
ri = boost::rbegin( cpair ),
re = boost::rend( cpair );
return 0;
}
``
[endsect]
[section:method_3 Method 3: provide range adaptor implementations]
[section:method_3_1 Method 3.1: Implement a Range Adaptor without arguments]
To implement a Range Adaptor without arguments (e.g. reversed) you need to:
# Provide a range for your return type, for example:
``
#include <boost/range/iterator_range.hpp>
#include <boost/iterator/reverse_iterator.hpp>
template< typename R >
struct reverse_range :
boost::iterator_range<
boost::reverse_iterator<
typename boost::range_iterator<R>::type> >
{
private:
typedef boost::iterator_range<
boost::reverse_iterator<
typename boost::range_iterator<R>::type> > base;
public:
typedef boost::reverse_iterator<
typename boost::range_iterator<R>::type > iterator;
reverse_range(R& r)
: base(iterator(boost::end(r)), iterator(boost::begin(r)))
{ }
};
``
# Provide a tag to uniquely identify your adaptor in the `operator|` function overload set
``
namespace detail {
struct reverse_forwarder {};
}
``
# Implement `operator|`
``
template< class BidirectionalRng >
inline reverse_range<BidirectionalRng>
operator|( BidirectionalRng& r, detail::reverse_forwarder )
{
return reverse_range<BidirectionalRng>( r );
}
template< class BidirectionalRng >
inline reverse_range<const BidirectionalRng>
operator|( const BidirectionalRng& r, detail::reverse_forwarder )
{
return reverse_range<const BidirectionalRng>( r );
}
``
# Declare the adaptor itself (it is a variable of the tag type).
``
namespace
{
const detail::reverse_forwarder reversed = detail::reverse_forwarder();
}
``
[endsect]
[section:method_3_2 Method 3.2: Implement a Range Adaptor with arguments]
# Provide a range for your return type, for example:
``
#include <boost/range/adaptor/argument_fwd.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/iterator/transform_iterator.hpp>
template<typename Value>
class replace_value
{
public:
typedef const Value& result_type;
typedef const Value& argument_type;
replace_value(const Value& from, const Value& to)
: m_from(from), m_to(to)
{
}
const Value& operator()(const Value& x) const
{
return (x == m_from) ? m_to : x;
}
private:
Value m_from;
Value m_to;
};
template<typename Range>
class replace_range
: public boost::iterator_range<
boost::transform_iterator<
replace_value<typename boost::range_value<Range>::type>,
typename boost::range_iterator<Range>::type> >
{
private:
typedef typename boost::range_value<Range>::type value_type;
typedef typename boost::range_iterator<Range>::type iterator_base;
typedef replace_value<value_type> Fn;
typedef boost::transform_iterator<Fn, iterator_base> replaced_iterator;
typedef boost::iterator_range<replaced_iterator> base_t;
public:
replace_range(Range& rng, value_type from, value_type to)
: base_t(replaced_iterator(boost::begin(rng), Fn(from,to)),
replaced_iterator(boost::end(rng), Fn(from,to)))
{
}
};
``
# Implement a holder class to hold the arguments required to construct the RangeAdaptor.
The holder combines multiple parameters into one that can be passed as the right operand of `operator|()`.
``
template<typename T>
class replace_holder : public boost::range_detail::holder2<T>
{
public:
replace_holder(const T& from, const T& to)
: boost::range_detail::holder2<T>(from, to)
{ }
private:
void operator=(const replace_holder&);
};
``
# Define an instance of the holder with the name of the adaptor
``
static boost::range_detail::forwarder2<replace_holder>
replaced = boost::range_detail::forwarder2<replace_holder>();
``
# Define `operator|`
``
template<typename SinglePassRange>
inline replace_range<SinglePassRange>
operator|(SinglePassRange& rng,
const replace_holder<typename boost::range_value<SinglePassRange>::type>& f)
{
return replace_range<SinglePassRange>(rng, f.val1, f.val2);
}
template<typename SinglePassRange>
inline replace_range<const SinglePassRange>
operator|(const SinglePassRange& rng,
const replace_holder<typename boost::range_value<SinglePassRange>::type>& f)
{
return replace_range<const SinglePassRange>(rng, f.val1, f.val2);
}
``
[endsect]
[endsect]
[endsect]
|