Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const where possible #79

Merged
merged 8 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -109,7 +109,7 @@ namespace gcn
*
* @param message The error message of the exception.
*/
Exception(const std::string& message);
Exception(std::string message);

/**
* Constructor.
Expand All @@ -124,9 +124,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 @@ -71,22 +71,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 @@ -221,13 +221,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 @@ -249,7 +249,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 @@ -281,13 +281,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 @@ -388,7 +388,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 @@ -429,13 +429,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 @@ -465,7 +465,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 @@ -506,13 +506,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 = 0;
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 @@ -225,10 +225,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 @@ -381,10 +382,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 @@ -108,7 +108,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 @@ -194,16 +194,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 @@ -219,7 +218,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 @@ -289,7 +288,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 @@ -400,7 +399,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 @@ -472,10 +471,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;
midwan marked this conversation as resolved.
Show resolved Hide resolved

drawHLine(x1, y1, x2);
drawHLine(x1, y2, x2);
Expand Down Expand Up @@ -510,8 +509,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