Nvwa  1.1
istream_line_reader.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 
4 /*
5  * Copyright (C) 2017 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
53 #ifndef NVWA_ISTREAM_LINE_READER_H
54 #define NVWA_ISTREAM_LINE_READER_H
55 
56 #include <assert.h> // assert
57 #include <istream> // std::istream
58 #include <iterator> // std::input_iterator_tag
59 #include <string> // std::string
60 #include "_nvwa.h" // NVWA_NAMESPACE_*
61 #include "c++11.h" // _NOEXCEPT/_NULLPTR
62 
63 NVWA_NAMESPACE_BEGIN
64 
67 public:
73  class iterator // implements InputIterator
74  {
75  public:
76  typedef int difference_type;
77  typedef std::string value_type;
78  typedef value_type* pointer_type;
79  typedef value_type& reference;
80  typedef std::input_iterator_tag iterator_category;
81 
82  iterator() : _M_stream(_NULLPTR) {}
83  explicit iterator(std::istream& is) : _M_stream(&is)
84  {
85  ++*this;
86  }
87 
88  reference operator*()
89  {
90  assert(_M_stream != _NULLPTR);
91  return _M_line;
92  }
93  pointer_type operator->()
94  {
95  assert(_M_stream != _NULLPTR);
96  return &_M_line;
97  }
98  iterator& operator++()
99  {
100  assert(_M_stream != _NULLPTR);
101  getline(*_M_stream, _M_line);
102  if (!*_M_stream)
103  _M_stream = _NULLPTR;
104  return *this;
105  }
106  iterator operator++(int)
107  {
108  iterator temp(*this);
109  ++*this;
110  return temp;
111  }
112 
113  bool operator==(const iterator& rhs) const
114  {
115  return _M_stream == rhs._M_stream;
116  }
117  bool operator!=(const iterator& rhs) const
118  {
119  return !operator==(rhs);
120  }
121 
122  private:
123  std::istream* _M_stream;
124  std::string _M_line;
125  };
126 
127  explicit istream_line_reader(std::istream& is) _NOEXCEPT
128  : _M_stream(is)
129  {
130  }
131  iterator begin()
132  {
133  return iterator(_M_stream);
134  }
135  iterator end() const
136  {
137  return iterator();
138  }
139 
140 private:
141  std::istream& _M_stream;
142 };
143 
144 NVWA_NAMESPACE_END
145 
146 #endif // NVWA_ISTREAM_LINE_READER_H
Class to allow iteration over all lines from an input stream.
Definition: istream_line_reader.h:66
Iterator that contains the line content.
Definition: istream_line_reader.h:73
C++11 feature detection macros and workarounds.
Common definitions for preprocessing.