Add some missing includes for various standard library implementations.
[oota-llvm.git] / include / llvm / Support / LineIterator.h
1 //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_LINEITERATOR_H__
11 #define LLVM_SUPPORT_LINEITERATOR_H__
12
13 #include "llvm/ADT/StringRef.h"
14 #include <iterator>
15
16 namespace llvm {
17
18 class MemoryBuffer;
19
20 /// \brief A forward iterator which reads non-blank text lines from a buffer.
21 ///
22 /// This class provides a forward iterator interface for reading one line at
23 /// a time from a buffer. When default constructed the iterator will be the
24 /// "end" iterator.
25 ///
26 /// The iterator also is aware of what line number it is currently processing
27 /// and can strip comment lines given the comment-starting character.
28 ///
29 /// Note that this iterator requires the buffer to be nul terminated.
30 class line_iterator
31     : public std::iterator<std::forward_iterator_tag, StringRef> {
32   const MemoryBuffer *Buffer;
33   char CommentMarker;
34
35   unsigned LineNumber;
36   StringRef CurrentLine;
37
38 public:
39   /// \brief Default construct an "end" iterator.
40   line_iterator() : Buffer(nullptr) {}
41
42   /// \brief Construct a new iterator around some memory buffer.
43   explicit line_iterator(const MemoryBuffer &Buffer, char CommentMarker = '\0');
44
45   /// \brief Return true if we've reached EOF or are an "end" iterator.
46   bool is_at_eof() const { return !Buffer; }
47
48   /// \brief Return true if we're an "end" iterator or have reached EOF.
49   bool is_at_end() const { return is_at_eof(); }
50
51   /// \brief Return the current line number. May return any number at EOF.
52   int64_t line_number() const { return LineNumber; }
53
54   /// \brief Advance to the next (non-empty, non-comment) line.
55   line_iterator &operator++() {
56     advance();
57     return *this;
58   }
59   line_iterator operator++(int) {
60     line_iterator tmp(*this);
61     advance();
62     return tmp;
63   }
64
65   /// \brief Get the current line as a \c StringRef.
66   StringRef operator*() const { return CurrentLine; }
67   const StringRef *operator->() const { return &CurrentLine; }
68
69   friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
70     return LHS.Buffer == RHS.Buffer &&
71            LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
72   }
73
74   friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
75     return !(LHS == RHS);
76   }
77
78 private:
79   /// \brief Advance the iterator to the next line.
80   void advance();
81 };
82 }
83
84 #endif // LLVM_SUPPORT_LINEITERATOR_H__