Skip to content

Commit dcb1bad

Browse files
committed
An effort at improvements towards const correctness. Tried not to make any changes which might impact existing code. Corrected some little things (misspellings, mostly). Added Q_ASSERT_X to some constructors to ensure that pointers were valid. Added 'CONFIG += warn_on' to project file.
1 parent 34f5c78 commit dcb1bad

10 files changed

Lines changed: 124 additions & 95 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/**/__pycache__
33
/.venv
44
/.vscode
5+
/src/qhexedit.pro.user

src/chunks.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool Chunks::setIODevice(QIODevice &ioDevice)
6262

6363
// ***************************************** Getting data out of Chunks
6464

65-
QByteArray Chunks::data(qint64 pos, qint64 maxSize, QByteArray *highlighted)
65+
QByteArray Chunks::data(qint64 pos, qint64 maxSize, QByteArray *highlighted) const
6666
{
6767
qint64 ioDelta = 0;
6868
int chunkIdx = 0;
@@ -147,7 +147,7 @@ QByteArray Chunks::data(qint64 pos, qint64 maxSize, QByteArray *highlighted)
147147
return buffer;
148148
}
149149

150-
bool Chunks::write(QIODevice &iODevice, qint64 pos, qint64 count)
150+
bool Chunks::write(QIODevice &iODevice, qint64 pos, qint64 count) const
151151
{
152152
if (count == -1)
153153
count = _size;
@@ -176,7 +176,7 @@ void Chunks::setDataChanged(qint64 pos, bool dataChanged)
176176
_chunks[chunkIdx].dataChanged[(int)posInBa] = char(dataChanged);
177177
}
178178

179-
bool Chunks::dataChanged(qint64 pos)
179+
bool Chunks::dataChanged(qint64 pos) const
180180
{
181181
foreach (Chunk chunk, _chunks)
182182
{
@@ -191,7 +191,7 @@ bool Chunks::dataChanged(qint64 pos)
191191

192192
// ***************************************** Search API
193193

194-
qint64 Chunks::indexOf(const QByteArray &ba, qint64 from)
194+
qint64 Chunks::indexOf(const QByteArray &ba, qint64 from) const
195195
{
196196
qint64 result = -1;
197197
QByteArray buffer;
@@ -206,7 +206,7 @@ qint64 Chunks::indexOf(const QByteArray &ba, qint64 from)
206206
return result;
207207
}
208208

209-
qint64 Chunks::lastIndexOf(const QByteArray &ba, qint64 from)
209+
qint64 Chunks::lastIndexOf(const QByteArray &ba, qint64 from) const
210210
{
211211
qint64 result = -1;
212212
QByteArray buffer;
@@ -276,22 +276,22 @@ bool Chunks::removeAt(qint64 pos)
276276

277277
// ***************************************** Utility functions
278278

279-
char Chunks::operator[](qint64 pos)
279+
char Chunks::operator[](qint64 pos) const
280280
{
281281
return data(pos, 1).at(0);
282282
}
283283

284-
qint64 Chunks::pos()
284+
qint64 Chunks::pos() const
285285
{
286286
return _pos;
287287
}
288288

289-
qint64 Chunks::size()
289+
qint64 Chunks::size() const
290290
{
291291
return _size;
292292
}
293293

294-
int Chunks::getChunkIndex(qint64 absPos)
294+
int Chunks::getChunkIndex(qint64 absPos) const
295295
{
296296
// This routine checks, if there is already a copied chunk available. If os, it
297297
// returns a reference to it. If there is no copied chunk available, original

src/chunks.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,40 @@ class Chunks: public QObject
4949
Q_OBJECT
5050
public:
5151
// Constructors and file settings
52-
Chunks(QObject *parent);
53-
Chunks(QIODevice &ioDevice, QObject *parent);
52+
explicit Chunks(QObject *parent = nullptr);
53+
Chunks(QIODevice &ioDevice, QObject *parent = nullptr);
5454
bool setIODevice(QIODevice &ioDevice);
5555

5656
// Getting data out of Chunks
57-
QByteArray data(qint64 pos=0, qint64 count=-1, QByteArray *highlighted=0);
58-
bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1);
57+
QByteArray data(qint64 pos=0, qint64 count=-1, QByteArray *highlighted=nullptr) const;
58+
bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1) const;
5959

6060
// Set and get highlighting infos
6161
void setDataChanged(qint64 pos, bool dataChanged);
62-
bool dataChanged(qint64 pos);
62+
bool dataChanged(qint64 pos) const;
6363

6464
// Search API
65-
qint64 indexOf(const QByteArray &ba, qint64 from);
66-
qint64 lastIndexOf(const QByteArray &ba, qint64 from);
65+
qint64 indexOf(const QByteArray &ba, qint64 from) const;
66+
qint64 lastIndexOf(const QByteArray &ba, qint64 from) const;
6767

6868
// Char manipulations
6969
bool insert(qint64 pos, char b);
7070
bool overwrite(qint64 pos, char b);
7171
bool removeAt(qint64 pos);
7272

7373
// Utility functions
74-
char operator[](qint64 pos);
75-
qint64 pos();
76-
qint64 size();
74+
char operator[](qint64 pos) const;
75+
qint64 pos() const;
76+
qint64 size() const;
7777

7878

7979
private:
80-
int getChunkIndex(qint64 absPos);
80+
int getChunkIndex(qint64 absPos) const;
8181

8282
QIODevice * _ioDevice;
8383
qint64 _pos;
8484
qint64 _size;
85-
QList<Chunk> _chunks;
85+
mutable QList<Chunk> _chunks;
8686

8787
#ifdef MODUL_TEST
8888
public:

src/color_manager.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ ColoredArea::ColoredArea(qint64 posStart, qint64 posEnd, QPen pen, QBrush backgr
4747
_areaStyle = background;
4848
}
4949

50-
QPen ColoredArea::fontPen()
50+
QPen ColoredArea::fontPen() const
5151
{
5252
return _fontColor;
5353
}
5454

55-
QColor ColoredArea::fontColor()
55+
QColor ColoredArea::fontColor() const
5656
{
5757
return _fontColor.color();
5858
}
@@ -62,12 +62,12 @@ void ColoredArea::setFontColor(QColor color)
6262
_fontColor = QPen(color);
6363
}
6464

65-
QColor ColoredArea::areaColor()
65+
QColor ColoredArea::areaColor() const
6666
{
6767
return _areaStyle.color();
6868
}
6969

70-
QBrush ColoredArea::areaStyle()
70+
QBrush ColoredArea::areaStyle() const
7171
{
7272
return _areaStyle;
7373
}
@@ -82,21 +82,21 @@ void ColoredArea::setAreaStyle(QBrush backround)
8282
_areaStyle = backround;
8383
}
8484

