ScummVM API documentation
te_signal.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef TETRAEDGE_TE_TE_SIGNAL_H
23 #define TETRAEDGE_TE_TE_SIGNAL_H
24 
25 #include "common/array.h"
26 #include "common/ptr.h"
27 #include "tetraedge/te/te_callback.h"
28 
29 namespace Tetraedge {
30 
31 template<class C> bool _teCallbackSorter(const C &c1, const C &c2) {
32  // sort in *descending* priority.
33  float p1 = c1->priority();
34  float p2 = c2->priority();
35  return p2 < p1;
36 }
37 
38 typedef Common::SharedPtr<TeICallback0Param> TeICallback0ParamPtr;
39 
40 class TeSignal0Param : public Common::Array<TeICallback0ParamPtr> {
41 public:
43 
44  bool call() {
45  Common::sort(this->begin(), this->end(), _teCallbackSorter<TeICallback0ParamPtr>);
47  typename Common::Array<TeICallback0ParamPtr>::iterator end_ = this->end();
48  for (; i < end_; i++) {
49  if ((*i)->call())
50  return true;
51  }
52  return false;
53  }
54 
55  void remove(const TeICallback0ParamPtr &item) {
57  typename Common::Array<TeICallback0ParamPtr>::iterator end_ = this->end();
58  for (; i < end_; i++) {
59  if ((*i)->equals(item.get())) {
60  i = this->erase(i);
61  }
62  }
63  }
64 
65  template<class T> void add(T *obj, typename TeCallback0Param<T>::TMethod method) {
66  this->push_back(TeICallback0ParamPtr(new TeCallback0Param<T>(obj, method)));
67  }
68 
69  template<class T> void remove(T *obj, typename TeCallback0Param<T>::TMethod method) {
70  TeICallback0ParamPtr temp(new TeCallback0Param<T>(obj, method));
71  this->remove(temp);
72  }
73 
74 };
75 
76 /* Pointer to a callback with a single parameter of type T */
78 
79 /* Array of callbacks with a single parameter of type T */
80 template<class T> class TeSignal1Param : public Common::Array<TeICallback1ParamPtr<T>> {
81 public:
83 
84  bool call(T t) {
85  Common::sort(this->begin(), this->end(), _teCallbackSorter<TeICallback1ParamPtr<T>>);
88  for (; i < end_; i++) {
89  if ((*i)->call(t))
90  return true;
91  }
92  return false;
93  }
94 
95  void remove(const TeICallback1ParamPtr<T> &item) {
98  for (; i < end_; i++) {
99  if ((*i)->equals(item.get())) {
100  i = this->erase(i);
101  }
102  }
103  }
104 
105  template<class S> void add(S *obj, typename TeCallback1Param<S, T>::TMethod method) {
107  }
108 
109  template<class S> void remove(S *obj, typename TeCallback1Param<S, T>::TMethod method) {
110  TeICallback1ParamPtr<T> temp(new TeCallback1Param<S, T>(obj, method));
111  this->remove(temp);
112  }
113 };
114 
115 /* Pointer to a callback with 2 paramets parameter of type T and S */
116 template<class S, class T> using TeICallback2ParamPtr = Common::SharedPtr<TeICallback2Param<S, T>>;
117 
118 /* Array of callbacks with a two parameters of type T */
119 template<class S, class T> class TeSignal2Param : public Common::Array<TeICallback2ParamPtr<S, T>> {
120 public:
122 
123  bool call(S s, T t) {
124  Common::sort(this->begin(), this->end(), _teCallbackSorter<TeICallback2ParamPtr<S, T>>);
127  for (; i < end_; i++) {
128  if ((*i)->call(s, t))
129  return true;
130  }
131  return false;
132  }
133 
134  void remove(const TeICallback2ParamPtr<S, T> &item) {
137  for (; i < end_; i++) {
138  if ((*i)->equals(item.get())) {
139  i = this->erase(i);
140  }
141  }
142  }
143 
144  template<class C> void add(C *obj, typename TeCallback2Param<C, S, T>::TMethod method) {
146  }
147 
148  template<class C> void remove(C *obj, typename TeCallback2Param<C, S, T>::TMethod method) {
150  this->remove(temp);
151  }
152 };
153 
154 } // end namespace Tetraedge
155 
156 #endif // TETRAEDGE_TE_TE_SIGNAL_H
Definition: te_signal.h:40
Definition: detection.h:27
Definition: array.h:52
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
T * iterator
Definition: array.h:54
Definition: te_callback.h:107
void push_back(const TeICallback0ParamPtr &element)
Definition: array.h:180
Definition: te_callback.h:74
Definition: te_callback.h:42
Definition: te_signal.h:119
Definition: te_signal.h:80
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
iterator erase(iterator pos)
Definition: array.h:328