xref: /glogg/src/marks.h (revision 71df3a3b2f2815033cc0516290223ff5cf9d547e) !
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:
Mark(int line)30bb02e0acSNicolas Bonnefon     Mark( int line ) { lineNumber_ = line; };
31bb02e0acSNicolas Bonnefon 
32bb02e0acSNicolas Bonnefon     // Accessors
lineNumber()33bb02e0acSNicolas Bonnefon     int lineNumber() const { return lineNumber_; }
34bb02e0acSNicolas Bonnefon 
35*3ff6c941SAnton Filimonov     bool operator <( const Mark& other ) const
36*3ff6c941SAnton Filimonov     { return lineNumber_ < other.lineNumber_; }
37*3ff6c941SAnton Filimonov 
38bb02e0acSNicolas Bonnefon   private:
39bb02e0acSNicolas Bonnefon     int lineNumber_;
40bb02e0acSNicolas Bonnefon };
41bb02e0acSNicolas Bonnefon 
42bb02e0acSNicolas Bonnefon // A list of marks, i.e. line numbers optionally associated to an
43bb02e0acSNicolas Bonnefon // identifying character.
44bb02e0acSNicolas Bonnefon class Marks {
45bb02e0acSNicolas Bonnefon   public:
46bb02e0acSNicolas Bonnefon     // Create an empty Marks
47bb02e0acSNicolas Bonnefon     Marks();
48bb02e0acSNicolas Bonnefon 
49bb02e0acSNicolas Bonnefon     // Add a mark at the given line, optionally identified by the given char
50bb02e0acSNicolas Bonnefon     // If a mark for this char already exist, the previous one is replaced.
51bb02e0acSNicolas Bonnefon     // It will happily add marks anywhere, even at stupid indexes.
52bb02e0acSNicolas Bonnefon     void addMark( qint64 line, QChar mark = QChar() );
53bb02e0acSNicolas Bonnefon     // Get the (unique) mark identified by the passed char.
54bb02e0acSNicolas Bonnefon     qint64 getMark( QChar mark ) const;
55bb02e0acSNicolas Bonnefon     // Returns wheither the passed line has a mark on it.
56bb02e0acSNicolas Bonnefon     bool isLineMarked( qint64 line ) const;
57bb02e0acSNicolas Bonnefon     // Delete the mark identified by the passed char.
58bb02e0acSNicolas Bonnefon     void deleteMark( QChar mark );
59bb02e0acSNicolas Bonnefon     // Delete the mark present on the passed line or do nothing if there is
60bb02e0acSNicolas Bonnefon     // none.
61bb02e0acSNicolas Bonnefon     void deleteMark( qint64 line );
62bb02e0acSNicolas Bonnefon     // Get the line marked identified by the index (in this list) passed.
getLineMarkedByIndex(int index)63bb02e0acSNicolas Bonnefon     qint64 getLineMarkedByIndex( int index ) const
64bb02e0acSNicolas Bonnefon     { return marks_[index].lineNumber(); }
65bb02e0acSNicolas Bonnefon     // Return the total number of marks
size()66481c483cSSergei Dyshel     unsigned size() const
67481c483cSSergei Dyshel     { return static_cast<unsigned>( marks_.size() ); }
68bb02e0acSNicolas Bonnefon     // Completely clear the marks list.
69bb02e0acSNicolas Bonnefon     void clear();
70bb02e0acSNicolas Bonnefon 
71bb02e0acSNicolas Bonnefon     // Iterator
72bb02e0acSNicolas Bonnefon     // Provide a const_iterator for the client to iterate through the marks.
73*3ff6c941SAnton Filimonov     class const_iterator
74*3ff6c941SAnton Filimonov             : public std::iterator<
75*3ff6c941SAnton Filimonov                 QList<Mark>::const_iterator::iterator_category,
76*3ff6c941SAnton Filimonov                 QList<Mark>::const_iterator::value_type,
77*3ff6c941SAnton Filimonov                 QList<Mark>::const_iterator::difference_type,
78*3ff6c941SAnton Filimonov                 QList<Mark>::const_iterator::pointer,
79*3ff6c941SAnton Filimonov                 QList<Mark>::const_iterator::reference>
80*3ff6c941SAnton Filimonov     {
81bb02e0acSNicolas Bonnefon       public:
const_iterator(QList<Mark>::const_iterator iter)82bb02e0acSNicolas Bonnefon         const_iterator( QList<Mark>::const_iterator iter )
83bb02e0acSNicolas Bonnefon         { internal_iter_ = iter; }
const_iterator(const const_iterator & original)84bb02e0acSNicolas Bonnefon         const_iterator( const const_iterator& original )
85bb02e0acSNicolas Bonnefon         { internal_iter_ = original.internal_iter_; }
86bb02e0acSNicolas Bonnefon         const Mark& operator*()
87bb02e0acSNicolas Bonnefon         { return *internal_iter_; }
88bb02e0acSNicolas Bonnefon         const Mark* operator->()
89bb02e0acSNicolas Bonnefon         { return &(*internal_iter_); }
90bb02e0acSNicolas Bonnefon         bool operator!=( const const_iterator& other ) const
91bb02e0acSNicolas Bonnefon         { return ( internal_iter_ != other.internal_iter_ ); }
92bb02e0acSNicolas Bonnefon         const_iterator& operator++()
93bb02e0acSNicolas Bonnefon         { ++internal_iter_ ; return *this; }
94*3ff6c941SAnton Filimonov         const_iterator& operator--()
95*3ff6c941SAnton Filimonov         { --internal_iter_ ; return *this; }
96*3ff6c941SAnton Filimonov 
97*3ff6c941SAnton Filimonov         int operator-( const const_iterator& other ) const
98*3ff6c941SAnton Filimonov         { return ( internal_iter_ - other.internal_iter_ ); }
99*3ff6c941SAnton Filimonov 
100*3ff6c941SAnton Filimonov         const_iterator operator+(int n) const
101*3ff6c941SAnton Filimonov         { return const_iterator(internal_iter_ + n); }
102*3ff6c941SAnton Filimonov         const_iterator& operator+=(int n)
103*3ff6c941SAnton Filimonov         { internal_iter_+=n ; return *this; }
104*3ff6c941SAnton Filimonov         const_iterator operator-(int n) const
105*3ff6c941SAnton Filimonov         { return const_iterator(internal_iter_ - n); }
106*3ff6c941SAnton Filimonov         const_iterator& operator-=(int n)
107*3ff6c941SAnton Filimonov         { internal_iter_-=n ; return *this; }
108bb02e0acSNicolas Bonnefon 
109bb02e0acSNicolas Bonnefon       private:
110bb02e0acSNicolas Bonnefon         QList<Mark>::const_iterator internal_iter_;
111bb02e0acSNicolas Bonnefon     };
112bb02e0acSNicolas Bonnefon 
begin()113bb02e0acSNicolas Bonnefon     const_iterator begin() const
114bb02e0acSNicolas Bonnefon     { return const_iterator( marks_.begin() ); }
end()115bb02e0acSNicolas Bonnefon     const_iterator end() const
116bb02e0acSNicolas Bonnefon     { return const_iterator( marks_.end() ); }
117bb02e0acSNicolas Bonnefon 
118bb02e0acSNicolas Bonnefon   private:
119bb02e0acSNicolas Bonnefon     // List of marks.
120bb02e0acSNicolas Bonnefon     QList<Mark> marks_;
121bb02e0acSNicolas Bonnefon };
122bb02e0acSNicolas Bonnefon 
123bb02e0acSNicolas Bonnefon #endif
124