PolyBoRi
TransformedIterator.h
Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //*****************************************************************************
00014 //*****************************************************************************
00015 
00016 #ifndef polybori_iterators_TransformedIterator_h_
00017 #define polybori_iterators_TransformedIterator_h_
00018 
00019 // include basic definitions
00020 #include <polybori/pbori_defs.h>
00021 
00022 #include <boost/iterator/iterator_facade.hpp>
00023 #include <iterator>
00024 
00025 BEGIN_NAMESPACE_PBORI
00026 
00027 template <class UnaryFunc>
00028 class UnaryOperatorTraits {
00029 public: 
00030   typedef typename UnaryFunc::result_type result_type;
00031   typedef typename UnaryFunc::argument_type argument_type;
00032 };
00033 
00034 template <class ResultType, class ArgType>
00035 class UnaryOperatorTraits<ResultType (*)(ArgType)> {
00036  public:
00037   typedef ResultType result_type;
00038   typedef ArgType argument_type;
00039 };
00040 
00047 template <class IteratorType, class OperatorType>
00048 class TransformedIterator:
00049   public boost::iterator_facade<
00050   TransformedIterator<IteratorType, OperatorType>,
00051   typename UnaryOperatorTraits<OperatorType>::result_type,
00052   std::forward_iterator_tag,
00053   typename UnaryOperatorTraits<OperatorType>::result_type > {
00054 
00055   typedef IteratorType iterator;
00056   typedef TransformedIterator self;
00057 
00058 public:
00059   typedef OperatorType operator_type;
00060   typedef typename operator_type::result_type result_type;
00061 
00063   TransformedIterator(iterator iter, const operator_type& func):
00064     m_iter(iter), m_func(func) { }
00065 
00066 
00068   void increment() { ++m_iter; }
00069 
00071   result_type dereference() const { return m_func(*m_iter); }
00072 
00074   bool equal(const self& rhs) const {  return m_iter == rhs.m_iter; }
00075 
00076 private:
00077   iterator m_iter;
00078   const operator_type& m_func;
00079 };
00080 
00081 END_NAMESPACE_PBORI
00082 
00083 #endif /* polybori_TransformedIterator_h_ */