From 4b47675441b76620519b0204497686b09113daaa Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 1 May 2011 16:47:53 +0200 Subject: Make RefCount<> more sensible the previous version of operator*() made no sense. E.g., it violated the invariant (*ptr).foo <=> ptr->foo. The dereferencing operator now returns a reference to the pointed-to object, rather than a pointer to it. I also added a bool conversion operator, which can be used in testing the NULL-ness of the pointer. Anyone wondering if that could be done in a simpler way is encouraged to read . And, finally, I removed the mutable flag from the m_data member, since it does not need it. --- src/CommandDialog.cc | 2 +- src/FbTk/LogicCommands.cc | 6 +++--- src/FbTk/LogicCommands.hh | 4 ++-- src/FbTk/MacroCommand.cc | 2 +- src/FbTk/MultiButtonMenuItem.cc | 2 +- src/FbTk/RefCount.hh | 8 ++++++-- src/FbTk/Timer.cc | 6 +++--- src/Keys.cc | 6 +++--- src/MenuCreator.cc | 2 +- src/Remember.cc | 8 ++++---- src/ToolFactory.cc | 2 +- src/WorkspaceCmd.cc | 2 +- 12 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/CommandDialog.cc b/src/CommandDialog.cc index bef8a63..023c22f 100644 --- a/src/CommandDialog.cc +++ b/src/CommandDialog.cc @@ -51,7 +51,7 @@ void CommandDialog::exec(const std::string &text){ if (cmd.get()) cmd->execute(); // post execute - if (*m_postcommand != 0) + if (m_postcommand != 0) m_postcommand->execute(); } diff --git a/src/FbTk/LogicCommands.cc b/src/FbTk/LogicCommands.cc index b3abe31..bda67f5 100644 --- a/src/FbTk/LogicCommands.cc +++ b/src/FbTk/LogicCommands.cc @@ -44,7 +44,7 @@ M *addCommands(M *macro, const string &args, bool trusted) { std::vector::iterator it = cmds.begin(), it_end = cmds.end(); for (; it != it_end; ++it) { cmd = CommandParser::instance().parse(*it, trusted); - if (*cmd) + if (cmd) macro->add(cmd); } @@ -91,13 +91,13 @@ Command *IfCommand::parse(const std::string &command, const std::string &a return 0; cond = CommandParser::instance().parse(cmds[0], trusted); - if (*cond == 0) + if (cond == 0) return 0; t = CommandParser::instance().parse(cmds[1], trusted); if (cmds.size() >= 3) f = CommandParser::instance().parse(cmds[2], trusted); - if (*t == 0 && *f == 0) + if (t == 0 && f == 0) return 0; return new IfCommand(cond, t, f); diff --git a/src/FbTk/LogicCommands.hh b/src/FbTk/LogicCommands.hh index 5e84473..c0cb938 100644 --- a/src/FbTk/LogicCommands.hh +++ b/src/FbTk/LogicCommands.hh @@ -38,9 +38,9 @@ public: m_cond(cond), m_t(t), m_f(f) { } void execute() { if (m_cond->execute()) { - if (*m_t) m_t->execute(); + if (m_t) m_t->execute(); } else - if (*m_f) m_f->execute(); + if (m_f) m_f->execute(); } static Command *parse(const std::string &cmd, const std::string &args, bool trusted); diff --git a/src/FbTk/MacroCommand.cc b/src/FbTk/MacroCommand.cc index 0b7a6b8..e04d92a 100644 --- a/src/FbTk/MacroCommand.cc +++ b/src/FbTk/MacroCommand.cc @@ -42,7 +42,7 @@ M *addCommands(M *macro, const std::string &args, bool trusted) { std::list::iterator it = cmds.begin(), it_end = cmds.end(); for (; it != it_end; ++it) { cmd = CommandParser::instance().parse(*it, trusted); - if (*cmd) + if (cmd) macro->add(cmd); } } diff --git a/src/FbTk/MultiButtonMenuItem.cc b/src/FbTk/MultiButtonMenuItem.cc index ed67c83..1b61a99 100644 --- a/src/FbTk/MultiButtonMenuItem.cc +++ b/src/FbTk/MultiButtonMenuItem.cc @@ -55,7 +55,7 @@ void MultiButtonMenuItem::click(int button, int time, unsigned int mods) { if (button <= 0 || button > static_cast(buttons()) || buttons() == 0) return; - if (*m_button_exe[button - 1] != 0) + if (m_button_exe[button - 1] != 0) m_button_exe[button - 1]->execute(); } diff --git a/src/FbTk/RefCount.hh b/src/FbTk/RefCount.hh index 6d1b9b7..597f847 100644 --- a/src/FbTk/RefCount.hh +++ b/src/FbTk/RefCount.hh @@ -27,6 +27,8 @@ namespace FbTk { /// holds a pointer with reference counting, similar to std:auto_ptr template class RefCount { + typedef Pointer* RefCount::*bool_type; + public: RefCount(); explicit RefCount(Pointer *p); @@ -35,9 +37,11 @@ public: ~RefCount(); RefCount &operator = (const RefCount ©); RefCount &operator = (Pointer *p); - Pointer *operator * () const { return get(); } + Pointer &operator * () const { return *get(); } Pointer *operator -> () const { return get(); } Pointer *get() const { return m_data; } + /// conversion to "bool" + operator bool_type() const { return m_data ? &RefCount::m_data : 0; } private: /// increase reference count @@ -45,7 +49,7 @@ private: /// decrease reference count void decRefCount(); Pointer *m_data; ///< data holder - mutable unsigned int *m_refcount; ///< holds reference counting + unsigned int *m_refcount; ///< holds reference counting }; // implementation diff --git a/src/FbTk/Timer.cc b/src/FbTk/Timer.cc index 60df968..cb9ac59 100644 --- a/src/FbTk/Timer.cc +++ b/src/FbTk/Timer.cc @@ -98,7 +98,7 @@ void Timer::start() { gettimeofday(&m_start, 0); // only add Timers that actually DO something - if ((! m_timing || m_interval != 0) && *m_handler) { + if ((! m_timing || m_interval != 0) && m_handler) { m_timing = true; addTimer(this); //add us to the list } @@ -121,7 +121,7 @@ void Timer::makeEndTime(timeval &tm) const { void Timer::fireTimeout() { - if (*m_handler) + if (m_handler) m_handler->execute(); } @@ -273,7 +273,7 @@ Command *DelayedCmd::parse(const std::string &command, return 0; RefCount > cmd(CommandParser::instance().parse(cmd_str, trusted)); - if (*cmd == 0) + if (cmd == 0) return 0; int delay = 200000; diff --git a/src/Keys.cc b/src/Keys.cc index d3b82e9..79c273b 100644 --- a/src/Keys.cc +++ b/src/Keys.cc @@ -487,7 +487,7 @@ bool Keys::addBinding(const string &linebuffer) { first_new_key = new t_key(type, mod, key, context, isdouble); current_key = first_new_key; - } else if (*current_key->m_command) // already being used + } else if (current_key->m_command) // already being used return false; } else { t_key *temp_key = new t_key(type, mod, key, context, @@ -511,7 +511,7 @@ bool Keys::addBinding(const string &linebuffer) { if (str) // +1 to skip ':' current_key->m_command = FbTk::CommandParser::instance().parse(str + 1); - if (!str || *current_key->m_command == 0 || mod) { + if (!str || current_key->m_command == 0 || mod) { delete first_new_key; return false; } @@ -572,7 +572,7 @@ bool Keys::doAction(int type, unsigned int mods, unsigned int key, setKeyMode(next_key); return true; } - if (!temp_key || *temp_key->m_command == 0) { + if (!temp_key || temp_key->m_command == 0) { if (type == KeyPress && !FbTk::KeyUtil::instance().keycodeToModmask(key)) { // if we're in the middle of an emacs-style keychain, exit it diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index 9884187..74968e3 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -328,7 +328,7 @@ void translateMenuItem(FbTk::Parser &parse, ParseItem &pitem, // we need to attach command to arguments so command parser can parse it string line = str_key + " " + str_cmd; FbTk::RefCount > command(FbTk::CommandParser::instance().parse(line)); - if (*command != 0) { + if (command != 0) { // special NLS default labels if (str_label.empty()) { if (str_key == "reconfig" || str_key == "reconfigure") { diff --git a/src/Remember.cc b/src/Remember.cc index 6dd636f..10aa52c 100644 --- a/src/Remember.cc +++ b/src/Remember.cc @@ -584,8 +584,8 @@ Application* findMatchingPatterns(ClientPattern *pat, Remember::Patterns *patlis for (; it != it_end; ++it) { if (*it->first == *pat && is_group == it->second->is_grouped && transient == it->second->is_transient && - ((match_pat == 0 && *it->second->group_pattern == 0) || - (match_pat && *match_pat == **it->second->group_pattern))) { + ((match_pat == 0 && it->second->group_pattern == 0) || + (match_pat && *match_pat == *it->second->group_pattern))) { Application *ret = it->second; @@ -887,7 +887,7 @@ void Remember::save() { grouped_apps.insert(&a); // otherwise output this whole group apps_file << "[group]"; - if (*a.group_pattern) + if (a.group_pattern) apps_file << " " << a.group_pattern->toString(); apps_file << endl; @@ -1366,7 +1366,7 @@ FluxboxWindow *Remember::findGroup(Application *app, BScreen &screen) { for (; it != it_end; ++it) { if (it->second == app && it->first->fbwindow() && &screen == &it->first->screen() && - (!*app->group_pattern || app->group_pattern->match(*it->first))) + (!app->group_pattern || app->group_pattern->match(*it->first))) return it->first->fbwindow(); } diff --git a/src/ToolFactory.cc b/src/ToolFactory.cc index 2645956..d94105d 100644 --- a/src/ToolFactory.cc +++ b/src/ToolFactory.cc @@ -92,7 +92,7 @@ ToolbarItem *ToolFactory::create(const std::string &name, const FbTk::FbWindow & item = new ClockTool(parent, m_clock_theme, screen(), tbar.menu()); } else { FbTk::RefCount > cmd(FbTk::CommandParser::instance().parse(name)); - if (*cmd == 0) // we need a command + if (cmd == 0) // we need a command return 0; // TODO maybe direction of arrows should depend on toolbar layout ? diff --git a/src/WorkspaceCmd.cc b/src/WorkspaceCmd.cc index ad88640..34c8f9a 100644 --- a/src/WorkspaceCmd.cc +++ b/src/WorkspaceCmd.cc @@ -90,7 +90,7 @@ void WindowListCmd::execute() { WindowCmd::setWindow((*it)->fbwindow()); else if (typeid(**it) == typeid(WinClient)) WindowCmd::setClient(dynamic_cast(*it)); - if (!*m_filter || m_filter->execute()) + if (!m_filter || m_filter->execute()) m_cmd->execute(); } WindowCmd::setClient(old); -- cgit v0.11.2