Skip to content

Commit

Permalink
Merge pull request #79 from kallisti5/const-fixes
Browse files Browse the repository at this point in the history
Use const where possible
  • Loading branch information
midwan authored Sep 12, 2024
2 parents ee1e32b + ef2dbb8 commit a46b5e1
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 140 deletions.
2 changes: 1 addition & 1 deletion include/guisan/actionevent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace gcn
* @param source The source widget of the event.
* @param id An identifier of the event.
*/
ActionEvent(Widget* source, const std::string& id);
ActionEvent(Widget* source, std::string id);

/**
* Destructor.
Expand Down
8 changes: 4 additions & 4 deletions include/guisan/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace gcn
*
* @param message The error message of the exception.
*/
Exception(const std::string& message);
Exception(std::string message);

/**
* Constructor.
Expand All @@ -123,9 +123,9 @@ namespace gcn
* @param line The line number in the source code where the exception
* occured.
*/
Exception(const std::string& message,
const std::string& function,
const std::string& filename,
Exception(std::string message,
std::string function,
std::string filename,
unsigned int line);

/**
Expand Down
2 changes: 1 addition & 1 deletion include/guisan/imagefont.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode
* with the font.
* @throws Exception when no glyph is found.
*/
Rectangle scanForGlyph(unsigned char glyph, int x, int y, const Color& separator);
Rectangle scanForGlyph(unsigned char glyph, int x, int y, const Color& separator) const;

/**
* Holds the glyphs areas in the image.
Expand Down
2 changes: 1 addition & 1 deletion include/guisan/widgets/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace gcn
*
* @param caption the caption of the Button.
*/
Button(const std::string& caption);
Button(std::string caption);

/**
* Sets the Button caption.
Expand Down
2 changes: 1 addition & 1 deletion include/guisan/widgets/dropdown.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace gcn
* @param selected the selected item as an index from the list model.
* @see getSelected
*/
void setSelected(int selected);
void setSelected(int selected) const;

/**
* Sets the list model to use when displaying the list.
Expand Down
4 changes: 2 additions & 2 deletions src/actionevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@

namespace gcn
{
ActionEvent::ActionEvent(Widget* source, const std::string& id)
ActionEvent::ActionEvent(Widget* source, std::string id)
:Event(source),
mId(id)
mId(std::move(id))
{

}
Expand Down
17 changes: 8 additions & 9 deletions src/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,21 @@ namespace gcn
{
}

Exception::Exception(const std::string& message)
Exception::Exception(std::string message)
: mFunction("?"),
mMessage(message),
mMessage(std::move(message)),
mFilename("?"),
mLine(0)
{

}

Exception::Exception(const std::string& message,
const std::string& function,
const std::string& filename,
Exception::Exception(std::string message,
std::string function,
std::string filename,
unsigned int line)
: mFunction(function),
mMessage(message),
mFilename(filename),
: mFunction(std::move(function)),
mMessage(std::move(message)),
mFilename(std::move(filename)),
mLine(line)
{
}
Expand Down
22 changes: 11 additions & 11 deletions src/focushandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ namespace gcn
{
mFocusedWidget = mWidgets.at(focusedWidget);

Event focusEvent(mFocusedWidget);
const Event focusEvent(mFocusedWidget);
distributeFocusGainedEvent(focusEvent);
}

if (focused >= 0)
{
Event focusEvent(mWidgets.at(focused));
const Event focusEvent(mWidgets.at(focused));
distributeFocusLostEvent(focusEvent);
}
}
Expand All @@ -248,7 +248,7 @@ namespace gcn
focusedWidget = i;
}
}
int focused = focusedWidget;
const int focused = focusedWidget;

// i is a counter that ensures that the following loop
// won't get stuck in an infinite loop
Expand Down Expand Up @@ -280,13 +280,13 @@ namespace gcn
if (focusedWidget >= 0)
{
mFocusedWidget = mWidgets.at(focusedWidget);
Event focusEvent(mFocusedWidget);
const Event focusEvent(mFocusedWidget);
distributeFocusGainedEvent(focusEvent);
}

if (focused >= 0)
{
Event focusEvent(mWidgets.at(focused));
const Event focusEvent(mWidgets.at(focused));
distributeFocusLostEvent(focusEvent);
}
}
Expand Down Expand Up @@ -387,7 +387,7 @@ namespace gcn
focusedWidget = i;
}
}
int focused = focusedWidget;
const int focused = focusedWidget;
bool done = false;

// i is a counter that ensures that the following loop
Expand Down Expand Up @@ -428,13 +428,13 @@ namespace gcn
if (focusedWidget >= 0)
{
mFocusedWidget = mWidgets.at(focusedWidget);
Event focusEvent(mFocusedWidget);
const Event focusEvent(mFocusedWidget);
distributeFocusGainedEvent(focusEvent);
}

if (focused >= 0)
{
Event focusEvent(mWidgets.at(focused));
const Event focusEvent(mWidgets.at(focused));
distributeFocusLostEvent(focusEvent);
}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ namespace gcn
focusedWidget = i;
}
}
int focused = focusedWidget;
const int focused = focusedWidget;
bool done = false;

// i is a counter that ensures that the following loop
Expand Down Expand Up @@ -505,13 +505,13 @@ namespace gcn
if (focusedWidget >= 0)
{
mFocusedWidget = mWidgets.at(focusedWidget);
Event focusEvent(mFocusedWidget);
const Event focusEvent(mFocusedWidget);
distributeFocusGainedEvent(focusEvent);
}

if (focused >= 0)
{
Event focusEvent(mWidgets.at(focused));
const Event focusEvent(mWidgets.at(focused));
distributeFocusLostEvent(focusEvent);
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/genericinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,12 @@ namespace gcn

KeyInput GenericInput::dequeueKeyInput()
{
KeyInput keyInput;

if (mKeyInputQueue.empty())
{
throw GCN_EXCEPTION("The queue is empty.");
}

keyInput = mKeyInputQueue.front();
const KeyInput keyInput = mKeyInputQueue.front();
mKeyInputQueue.pop();

return keyInput;
Expand All @@ -158,17 +156,15 @@ namespace gcn

MouseInput GenericInput::dequeueMouseInput()
{
MouseInput mouseInput;

if (mMouseInputQueue.empty())
{
throw GCN_EXCEPTION("The queue is empty.");
}

mouseInput = mMouseInputQueue.front();
const MouseInput mouseInput = mMouseInputQueue.front();
mMouseInputQueue.pop();

return mouseInput;
return mouseInput;
}

void GenericInput::_pollInput()
Expand Down
4 changes: 2 additions & 2 deletions src/imagefont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace gcn
mFilename = filename;
mImage = Image::load(filename, false);

Color separator = mImage->getPixel(0, 0);
const Color separator = mImage->getPixel(0, 0);

int i;
for (i=0; separator == mImage->getPixel(i, 0)
Expand Down Expand Up @@ -284,7 +284,7 @@ namespace gcn
return mGlyphSpacing;
}

Rectangle ImageFont::scanForGlyph(unsigned char glyph, int x, int y, const Color& separator)
Rectangle ImageFont::scanForGlyph(unsigned char glyph, int x, int y, const Color& separator) const
{
Color color;
do
Expand Down
17 changes: 9 additions & 8 deletions src/sdl/sdl2graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,11 @@ namespace gcn

if (mAlpha)
{
int x1 = area.x > top.x ? area.x : top.x;
int y1 = area.y > top.y ? area.y : top.y;
int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width;
int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height;
const int x1 = area.x > top.x ? area.x : top.x;
const int y1 = area.y > top.y ? area.y : top.y;
const int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width;
const int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height;

SDL_Rect rect;
rect.x = x1;
rect.y = y1;
Expand Down Expand Up @@ -377,10 +378,10 @@ namespace gcn

void SDL2Graphics::drawRectangle(const Rectangle& rectangle)
{
int x1 = rectangle.x;
int x2 = rectangle.x + rectangle.width - 1;
int y1 = rectangle.y;
int y2 = rectangle.y + rectangle.height - 1;
const int x1 = rectangle.x;
const int x2 = rectangle.x + rectangle.width - 1;
const int y1 = rectangle.y;
const int y2 = rectangle.y + rectangle.height - 1;

drawHLine(x1, y1, x2);
drawHLine(x1, y2, x2);
Expand Down
33 changes: 16 additions & 17 deletions src/sdl/sdlgraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace gcn
bool SDLGraphics::pushClipArea(Rectangle area)
{
SDL_Rect rect;
bool result = Graphics::pushClipArea(area);
const bool result = Graphics::pushClipArea(area);

const ClipRectangle& carea = mClipStack.top();
rect.x = carea.x;
Expand Down Expand Up @@ -193,16 +193,15 @@ namespace gcn

if (mAlpha)
{
int x1 = area.x > top.x ? area.x : top.x;
int y1 = area.y > top.y ? area.y : top.y;
int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width;
int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height;
int x, y;
const int x1 = area.x > top.x ? area.x : top.x;
const int y1 = area.y > top.y ? area.y : top.y;
const int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width;
const int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height;

SDL_LockSurface(mTarget);
for (y = y1; y < y2; y++)
for (int y = y1; y < y2; y++)
{
for (x = x1; x < x2; x++)
for (int x = x1; x < x2; x++)
{
SDLputPixelAlpha(mTarget, x, y, mColor);
}
Expand All @@ -217,7 +216,7 @@ namespace gcn
rect.w = area.width;
rect.h = area.height;

Uint32 color = SDL_MapRGBA(mTarget->format, mColor.r, mColor.g, mColor.b, mColor.a);
const Uint32 color = SDL_MapRGBA(mTarget->format, mColor.r, mColor.g, mColor.b, mColor.a);
SDL_FillRect(mTarget, &rect, color);
}
}
Expand Down Expand Up @@ -287,7 +286,7 @@ namespace gcn
x2 = top.x + top.width -1;
}

int bpp = mTarget->format->BytesPerPixel;
const int bpp = mTarget->format->BytesPerPixel;

SDL_LockSurface(mTarget);

Expand Down Expand Up @@ -398,7 +397,7 @@ namespace gcn
y2 = top.y + top.height - 1;
}

int bpp = mTarget->format->BytesPerPixel;
const int bpp = mTarget->format->BytesPerPixel;

SDL_LockSurface(mTarget);

Expand Down Expand Up @@ -470,10 +469,10 @@ namespace gcn

void SDLGraphics::drawRectangle(const Rectangle& rectangle)
{
int x1 = rectangle.x;
int x2 = rectangle.x + rectangle.width - 1;
int y1 = rectangle.y;
int y2 = rectangle.y + rectangle.height - 1;
const int x1 = rectangle.x;
const int x2 = rectangle.x + rectangle.width - 1;
const int y1 = rectangle.y;
const int y2 = rectangle.y + rectangle.height - 1;

drawHLine(x1, y1, x2);
drawHLine(x1, y2, x2);
Expand Down Expand Up @@ -508,8 +507,8 @@ namespace gcn

// Draw a line with Bresenham

int dx = ABS(x2 - x1);
int dy = ABS(y2 - y1);
const int dx = ABS(x2 - x1);
const int dy = ABS(y2 - y1);

if (dx > dy)
{
Expand Down
8 changes: 4 additions & 4 deletions src/widgets/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ namespace gcn
addFocusListener(this);
}

Button::Button(const std::string& caption)
: mCaption(caption),
Button::Button(std::string caption)
: mCaption(std::move(caption)),
mHasMouse(false),
mKeyPressed(false),
mMousePressed(false),
Expand Down Expand Up @@ -264,7 +264,7 @@ namespace gcn

void Button::keyPressed(KeyEvent& keyEvent)
{
Key key = keyEvent.getKey();
const Key key = keyEvent.getKey();

if (key.getValue() == Key::Enter
|| key.getValue() == Key::Space)
Expand All @@ -276,7 +276,7 @@ namespace gcn

void Button::keyReleased(KeyEvent& keyEvent)
{
Key key = keyEvent.getKey();
const Key key = keyEvent.getKey();

if ((key.getValue() == Key::Enter
|| key.getValue() == Key::Space)
Expand Down
Loading

0 comments on commit a46b5e1

Please sign in to comment.