aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-05-01 14:47:53 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-05-10 11:00:45 (GMT)
commit4b47675441b76620519b0204497686b09113daaa (patch)
treec3494c61a82b84418761000d5fc7fc5d12188204 /src/FbTk
parentfa15400cc24ddcfd6e361bd068ae1986b9f9e561 (diff)
downloadfluxbox-4b47675441b76620519b0204497686b09113daaa.zip
fluxbox-4b47675441b76620519b0204497686b09113daaa.tar.bz2
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 <http://www.artima.com/cppsource/safebool.html>. And, finally, I removed the mutable flag from the m_data member, since it does not need it.
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/LogicCommands.cc6
-rw-r--r--src/FbTk/LogicCommands.hh4
-rw-r--r--src/FbTk/MacroCommand.cc2
-rw-r--r--src/FbTk/MultiButtonMenuItem.cc2
-rw-r--r--src/FbTk/RefCount.hh8
-rw-r--r--src/FbTk/Timer.cc6
6 files changed, 16 insertions, 12 deletions
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) {
44 std::vector<std::string>::iterator it = cmds.begin(), it_end = cmds.end(); 44 std::vector<std::string>::iterator it = cmds.begin(), it_end = cmds.end();
45 for (; it != it_end; ++it) { 45 for (; it != it_end; ++it) {
46 cmd = CommandParser<bool>::instance().parse(*it, trusted); 46 cmd = CommandParser<bool>::instance().parse(*it, trusted);
47 if (*cmd) 47 if (cmd)
48 macro->add(cmd); 48 macro->add(cmd);
49 } 49 }
50 50
@@ -91,13 +91,13 @@ Command<void> *IfCommand::parse(const std::string &command, const std::string &a
91 return 0; 91 return 0;
92 92
93 cond = CommandParser<bool>::instance().parse(cmds[0], trusted); 93 cond = CommandParser<bool>::instance().parse(cmds[0], trusted);
94 if (*cond == 0) 94 if (cond == 0)
95 return 0; 95 return 0;
96 96
97 t = CommandParser<void>::instance().parse(cmds[1], trusted); 97 t = CommandParser<void>::instance().parse(cmds[1], trusted);
98 if (cmds.size() >= 3) 98 if (cmds.size() >= 3)
99 f = CommandParser<void>::instance().parse(cmds[2], trusted); 99 f = CommandParser<void>::instance().parse(cmds[2], trusted);
100 if (*t == 0 && *f == 0) 100 if (t == 0 && f == 0)
101 return 0; 101 return 0;
102 102
103 return new IfCommand(cond, t, f); 103 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:
38 m_cond(cond), m_t(t), m_f(f) { } 38 m_cond(cond), m_t(t), m_f(f) { }
39 void execute() { 39 void execute() {
40 if (m_cond->execute()) { 40 if (m_cond->execute()) {
41 if (*m_t) m_t->execute(); 41 if (m_t) m_t->execute();
42 } else 42 } else
43 if (*m_f) m_f->execute(); 43 if (m_f) m_f->execute();
44 } 44 }
45 static Command<void> *parse(const std::string &cmd, const std::string &args, 45 static Command<void> *parse(const std::string &cmd, const std::string &args,
46 bool trusted); 46 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) {
42 std::list<std::string>::iterator it = cmds.begin(), it_end = cmds.end(); 42 std::list<std::string>::iterator it = cmds.begin(), it_end = cmds.end();
43 for (; it != it_end; ++it) { 43 for (; it != it_end; ++it) {
44 cmd = CommandParser<void>::instance().parse(*it, trusted); 44 cmd = CommandParser<void>::instance().parse(*it, trusted);
45 if (*cmd) 45 if (cmd)
46 macro->add(cmd); 46 macro->add(cmd);
47 } 47 }
48 } 48 }
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) {
55 if (button <= 0 || button > static_cast<signed>(buttons()) || buttons() == 0) 55 if (button <= 0 || button > static_cast<signed>(buttons()) || buttons() == 0)
56 return; 56 return;
57 57
58 if (*m_button_exe[button - 1] != 0) 58 if (m_button_exe[button - 1] != 0)
59 m_button_exe[button - 1]->execute(); 59 m_button_exe[button - 1]->execute();
60} 60}
61 61
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 {
27/// holds a pointer with reference counting, similar to std:auto_ptr 27/// holds a pointer with reference counting, similar to std:auto_ptr
28template <typename Pointer> 28template <typename Pointer>
29class RefCount { 29class RefCount {
30 typedef Pointer* RefCount::*bool_type;
31
30public: 32public:
31 RefCount(); 33 RefCount();
32 explicit RefCount(Pointer *p); 34 explicit RefCount(Pointer *p);
@@ -35,9 +37,11 @@ public:
35 ~RefCount(); 37 ~RefCount();
36 RefCount<Pointer> &operator = (const RefCount<Pointer> &copy); 38 RefCount<Pointer> &operator = (const RefCount<Pointer> &copy);
37 RefCount<Pointer> &operator = (Pointer *p); 39 RefCount<Pointer> &operator = (Pointer *p);
38 Pointer *operator * () const { return get(); } 40 Pointer &operator * () const { return *get(); }
39 Pointer *operator -> () const { return get(); } 41 Pointer *operator -> () const { return get(); }
40 Pointer *get() const { return m_data; } 42 Pointer *get() const { return m_data; }
43 /// conversion to "bool"
44 operator bool_type() const { return m_data ? &RefCount::m_data : 0; }
41 45
42private: 46private:
43 /// increase reference count 47 /// increase reference count
@@ -45,7 +49,7 @@ private:
45 /// decrease reference count 49 /// decrease reference count
46 void decRefCount(); 50 void decRefCount();
47 Pointer *m_data; ///< data holder 51 Pointer *m_data; ///< data holder
48 mutable unsigned int *m_refcount; ///< holds reference counting 52 unsigned int *m_refcount; ///< holds reference counting
49}; 53};
50 54
51// implementation 55// 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() {
98 gettimeofday(&m_start, 0); 98 gettimeofday(&m_start, 0);
99 99
100 // only add Timers that actually DO something 100 // only add Timers that actually DO something
101 if ((! m_timing || m_interval != 0) && *m_handler) { 101 if ((! m_timing || m_interval != 0) && m_handler) {
102 m_timing = true; 102 m_timing = true;
103 addTimer(this); //add us to the list 103 addTimer(this); //add us to the list
104 } 104 }
@@ -121,7 +121,7 @@ void Timer::makeEndTime(timeval &tm) const {
121 121
122 122
123void Timer::fireTimeout() { 123void Timer::fireTimeout() {
124 if (*m_handler) 124 if (m_handler)
125 m_handler->execute(); 125 m_handler->execute();
126} 126}
127 127
@@ -273,7 +273,7 @@ Command<void> *DelayedCmd::parse(const std::string &command,
273 return 0; 273 return 0;
274 274
275 RefCount<Command<void> > cmd(CommandParser<void>::instance().parse(cmd_str, trusted)); 275 RefCount<Command<void> > cmd(CommandParser<void>::instance().parse(cmd_str, trusted));
276 if (*cmd == 0) 276 if (cmd == 0)
277 return 0; 277 return 0;
278 278
279 int delay = 200000; 279 int delay = 200000;