85-
qint64 ColoredArea::posStart()
85+
qint64 ColoredArea::posStart() const
8686
{
8787
return _posStart;
88-
};
88+
}
8989

90-
qint64 ColoredArea::posEnd()
90+
qint64 ColoredArea::posEnd() const
9191
{
9292
return _posEnd;
93-
};
93+
}
9494

9595
void ColoredArea::setRange(qint64 posStart, qint64 posEnd)
9696
{
9797
_posStart = posStart;
9898
_posEnd = posEnd;
99-
};
99+
}
100100

101101
void ColoredArea::clear()
102102
{
@@ -111,7 +111,7 @@ ColorManager::ColorManager()
111111
{
112112
QPalette palette = qApp->palette();
113113
setPalette(palette);
114-
};
114+
}
115115

116116
void ColorManager::setPalette(const QPalette &palette)
117117
{
@@ -123,7 +123,7 @@ void ColorManager::setPalette(const QPalette &palette)
123123
}
124124

125125
// read only, copy of relevant ColoredArea is returned: you can't change anything
126-
ColoredArea ColorManager::markedArea(qint64 pos, Area area, Chunks *chunks)
126+
ColoredArea ColorManager::markedArea(qint64 pos, Area area, Chunks *chunks) const
127127
{
128128
// prio 1 selection
129129
if (pos >= _selection.posStart() && pos < _selection.posEnd())
@@ -145,9 +145,9 @@ ColoredArea ColorManager::markedArea(qint64 pos, Area area, Chunks *chunks)
145145
}
146146
// nothing found -> standard colors
147147
return this->notMarked(area);
148-
};
148+
}
149149

150-
ColoredArea& ColorManager::notMarked(Area area)
150+
const ColoredArea& ColorManager::notMarked(Area area) const
151151
{
152152
switch (area) {
153153
case Area::Address:
@@ -163,15 +163,25 @@ ColoredArea& ColorManager::notMarked(Area area)
163163
return _hex; // should never happen
164164
}
165165

166-
ColoredArea& ColorManager::selection()
166+
const ColoredArea &ColorManager::selection() const
167+
{
168+
return _selection;
169+
}
170+
171+
ColoredArea &ColorManager::selection()
167172
{
168173
return _selection;
169-
};
174+
}
175+
176+
const ColoredArea &ColorManager::highlighting() const
177+
{
178+
return _highlighting;
179+
}
170180

171-
ColoredArea& ColorManager::highlighting()
181+
ColoredArea &ColorManager::highlighting()
172182
{
173183
return _highlighting;
174-
};
184+
}
175185

