From cd15371fcf9ec37421273e64a5058caeb255503f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 11 Mar 2016 12:44:00 +1100 Subject: [PATCH] iterator: add infix_iterator --- iterator.hpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/iterator.hpp b/iterator.hpp index 22e831a3..82a8a640 100644 --- a/iterator.hpp +++ b/iterator.hpp @@ -67,4 +67,45 @@ class referencing_iterator { }; + +namespace util { + template < + typename T, + class CharT = char, + class Traits = std::char_traits + > + class infix_iterator : public std::iterator { + public: + using char_type = CharT; + using traits_type = Traits; + using ostream_type = std::basic_ostream; + + infix_iterator (ostream_type& _output, const CharT *_delimiter): + m_output (_output), + m_delimiter (_delimiter) + { ; } + + infix_iterator& + operator= (const T &value) + { + if (!m_first) + m_output << m_delimiter; + + m_output << value; + m_first = false; + + return *this; + } + + infix_iterator& operator* (void) { return *this; } + infix_iterator& operator++ (void) { return *this; } + infix_iterator& operator++ (int) { return *this; } + + private: + bool m_first = true; + ostream_type &m_output; + const CharT *m_delimiter; + }; +} + #endif