mirror of
https://github.com/aseprite/aseprite.git
synced 2025-02-20 18:40:57 +00:00
Add Observable template.
This commit is contained in:
parent
3b99e73734
commit
2ca98391e0
59
src/observable.h
Normal file
59
src/observable.h
Normal 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
|
@ -41,23 +41,20 @@ public:
|
||||
|
||||
Observers() { }
|
||||
|
||||
~Observers()
|
||||
{
|
||||
~Observers() {
|
||||
disposeAllObservers();
|
||||
}
|
||||
|
||||
// Adds the observer in the collection. The observer is owned by the
|
||||
// collection and will be destroyed calling the T::dispose() member
|
||||
// function.
|
||||
void addObserver(observer_type* observer)
|
||||
{
|
||||
void addObserver(observer_type* observer) {
|
||||
m_observers.push_back(observer);
|
||||
}
|
||||
|
||||
// Removes the observer from the collection. After calling this
|
||||
// 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);
|
||||
if (it != end())
|
||||
m_observers.erase(it);
|
||||
@ -65,16 +62,14 @@ public:
|
||||
|
||||
// Disposes all observers. It's called automatically in the
|
||||
// Observers dtor.
|
||||
void disposeAllObservers()
|
||||
{
|
||||
void disposeAllObservers() {
|
||||
while (!empty())
|
||||
(*begin())->dispose();
|
||||
|
||||
m_observers.clear();
|
||||
}
|
||||
|
||||
void notifyObservers(void (observer_type::*method)())
|
||||
{
|
||||
void notifyObservers(void (observer_type::*method)()) {
|
||||
for (iterator
|
||||
it = this->begin(),
|
||||
end = this->end(); it != end; ++it) {
|
||||
@ -82,13 +77,30 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Arg1>
|
||||
void notifyObservers(void (observer_type::*method)(Arg1), Arg1 arg1)
|
||||
{
|
||||
template<typename A1>
|
||||
void notifyObservers(void (observer_type::*method)(A1), A1 a1) {
|
||||
for (iterator
|
||||
it = this->begin(),
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user