176186
void ColorManager::addUserArea(qint64 posStart, qint64 posEnd, QColor fontColor, QBrush areaStyle)
177187
{
@@ -182,4 +192,4 @@ void ColorManager::addUserArea(qint64 posStart, qint64 posEnd, QColor fontColor,
182192
void ColorManager::clearUserAreas()
183193
{
184194
_userAreas.clear();
185-
}
195+
}

src/color_manager.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,40 @@ enum Area {
4747
class ColoredArea
4848
{
4949
public:
50-
// Cunstructors
50+
// Constructors
5151
ColoredArea();
5252
ColoredArea(QPen pen, QBrush background);
5353
ColoredArea(qint64 posStart, qint64 posEnd, QPen pen, QBrush background);
54+
// needs copy constructor and assignment operator:
55+
ColoredArea(const ColoredArea& other)
56+
: _posStart (other._posStart)
57+
, _posEnd (other._posEnd)
58+
, _fontColor(other._fontColor)
59+
, _areaStyle(other._areaStyle) {;}
60+
ColoredArea& operator=(const ColoredArea& other) {
61+
if (this != &other) {
62+
_posStart = other._posStart ;
63+
_posEnd = other._posEnd ;
64+
_fontColor = other._fontColor;
65+
_areaStyle = other._areaStyle;
66+
}
67+
return *this;
68+
}
5469

5570
// Property to set/get font color
56-
QColor fontColor();
71+
QColor fontColor() const;
5772
void setFontColor(QColor color);
5873

5974
// Property to set/get area style
60-
QColor areaColor();
61-
QBrush areaStyle();
75+
QColor areaColor() const;
76+
QBrush areaStyle() const;
6277
void setAreaColor(QColor color);
6378
void setAreaStyle(QBrush background);
6479

6580
// other Methods to acces and set internal data
66-
QPen fontPen();
67-
qint64 posStart();
68-
qint64 posEnd();
81+
QPen fontPen() const;
82+
qint64 posStart() const;
83+
qint64 posEnd() const;
6984
void setRange(qint64 posStart, qint64 posEnd);
7085
void clear();
7186

@@ -87,15 +102,17 @@ class ColorManager
87102
void setPalette(const QPalette &palette);
88103

89104
// Method returns color definitions for data at position pos in area area
90-
ColoredArea markedArea(qint64 pos, Area area, Chunks *chunks);
105+
ColoredArea markedArea(qint64 pos, Area area, Chunks *chunks) const;
91106

92-
// Method returns standard collors (without marking)
93-
ColoredArea& notMarked(Area);
107+
// Method returns standard colors (without marking)
108+
const ColoredArea &notMarked(Area) const;
94109

95110
// Get the selection color definitions
111+
const ColoredArea& selection() const;
96112
ColoredArea& selection();
97113

98114
// Get the highlighting color definitions
115+
const ColoredArea& highlighting() const;
99116
ColoredArea& highlighting();
100117

101118
// Add a user defined area
@@ -116,4 +133,4 @@ class ColorManager
116133

117134
/** \endcond docNever */
118135

119-
#endif // COLOR_MANAGER_H
136+
#endif // COLOR_MANAGER_H

src/commands.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CharCommand : public QUndoCommand
2727
enum CCmd {insert, removeAt, overwrite};
2828

2929
CharCommand(Chunks * chunks, CCmd cmd, qint64 charPos, char newChar,
30-
QUndoCommand *parent=0);
30+
QUndoCommand *parent=nullptr);
3131

3232
void undo();
3333
void redo();
@@ -52,6 +52,7 @@ CharCommand::CharCommand(Chunks * chunks, CCmd cmd, qint64 charPos, char newChar
5252
, _oldChar('\0')
5353
, _cmd(cmd)
5454
{
55+
Q_ASSERT_X(chunks, __func__, "The argument 'chunks' was nullptr.");
5556
}
5657

5758
bool CharCommand::mergeWith(const QUndoCommand *command)
@@ -112,8 +113,8 @@ void CharCommand::redo()
112113
UndoStack::UndoStack(Chunks * chunks, QObject * parent)
113114
: QUndoStack(parent)
114115
{
116+
Q_ASSERT_X(chunks, __func__, "The argument 'chunks' was nullptr.");
115117
_chunks = chunks;
116-
_parent = parent;
117118
this->setUndoLimit(1000);
118119
}
119120

src/commands.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class UndoStack : public QUndoStack
4848
Q_OBJECT
4949

5050
public:
51-
UndoStack(Chunks *chunks, QObject * parent=0);
51+
UndoStack(Chunks *chunks, QObject * parent=nullptr);
5252
void insert(qint64 pos, char c);
5353
void insert(qint64 pos, const QByteArray &ba);
5454
void removeAt(qint64 pos, qint64 len=1);
@@ -57,7 +57,6 @@ class UndoStack : public QUndoStack
5757

5858
private:
5959
Chunks * _chunks;
60-
QObject * _parent;
6160
};
6261

6362
/** \endcond docNever */

0 commit comments

Comments
 (0)