This commit is contained in:
50
include/sp/protocol/message/OstreamFieldIterator.h
Normal file
50
include/sp/protocol/message/OstreamFieldIterator.h
Normal file
@@ -0,0 +1,50 @@
|
||||
// infix_iterator.h
|
||||
//
|
||||
// Lifted from Jerry Coffin's 's prefix_ostream_iterator
|
||||
#pragma once
|
||||
|
||||
#include <iterator>
|
||||
#include <ostream>
|
||||
#include <sp/protocol/Field.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <class T, class charT = char, class traits = std::char_traits<charT>>
|
||||
class OstreamFieldIterator : public std::iterator<std::output_iterator_tag, void, void, void, void> {
|
||||
private:
|
||||
std::basic_ostream<charT, traits>* m_Os;
|
||||
std::string m_Delimiter;
|
||||
bool m_FirstElem;
|
||||
|
||||
public:
|
||||
typedef charT char_type;
|
||||
typedef traits traits_type;
|
||||
typedef std::basic_ostream<charT, traits> ostream_type;
|
||||
OstreamFieldIterator(ostream_type& a_Stream) : m_Os(&a_Stream), m_Delimiter(0), m_FirstElem(true) {}
|
||||
OstreamFieldIterator(ostream_type& a_Stream, std::string&& a_Delimiter) :
|
||||
m_Os(&a_Stream), m_Delimiter(std::move(a_Delimiter)), m_FirstElem(true) {}
|
||||
|
||||
auto& operator=(const T& item) {
|
||||
// Here's the only real change from ostream_iterator:
|
||||
// Normally, the '*m_Os << item;' would come before the 'if'.
|
||||
if (!m_FirstElem && !m_Delimiter.empty())
|
||||
*m_Os << m_Delimiter;
|
||||
*m_Os << sp::PrintableField<T>(item);
|
||||
m_FirstElem = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto& operator*() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto& operator++() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto& operator++(int) {
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
Reference in New Issue
Block a user