Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iterator>
- #include <istream>
- #include <string>
- class StreamedLines
- {
- public:
- struct Iterator
- {
- public:
- using iterator_category = std::input_iterator_tag;
- using difference_type = void;
- using value_type = std::string;
- using reference = std::string&;
- using pointer = std::string*;
- Iterator() = default;
- Iterator(StreamedLines *pStreamHolder)
- : m_pStreamHolder(pStreamHolder)
- {
- std::getline(m_pStreamHolder->m_in, m_currentLine);
- }
- Iterator(const Iterator &other) = default;
- Iterator(Iterator &&other)
- {
- m_pStreamHolder = other.m_pStreamHolder;
- m_currentLine = move(other.m_currentLine);
- }
- Iterator &operator=(const Iterator &other) = default;
- Iterator &operator=(Iterator &&other)
- {
- m_pStreamHolder = other.m_pStreamHolder;
- m_currentLine = move(other.m_currentLine);
- return *this;
- }
- std::string &operator*() { return m_currentLine; }
- std::string *operator->() { return &m_currentLine; }
- Iterator &operator++()
- {
- std::getline(m_pStreamHolder->m_in, m_currentLine);
- return *this;
- }
- Iterator operator++(int)
- {
- auto prev = *this;
- ++*this;
- return prev;
- }
- bool operator==(const Iterator &other)
- {
- return m_pStreamHolder == other.m_pStreamHolder
- || !m_pStreamHolder && other.m_pStreamHolder->m_in.eof()
- || !other.m_pStreamHolder && m_pStreamHolder->m_in.eof();
- }
- bool operator!=(const Iterator &other)
- {
- return !(*this == other);
- }
- private:
- StreamedLines *m_pStreamHolder;
- std::string m_currentLine;
- };
- StreamedLines(std::istream &in)
- : m_in(in)
- {
- }
- Iterator begin() { return Iterator(this); }
- Iterator end() { return Iterator(); }
- private:
- std::istream &m_in;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement