DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IteratorTraits.h
1 
17 #pragma once
18 
31 #if defined(IteratorTraits_RECURSES)
32 #error Recursive header files inclusion detected in IteratorTraits.h
33 #else // defined(IteratorTraits_RECURSES)
34 
35 #define IteratorTraits_RECURSES
36 
37 #if !defined IteratorTraits_h
38 
39 #define IteratorTraits_h
40 
42 // Inclusions
43 #include<iterator>
44 #include<boost/iterator/iterator_categories.hpp>
45 #include<boost/iterator/iterator_facade.hpp>
47 
48 namespace DGtal
49 {
50 
52 // tag classes for type (either Iterator or Circulator)
53 struct IteratorType {};
54 struct CirculatorType {};
55 
57 // tag classes for traversal category (Foward, Bidirectional, RandomAccess)
58 // valid for both iterator and circulator
59 struct ForwardCategory {};
62 
64 namespace detail
65 {
66 
68 
75  template <typename IC>
76  struct HasNestedType
77  {
78  typedef char yes[1];
79  typedef char no[2];
80 
81  template <typename C>
82  static yes& test(typename C::Type*);
83 
84  template <typename C>
85  static no& test(...);
86 
87  static const bool value = sizeof(test<IC>(0)) == sizeof(yes);
88  };
89 
91 
98  template <typename IC, bool flagHasNestedType = false>
99  struct IsCirculator
100  {
101  static const bool value = false;
102  };
103 
104  template <typename IC>
105  struct IsCirculator<IC,true>
106  {
107  //from en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
108  typedef char yes[1];
109  typedef char no[2];
110 
111  static yes& test(CirculatorType);
112 
113  static no& test(IteratorType);
114 
115  static const bool value = ( sizeof(test(typename IC::Type())) == sizeof(yes) );
116  };
117 
118 } //namespace detail
119 
121 
128 template <typename IC>
130 {
132 };
133 
134 namespace detail
135 {
137 
145  template<bool b = false>
147  {
148  typedef IteratorType Type;
149  };
150  template<>
152  {
154  };
155 }//namespace detail
156 
158 
165 template<typename IC>
167 public:
169 };
170 
172 
181 //default
182 template <typename C>
184  typedef C Category;
185 };
186 
187 //for STL iterators
188 template <>
189 struct IteratorCirculatorTagTraits<std::forward_iterator_tag> {
191 };
192 
193 template <>
194 struct IteratorCirculatorTagTraits<std::bidirectional_iterator_tag> {
196 };
197 
198 template <>
199 struct IteratorCirculatorTagTraits<std::random_access_iterator_tag> {
201 };
202 
203 //for boost traversal categories
204 template <>
205 struct IteratorCirculatorTagTraits<boost::forward_traversal_tag> {
207 };
208 
209 template <>
210 struct IteratorCirculatorTagTraits<boost::bidirectional_traversal_tag> {
212 };
213 
214 template <>
215 struct IteratorCirculatorTagTraits<boost::random_access_traversal_tag> {
217 };
218 
219 template <>
220 struct IteratorCirculatorTagTraits<boost::detail::iterator_category_with_traversal<std::input_iterator_tag,boost::forward_traversal_tag> > {
222 };
223 
224 template <>
225 struct IteratorCirculatorTagTraits<boost::detail::iterator_category_with_traversal<std::input_iterator_tag,boost::bidirectional_traversal_tag> > {
227 };
228 
229 template <>
230 struct IteratorCirculatorTagTraits<boost::detail::iterator_category_with_traversal<std::input_iterator_tag,boost::random_access_traversal_tag> > {
232 };
233 
234 
235 
236 
238 
247 template <typename IC>
249 
250  typedef typename IteratorCirculatorType<IC>::Type
252 
253  typedef typename IteratorCirculatorTagTraits
254  <typename boost::iterator_category<IC>::type>::Category
256 
257  typedef typename IC::value_type Value;
258  typedef typename IC::difference_type Difference;
259  typedef typename IC::pointer Pointer;
260  typedef typename IC::reference Reference;
261 
262 };
263 
264 template <class T>
266  typedef IteratorType Type;
268  typedef T Value;
269  typedef ptrdiff_t Difference;
270  typedef T* Pointer;
271  typedef T& Reference;
272 };
273 
274 template <class T>
275 struct IteratorCirculatorTraits<T const*>
276 {
277  typedef IteratorType Type;
279  typedef T Value;
280  typedef ptrdiff_t Difference;
281  typedef T const* Pointer;
282  typedef T const& Reference;
283 };
284 
285 } // namespace DGtal
286 
287 
289 // Includes inline functions.
290 //#include "DGtal/base/IteratorTraits.ih"
291 
292 // //
294 
295 #endif // !defined IteratorTraits_h
296 
297 #undef IteratorTraits_RECURSES
298 #endif // else defined(IteratorTraits_RECURSES)