iterator.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2009-2009: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENCE.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. +-----------------------------------------------------------------------------*/
  8. #ifndef BOOST_ICL_ITERATOR_HPP_JOFA_091003
  9. #define BOOST_ICL_ITERATOR_HPP_JOFA_091003
  10. #include <iterator>
  11. #include <boost/config/warning_disable.hpp>
  12. namespace boost{namespace icl
  13. {
  14. /** \brief Performes an addition using a container's memberfunction add, when operator= is called. */
  15. template<class ContainerT> class add_iterator
  16. {
  17. public:
  18. /// The container's type.
  19. typedef ContainerT container_type;
  20. typedef std::output_iterator_tag iterator_category;
  21. typedef void value_type;
  22. typedef void difference_type;
  23. typedef void pointer;
  24. typedef void reference;
  25. /** An add_iterator is constructed with a container and a position
  26. that has to be maintained. */
  27. add_iterator(ContainerT& cont, typename ContainerT::iterator iter)
  28. : _cont(&cont), _iter(iter) {}
  29. /** This assignment operator adds the \c value before the current position.
  30. It maintains it's position by incrementing after addition. */
  31. add_iterator& operator=(typename ContainerT::const_reference value)
  32. {
  33. _iter = icl::add(*_cont, _iter, value);
  34. if(_iter != _cont->end())
  35. ++_iter;
  36. return *this;
  37. }
  38. add_iterator& operator*() { return *this; }
  39. add_iterator& operator++() { return *this; }
  40. add_iterator& operator++(int){ return *this; }
  41. private:
  42. ContainerT* _cont;
  43. typename ContainerT::iterator _iter;
  44. };
  45. /** Function adder creates and initializes an add_iterator */
  46. template<class ContainerT, typename IteratorT>
  47. inline add_iterator<ContainerT> adder(ContainerT& cont, IteratorT iter_)
  48. {
  49. return add_iterator<ContainerT>(cont, typename ContainerT::iterator(iter_));
  50. }
  51. /** \brief Performes an insertion using a container's memberfunction add, when operator= is called. */
  52. template<class ContainerT> class insert_iterator
  53. {
  54. public:
  55. /// The container's type.
  56. typedef ContainerT container_type;
  57. typedef std::output_iterator_tag iterator_category;
  58. typedef void value_type;
  59. typedef void difference_type;
  60. typedef void pointer;
  61. typedef void reference;
  62. /** An insert_iterator is constructed with a container and a position
  63. that has to be maintained. */
  64. insert_iterator(ContainerT& cont, typename ContainerT::iterator iter)
  65. : _cont(&cont), _iter(iter) {}
  66. /** This assignment operator adds the \c value before the current position.
  67. It maintains it's position by incrementing after addition. */
  68. insert_iterator& operator=(typename ContainerT::const_reference value)
  69. {
  70. _iter = _cont->insert(_iter, value);
  71. if(_iter != _cont->end())
  72. ++_iter;
  73. return *this;
  74. }
  75. insert_iterator& operator*() { return *this; }
  76. insert_iterator& operator++() { return *this; }
  77. insert_iterator& operator++(int){ return *this; }
  78. private:
  79. ContainerT* _cont;
  80. typename ContainerT::iterator _iter;
  81. };
  82. /** Function inserter creates and initializes an insert_iterator */
  83. template<class ContainerT, typename IteratorT>
  84. inline insert_iterator<ContainerT> inserter(ContainerT& cont, IteratorT iter_)
  85. {
  86. return insert_iterator<ContainerT>(cont, typename ContainerT::iterator(iter_));
  87. }
  88. }} // namespace icl boost
  89. #endif // BOOST_ICL_ITERATOR_HPP_JOFA_091003