1 /* 2 * Copyright (C) 2011 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef MARKS_H 21 #define MARKS_H 22 23 #include <QChar> 24 #include <QList> 25 26 // Class encapsulating a single mark 27 // Contains the line number the mark is identifying. 28 class Mark { 29 public: 30 Mark( int line ) { lineNumber_ = line; }; 31 32 // Accessors 33 int lineNumber() const { return lineNumber_; } 34 35 bool operator <( const Mark& other ) const 36 { return lineNumber_ < other.lineNumber_; } 37 38 private: 39 int lineNumber_; 40 }; 41 42 // A list of marks, i.e. line numbers optionally associated to an 43 // identifying character. 44 class Marks { 45 public: 46 // Create an empty Marks 47 Marks(); 48 49 // Add a mark at the given line, optionally identified by the given char 50 // If a mark for this char already exist, the previous one is replaced. 51 // It will happily add marks anywhere, even at stupid indexes. 52 void addMark( qint64 line, QChar mark = QChar() ); 53 // Get the (unique) mark identified by the passed char. 54 qint64 getMark( QChar mark ) const; 55 // Returns wheither the passed line has a mark on it. 56 bool isLineMarked( qint64 line ) const; 57 // Delete the mark identified by the passed char. 58 void deleteMark( QChar mark ); 59 // Delete the mark present on the passed line or do nothing if there is 60 // none. 61 void deleteMark( qint64 line ); 62 // Get the line marked identified by the index (in this list) passed. 63 qint64 getLineMarkedByIndex( int index ) const 64 { return marks_[index].lineNumber(); } 65 // Return the total number of marks 66 unsigned size() const 67 { return static_cast<unsigned>( marks_.size() ); } 68 // Completely clear the marks list. 69 void clear(); 70 71 // Iterator 72 // Provide a const_iterator for the client to iterate through the marks. 73 class const_iterator 74 : public std::iterator< 75 QList<Mark>::const_iterator::iterator_category, 76 QList<Mark>::const_iterator::value_type, 77 QList<Mark>::const_iterator::difference_type, 78 QList<Mark>::const_iterator::pointer, 79 QList<Mark>::const_iterator::reference> 80 { 81 public: 82 const_iterator( QList<Mark>::const_iterator iter ) 83 { internal_iter_ = iter; } 84 const_iterator( const const_iterator& original ) 85 { internal_iter_ = original.internal_iter_; } 86 const Mark& operator*() 87 { return *internal_iter_; } 88 const Mark* operator->() 89 { return &(*internal_iter_); } 90 bool operator!=( const const_iterator& other ) const 91 { return ( internal_iter_ != other.internal_iter_ ); } 92 const_iterator& operator++() 93 { ++internal_iter_ ; return *this; } 94 const_iterator& operator--() 95 { --internal_iter_ ; return *this; } 96 97 int operator-( const const_iterator& other ) const 98 { return ( internal_iter_ - other.internal_iter_ ); } 99 100 const_iterator operator+(int n) const 101 { return const_iterator(internal_iter_ + n); } 102 const_iterator& operator+=(int n) 103 { internal_iter_+=n ; return *this; } 104 const_iterator operator-(int n) const 105 { return const_iterator(internal_iter_ - n); } 106 const_iterator& operator-=(int n) 107 { internal_iter_-=n ; return *this; } 108 109 private: 110 QList<Mark>::const_iterator internal_iter_; 111 }; 112 113 const_iterator begin() const 114 { return const_iterator( marks_.begin() ); } 115 const_iterator end() const 116 { return const_iterator( marks_.end() ); } 117 118 private: 119 // List of marks. 120 QList<Mark> marks_; 121 }; 122 123 #endif 124