diff options
author | Andrzej Rybczak <electricityispower@gmail.com> | 2014-10-31 15:30:54 +0100 |
---|---|---|
committer | Andrzej Rybczak <electricityispower@gmail.com> | 2014-10-31 15:30:54 +0100 |
commit | b265d56cdf18fb805fc9d22b43b88147519709d5 (patch) | |
tree | 57d707b64e2516244d67125c22f4a04c53316297 | |
parent | fa1cd965faf843077686b051c79c49fdb7dfbef6 (diff) |
window: add ScopedStringHelper and use it where appropriate
-rw-r--r-- | src/actions.cpp | 10 | ||||
-rw-r--r-- | src/status.cpp | 3 | ||||
-rw-r--r-- | src/window.h | 27 |
3 files changed, 31 insertions, 9 deletions
diff --git a/src/actions.cpp b/src/actions.cpp index 6d580e04..351c4b7a 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -842,10 +842,11 @@ void ExecuteCommand::run() std::string cmd_name; { Statusbar::ScopedLock lock; + NC::Window::ScopedStringHelper helper(*wFooter, + Statusbar::Helpers::TryExecuteImmediateCommand() + ); Statusbar::put() << NC::Format::Bold << ":" << NC::Format::NoBold; - wFooter->setGetStringHelper(Statusbar::Helpers::TryExecuteImmediateCommand()); cmd_name = wFooter->getString(); - wFooter->setGetStringHelper(Statusbar::Helpers::getString); } auto cmd = Bindings.findCommand(cmd_name); @@ -1882,10 +1883,11 @@ void ApplyFilter::run() try { Statusbar::ScopedLock lock; + NC::Window::ScopedStringHelper helper(*wFooter, + Statusbar::Helpers::ApplyFilterImmediately(f, filter) + ); Statusbar::put() << NC::Format::Bold << "Apply filter: " << NC::Format::NoBold; - wFooter->setGetStringHelper(Statusbar::Helpers::ApplyFilterImmediately(f, filter)); wFooter->getString(filter); - wFooter->setGetStringHelper(Statusbar::Helpers::getString); } catch (NC::PromptAborted &) { diff --git a/src/status.cpp b/src/status.cpp index a6170e2b..bb2e2656 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -167,7 +167,7 @@ void Status::handleServerError(MPD::ServerError &e) Statusbar::printf("MPD: %1%", e.what()); if (e.code() == MPD_SERVER_ERROR_PERMISSION) { - wFooter->setGetStringHelper(nullptr); + NC::Window::ScopedStringHelper helper(*wFooter, nullptr); Statusbar::put() << "Password: "; Mpd.SetPassword(wFooter->getString(0, true)); try { @@ -176,7 +176,6 @@ void Status::handleServerError(MPD::ServerError &e) } catch (MPD::ServerError &e_prim) { handleServerError(e_prim); } - wFooter->setGetStringHelper(Statusbar::Helpers::getString); } else if (e.code() == MPD_SERVER_ERROR_NO_EXIST && myScreen == myBrowser) { diff --git a/src/window.h b/src/window.h index 6e9dcc10..8d61e47e 100644 --- a/src/window.h +++ b/src/window.h @@ -188,6 +188,23 @@ struct XY /// Main class of NCurses namespace, used as base for other specialized windows struct Window { + /// Sets helper to a specific value for the current scope + struct ScopedStringHelper + { + template <typename HelperT> + ScopedStringHelper(Window &w, HelperT &&helper) noexcept + : m_w(w), m_helper(std::move(w.m_get_string_helper)) { + m_w.m_get_string_helper = std::forward<HelperT>(helper); + } + ~ScopedStringHelper() noexcept { + m_w.m_get_string_helper = std::move(m_helper); + } + + private: + Window &m_w; + GetStringHelper m_helper; + }; + Window() : m_window(0), m_border_window(0) { } /// Constructs an empty window with given parameters @@ -254,11 +271,11 @@ struct Window /// @see setGetStringHelper() /// @see SetTimeout() /// @see CreateHistory() - std::string getString(const std::string &base, size_t width = 0, bool encrypted = 0); + std::string getString(const std::string &base, size_t width = 0, bool encrypted = false); /// Wrapper for above function that doesn't take base string (it will be empty). /// Taken parameters are the same as for above. - std::string getString(size_t width = 0, bool encrypted = 0) + std::string getString(size_t width = 0, bool encrypted = false) { return getString("", width, encrypted); } @@ -285,7 +302,11 @@ struct Window /// Sets helper function used in getString() /// @param helper pointer to function that matches getStringHelper prototype /// @see getString() - void setGetStringHelper(GetStringHelper helper) { m_get_string_helper = helper; } + template <typename HelperT> + void setGetStringHelper(HelperT &&helper) + { + m_get_string_helper = std::forward<HelperT>(helper); + } /// Run current GetString helper function (if defined). /// @see getString() |