From a416368e0d5ec61a17a3642777cbe535f99e46d6 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sun, 22 Jan 2023 17:12:56 -0500 Subject: [PATCH] const-correctness in dialogxml (dialogs) --- src/dialogxml/dialogs/dialog.cpp | 14 +++++++++----- src/dialogxml/dialogs/dialog.hpp | 14 ++++++++------ src/dialogxml/dialogs/pictchoice.cpp | 6 +++--- src/dialogxml/dialogs/pictchoice.hpp | 6 +++--- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/dialogxml/dialogs/dialog.cpp b/src/dialogxml/dialogs/dialog.cpp index 97074851..6b590a25 100644 --- a/src/dialogxml/dialogs/dialog.cpp +++ b/src/dialogxml/dialogs/dialog.cpp @@ -740,7 +740,7 @@ void cDialog::setBg(short n){ bg = n; } -short cDialog::getBg() { +short cDialog::getBg() const { return bg; } @@ -748,7 +748,7 @@ void cDialog::setDefTextClr(sf::Color clr){ defTextClr = clr; } -sf::Color cDialog::getDefTextClr() { +sf::Color cDialog::getDefTextClr() const { return defTextClr; } @@ -767,7 +767,7 @@ void cDialog::untoast() { this->getControl(currentFocus).triggerFocusHandler(*this, currentFocus, false); } -bool cDialog::accepted() { +bool cDialog::accepted() const { return didAccept; } @@ -1057,6 +1057,10 @@ cControl& cDialog::operator[](std::string id){ return getControl(id); } +const cControl& cDialog::operator[](std::string id) const { + return const_cast(*this).getControl(id); +} + cControl& cDialog::getControl(std::string id) { ctrlIter iter = controls.find(id); if(iter != controls.end()) return *(iter->second); @@ -1073,8 +1077,8 @@ cControl& cDialog::getControl(std::string id) { throw std::invalid_argument(id + " does not exist in dialog " + fname); } -bool cDialog::hasControl(std::string id) { - ctrlIter iter = controls.find(id); +bool cDialog::hasControl(std::string id) const { + auto iter = controls.find(id); if(iter != controls.end()) return true; iter = controls.begin(); diff --git a/src/dialogxml/dialogs/dialog.hpp b/src/dialogxml/dialogs/dialog.hpp index 88a8451a..7404dce4 100644 --- a/src/dialogxml/dialogs/dialog.hpp +++ b/src/dialogxml/dialogs/dialog.hpp @@ -111,7 +111,7 @@ public: /// @tparam type The result type. /// @throw boost::bad_any_cast if the provided result type is different from the type set by getResult(). /// @return The dialog's result. - template type getResult(){ + template type getResult() const { return boost::any_cast(result); } /// Set the result of the dialog. @@ -134,13 +134,13 @@ public: void setBg(short n); /// Get the background pattern of the dialog. /// @return The numeric index of the background pattern. - short getBg(); + short getBg() const; /// Set the default text colour applied to new dialogs when loading from a file. /// @param clr The text colour. void setDefTextClr(sf::Color clr); /// Get the default text colour applied to new dialogs when loading from a file. /// @return The text colour. - sf::Color getDefTextClr(); + sf::Color getDefTextClr() const; /// Set the focused text field. /// @param newFocus A pointer to the text field to receive focus. /// @param force If true, the change will be forced. @@ -168,11 +168,11 @@ public: void untoast(); /// Determine how the dialog exited. /// @return the argument passed to toast() when the dialog was closed - bool accepted(); + bool accepted() const; /// Check if a control exists with a given ID. /// @param id The unique key of the control. /// @return true if it exists. - bool hasControl(std::string id); + bool hasControl(std::string id) const; /// Get a reference to a control. /// @param id The unique key of the control. /// @throw std::invalid_argument if the control does not exist. @@ -180,6 +180,8 @@ public: cControl& getControl(std::string id); /// @copydoc getControl() cControl& operator[](std::string id); + /// @copydoc getControl() + const cControl& operator[](std::string id) const; /// Recalculate the dialog's bounding rect. /// Call this after adding controls to the dialog to ensure that the control is within the bounding rect. void recalcRect(); @@ -208,7 +210,7 @@ public: void attachFocusHandlers(std::function handler, std::vector controls); /// Get the bounding rect of the dialog. /// @return The dialog's bounding rect. - rectangle getBounds() {return winRect;} + rectangle getBounds() const {return winRect;} /// Send keyboard input to the frontmost dialog. /// Currently, only text edit fields will respond to this. /// @return true if there was a dialog opened to send to. diff --git a/src/dialogxml/dialogs/pictchoice.cpp b/src/dialogxml/dialogs/pictchoice.cpp index 19775a41..ef590529 100644 --- a/src/dialogxml/dialogs/pictchoice.cpp +++ b/src/dialogxml/dialogs/pictchoice.cpp @@ -146,14 +146,14 @@ bool cPictChoice::onSelect(bool losing) { return true; } -pic_num_t cPictChoice::getPicChosen() { +pic_num_t cPictChoice::getPicChosen() const { return dlg.getResult>().first; } -ePicType cPictChoice::getPicChosenType() { +ePicType cPictChoice::getPicChosenType() const { return dlg.getResult>().second; } -size_t cPictChoice::getSelected() { +size_t cPictChoice::getSelected() const { return cur; } diff --git a/src/dialogxml/dialogs/pictchoice.hpp b/src/dialogxml/dialogs/pictchoice.hpp index 81b3ef19..503f1049 100644 --- a/src/dialogxml/dialogs/pictchoice.hpp +++ b/src/dialogxml/dialogs/pictchoice.hpp @@ -68,13 +68,13 @@ public: bool show(size_t cur_sel); /// Get the chosen icon. /// @return The number of the chosen icon. - pic_num_t getPicChosen(); + pic_num_t getPicChosen() const; /// Get the chosen icon. /// @return The type of the chosen icon. - ePicType getPicChosenType(); + ePicType getPicChosenType() const; /// Get the index of the selected icon in the original list. /// @return The index - size_t getSelected(); + size_t getSelected() const; }; #endif