pixel_iterator_adaptor.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003    
00004     Use, modification and distribution are subject to the Boost Software License,
00005     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
00006     http://www.boost.org/LICENSE_1_0.txt).
00007 
00008     See http://opensource.adobe.com/gil for most recent version including documentation.
00009 */
00010 
00011 /*************************************************************************************************/
00012 
00013 #ifndef GIL_PIXEL_ITERATOR_ADAPTOR_H
00014 #define GIL_PIXEL_ITERATOR_ADAPTOR_H
00015 
00024 
00025 #include <iterator>
00026 #include <boost/iterator/iterator_facade.hpp>
00027 #include "gil_config.hpp"
00028 #include "gil_concept.hpp"
00029 #include "pixel_iterator.hpp"
00030 
00031 namespace boost { namespace gil {
00032 
00033 
00037 
00038 
00041 
00042 template <typename Iterator,    // Models Iterator
00043           typename DFn>  // Models Returns the result of dereferencing a given iterator of type Iterator
00044 class dereference_iterator_adaptor : public iterator_adaptor<dereference_iterator_adaptor<Iterator,DFn>,
00045                                                              Iterator,
00046                                                              typename DFn::value_type,
00047                                                              use_default,
00048                                                              typename DFn::reference,
00049                                                              use_default> {
00050     DFn _deref_fn;
00051 public:
00052     typedef iterator_adaptor<dereference_iterator_adaptor<Iterator,DFn>,
00053                                     Iterator,
00054                                     typename DFn::value_type,
00055                                     use_default,
00056                                     typename DFn::reference,
00057                                     use_default> parent_t;
00058     typedef typename DFn::result_type                         reference;
00059     typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
00060     typedef DFn                                               dereference_fn;
00061 
00062     dereference_iterator_adaptor() {}
00063     template <typename Iterator1> 
00064     dereference_iterator_adaptor(const dereference_iterator_adaptor<Iterator1,DFn>& dit) : parent_t(dit.base()), _deref_fn(dit._deref_fn) {}
00065     dereference_iterator_adaptor(Iterator it, DFn deref_fn=DFn()) : parent_t(it), _deref_fn(deref_fn) {}
00066     template <typename Iterator1, typename DFn1> 
00067     dereference_iterator_adaptor(const dereference_iterator_adaptor<Iterator1,DFn1>& it) : parent_t(it.base()), _deref_fn(it._deref_fn) {}
00070     reference operator[](difference_type d) const { return *(*this+d);}
00071 
00072     // although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
00073     // it is often faster to just apply the relation operator to the base
00074     bool    operator> (const dereference_iterator_adaptor& p) const { return this->base_reference()> p.base_reference(); }
00075     bool    operator< (const dereference_iterator_adaptor& p) const { return this->base_reference()< p.base_reference(); }
00076     bool    operator>=(const dereference_iterator_adaptor& p) const { return this->base_reference()>=p.base_reference(); }
00077     bool    operator<=(const dereference_iterator_adaptor& p) const { return this->base_reference()<=p.base_reference(); }
00078     bool    operator==(const dereference_iterator_adaptor& p) const { return this->base_reference()==p.base_reference(); }
00079     bool    operator!=(const dereference_iterator_adaptor& p) const { return this->base_reference()!=p.base_reference(); }
00080 
00081     Iterator& base()              { return this->base_reference(); }
00082     const Iterator& base() const  { return this->base_reference(); }
00083     const DFn& deref_fn() const { return _deref_fn; }
00084 private:
00085     template <typename Iterator1, typename DFn1> 
00086     friend class dereference_iterator_adaptor;
00087     friend class boost::iterator_core_access;
00088 
00089     reference dereference() const { return _deref_fn(*(this->base_reference())); }
00090 };
00091 
00092 template <typename I, typename DFn> 
00093 struct const_iterator_type<dereference_iterator_adaptor<I,DFn> > { 
00094     typedef dereference_iterator_adaptor<typename const_iterator_type<I>::type,typename DFn::const_t> type; 
00095 };
00096 
00097 template <typename I, typename DFn> 
00098 struct iterator_is_mutable<dereference_iterator_adaptor<I,DFn> > : public mpl::bool_<DFn::is_mutable> {};
00099 
00100 
00101 template <typename I, typename DFn>
00102 struct is_iterator_adaptor<dereference_iterator_adaptor<I,DFn> > : public mpl::true_{};
00103 
00104 template <typename I, typename DFn>
00105 struct iterator_adaptor_get_base<dereference_iterator_adaptor<I,DFn> > {
00106     typedef I type;
00107 };
00108 
00109 template <typename I, typename DFn, typename NewBaseIterator>
00110 struct iterator_adaptor_rebind<dereference_iterator_adaptor<I,DFn>,NewBaseIterator> {
00111     typedef dereference_iterator_adaptor<NewBaseIterator,DFn> type;
00112 };
00113 
00115 //  PixelBasedConcept
00117 
00118 template <typename I, typename DFn>
00119 struct color_space_type<dereference_iterator_adaptor<I,DFn> > : public color_space_type<typename DFn::value_type> {};
00120 
00121 template <typename I, typename DFn>
00122 struct channel_mapping_type<dereference_iterator_adaptor<I,DFn> > : public channel_mapping_type<typename DFn::value_type> {};
00123 
00124 template <typename I, typename DFn>
00125 struct is_planar<dereference_iterator_adaptor<I,DFn> > : public is_planar<typename DFn::value_type> {};
00126 
00127 template <typename I, typename DFn>
00128 struct channel_type<dereference_iterator_adaptor<I,DFn> > : public channel_type<typename DFn::value_type> {};
00129 
00130 
00132 //  MemoryBasedIteratorConcept
00134 
00135 template <typename Iterator, typename DFn>
00136 struct byte_to_memunit<dereference_iterator_adaptor<Iterator,DFn> > : public byte_to_memunit<Iterator> {};
00137 
00138 template <typename Iterator, typename DFn>
00139 inline typename std::iterator_traits<Iterator>::difference_type 
00140 memunit_step(const dereference_iterator_adaptor<Iterator,DFn>& p) { 
00141     return memunit_step(p.base());
00142 }
00143 
00144 template <typename Iterator, typename DFn>
00145 inline typename std::iterator_traits<Iterator>::difference_type 
00146 memunit_distance(const dereference_iterator_adaptor<Iterator,DFn>& p1, 
00147               const dereference_iterator_adaptor<Iterator,DFn>& p2) { 
00148     return memunit_distance(p1.base(),p2.base()); 
00149 }
00150 
00151 template <typename Iterator, typename DFn>
00152 inline void memunit_advance(dereference_iterator_adaptor<Iterator,DFn>& p, 
00153                          typename std::iterator_traits<Iterator>::difference_type diff) { 
00154     memunit_advance(p.base(), diff);
00155 }
00156 
00157 template <typename Iterator, typename DFn>
00158 inline dereference_iterator_adaptor<Iterator,DFn> 
00159 memunit_advanced(const dereference_iterator_adaptor<Iterator,DFn>& p, 
00160               typename std::iterator_traits<Iterator>::difference_type diff) { 
00161     return dereference_iterator_adaptor<Iterator,DFn>(memunit_advanced(p.base(), diff), p.deref_fn()); 
00162 }
00163 
00164 
00165 template <typename Iterator, typename DFn>
00166 inline 
00167 typename std::iterator_traits<dereference_iterator_adaptor<Iterator,DFn> >::reference 
00168 memunit_advanced_ref(const dereference_iterator_adaptor<Iterator,DFn>& p, 
00169                   typename std::iterator_traits<Iterator>::difference_type diff) { 
00170     return *memunit_advanced(p, diff);
00171 }
00172 
00174 //  HasDynamicXStepTypeConcept
00176 
00177 template <typename Iterator, typename DFn>
00178 struct dynamic_x_step_type<dereference_iterator_adaptor<Iterator,DFn> > {
00179     typedef dereference_iterator_adaptor<typename dynamic_x_step_type<Iterator>::type,DFn> type;
00180 };
00181 
00184 template <typename Iterator, typename Deref>
00185 struct iterator_add_deref {
00186     GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
00187 
00188     typedef dereference_iterator_adaptor<Iterator, Deref> type;
00189 
00190     static type make(const Iterator& it, const Deref& d) { return type(it,d); }
00191 };
00192 
00195 template <typename Iterator, typename PREV_DEREF, typename Deref>
00196 struct iterator_add_deref<dereference_iterator_adaptor<Iterator, PREV_DEREF>,Deref> {
00197 //    GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
00198 
00199     typedef dereference_iterator_adaptor<Iterator, deref_compose<Deref,PREV_DEREF> > type;
00200 
00201     static type make(const dereference_iterator_adaptor<Iterator, PREV_DEREF>& it, const Deref& d) { 
00202         return type(it.base(),deref_compose<Deref,PREV_DEREF>(d,it.deref_fn())); 
00203     }
00204 };
00205 
00206 } }  // namespace boost::gil
00207 
00208 #endif

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google