Sierra Toolkit  Version of the Day
random_access_iterator_wrapper.hpp
1 #ifndef stk_util_util_random_access_iterator_wrapper_hpp
2 #define stk_util_util_random_access_iterator_wrapper_hpp
3 
4 #include <boost/iterator/iterator_adaptor.hpp>
5 
6 #ifndef BOOST_NO_SFINAE
7 # include <boost/type_traits/is_convertible.hpp>
8 # include <boost/utility/enable_if.hpp>
9 #endif
10 
11 namespace stk_util {
12 
13 
14 template <class Value>
15 class random_access_iterator_wrapper
16  : public boost::iterator_adaptor<
17  random_access_iterator_wrapper<Value> // Derived
18  , Value* // Base
19  , boost::use_default // Value
20  , boost::random_access_traversal_tag // CategoryOrTraversal
21  >
22 {
23  private:
24 
25  typedef boost::iterator_adaptor<
26  random_access_iterator_wrapper<Value>,
27  Value*,
28  boost::use_default,
29  boost::random_access_traversal_tag
30  > base_type;
31 
32  struct enabler {}; // used to enable coversion constructor (if SFINAE)
33 
34  public:
35  random_access_iterator_wrapper()
36  : base_type(0) {}
37 
38  explicit random_access_iterator_wrapper(Value* p)
39  : base_type(p) {}
40 
41  Value& operator[] (ptrdiff_t index)
42  { return *(*this + index); }
43 
44  template <class OtherValue>
45  random_access_iterator_wrapper(
46  random_access_iterator_wrapper<OtherValue> const& other
47 # ifndef BOOST_NO_SFINAE
48  , typename boost::enable_if<
49  boost::is_convertible<OtherValue*,Value*>
50  , enabler
51  >::type = enabler()
52 # endif
53  )
54  : base_type(other.base()) {}
55 };
56 
57 }//namespace stk_util
58 
59 #endif
60