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