Add Observable template.

This commit is contained in:
David Capello 2012-08-18 17:47:00 -03:00
parent 3b99e73734
commit 2ca98391e0
2 changed files with 85 additions and 14 deletions

59
src/observable.h Normal file
View File

@ -0,0 +1,59 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef OBSERVABLE_H_INCLUDED
#define OBSERVABLE_H_INCLUDED
#include "observers.h"
template<typename Observer>
class Observable {
public:
void addObserver(Observer* observer) {
m_observers.addObserver(observer);
}
void removeObserver(Observer* observer) {
m_observers.removeObserver(observer);
}
void notifyObservers(void (Observer::*method)()) {
m_observers.notifyObservers(method);
}
template<typename A1>
void notifyObservers(void (Observer::*method)(A1), A1 a1) {
m_observers.notifyObservers(method, a1);
}
template<typename A1, typename A2>
void notifyObservers(void (Observer::*method)(A1, A2), A1 a1, A2 a2) {
m_observers.notifyObservers(method, a1, a2);
}
template<typename A1, typename A2, typename A3>
void notifyObservers(void (Observer::*method)(A1, A2, A3), A1 a1, A2 a2, A3 a3) {
m_observers.notifyObservers(method, a1, a2, a3);
}
private:
Observers<Observer> m_observers;
};
#endif // OBSERVERS_H_INCLUDED

View File

@ -41,23 +41,20 @@ public:
Observers() { } Observers() { }
~Observers() ~Observers() {
{
disposeAllObservers(); disposeAllObservers();
} }
// Adds the observer in the collection. The observer is owned by the // Adds the observer in the collection. The observer is owned by the
// collection and will be destroyed calling the T::dispose() member // collection and will be destroyed calling the T::dispose() member
// function. // function.
void addObserver(observer_type* observer) void addObserver(observer_type* observer) {
{
m_observers.push_back(observer); m_observers.push_back(observer);
} }
// Removes the observer from the collection. After calling this // Removes the observer from the collection. After calling this
// function you own the observer so you have to dispose it. // function you own the observer so you have to dispose it.
void removeObserver(observer_type* observer) void removeObserver(observer_type* observer) {
{
iterator it = std::find(m_observers.begin(), m_observers.end(), observer); iterator it = std::find(m_observers.begin(), m_observers.end(), observer);
if (it != end()) if (it != end())
m_observers.erase(it); m_observers.erase(it);
@ -65,16 +62,14 @@ public:
// Disposes all observers. It's called automatically in the // Disposes all observers. It's called automatically in the
// Observers dtor. // Observers dtor.
void disposeAllObservers() void disposeAllObservers() {
{
while (!empty()) while (!empty())
(*begin())->dispose(); (*begin())->dispose();
m_observers.clear(); m_observers.clear();
} }
void notifyObservers(void (observer_type::*method)()) void notifyObservers(void (observer_type::*method)()) {
{
for (iterator for (iterator
it = this->begin(), it = this->begin(),
end = this->end(); it != end; ++it) { end = this->end(); it != end; ++it) {
@ -82,13 +77,30 @@ public:
} }
} }
template<typename Arg1> template<typename A1>
void notifyObservers(void (observer_type::*method)(Arg1), Arg1 arg1) void notifyObservers(void (observer_type::*method)(A1), A1 a1) {
{
for (iterator for (iterator
it = this->begin(), it = this->begin(),
end = this->end(); it != end; ++it) { end = this->end(); it != end; ++it) {
((*it)->*method)(arg1); ((*it)->*method)(a1);
}
}
template<typename A1, typename A2>
void notifyObservers(void (observer_type::*method)(A1, A2), A1 a1, A2 a2) {
for (iterator
it = this->begin(),
end = this->end(); it != end; ++it) {
((*it)->*method)(a1, a2);
}
}
template<typename A1, typename A2, typename A3>
void notifyObservers(void (observer_type::*method)(A1, A2, A3), A1 a1, A2 a2, A3 a3) {
for (iterator
it = this->begin(),
end = this->end(); it != end; ++it) {
((*it)->*method)(a1, a2, a3);
} }
} }