PTLib  Version 2.12.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
notifier_ext.h
Go to the documentation of this file.
1 /*
2  * notifier_ext.h
3  *
4  * Smart Notifiers and Notifier Lists
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 2004 Reitek S.p.A.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 29653 $
27  * $Author: rjongbloed $
28  * $Date: 2013-05-07 16:11:36 +1000 (Tue, 07 May 2013) $
29  */
30 
31 #ifndef PTLIB_NOTIFIER_EXT_H
32 #define PTLIB_NOTIFIER_EXT_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <list>
39 
40 
42 
43 typedef unsigned long PNotifierIdentifer;
44 
61 {
62  public:
64  virtual ~PValidatedNotifierTarget();
65 
66  // Do not copy the id across
69 
70  static bool Exists(PNotifierIdentifer id);
71 
72  private:
73  PNotifierIdentifer m_validatedNotifierId;
74 
75  template <typename ParmType> friend class PValidatedNotifierFunction;
76 };
77 
78 
86 template <typename ParamType>
88 {
91 
92  public:
93  PValidatedNotifierFunction(void * voidTarget, PValidatedNotifierTarget * notifierTarget)
94  : Parent(voidTarget)
95  , m_targetID(notifierTarget->m_validatedNotifierId)
96  { }
97 
98  virtual void * GetTarget() const
99  {
100  return PValidatedNotifierTarget::Exists(this->m_targetID) ? this->m_target : NULL;
101  }
102 
103  protected:
105 };
106 
107 
111 #define PDECLARE_VALIDATED_NOTIFIER2(notifier, notifiee, func, ParamType) \
112  PDECLARE_NOTIFIER_COMMON(notifier, notifiee, func, ParamType, PValidatedNotifierFunction<ParamType>)
113 
115 #define PDECLARE_VALIDATED_NOTIFIER(notifier, notifiee, func) \
116  PDECLARE_VALIDATED_NOTIFIER2(notifier, notifiee, func, P_INT_PTR)
117 
118 
120 
122 {
124  virtual void Call() = 0;
125 
126  static void Queue(PNotifierIdentifer id, PAsyncNotifierCallback * callback);
127 };
128 
129 
144 {
145  public:
147  virtual ~PAsyncNotifierTarget();
148 
156  const PTimeInterval & wait = 0
157  );
158 
167  virtual void AsyncNotifierSignal();
168 
169  private:
170  PNotifierIdentifer m_asyncNotifierId;
171 
172  template <typename ParmType> friend class PAsyncNotifierFunction;
173 };
174 
175 
188 template <typename ParamType>
190 {
192  PCLASSINFO(PAsyncNotifierFunction, Parent);
193 
194  public:
195  PAsyncNotifierFunction(void * voidTarget, PAsyncNotifierTarget * notifierTarget)
196  : Parent(voidTarget)
197  , m_targetID(notifierTarget->m_asyncNotifierId)
198  { }
199 
200  protected:
202 
203  template <class Target>
205  {
206  private:
207  const Target & m_target;
208  PObject & m_notifier;
209  ParamType m_extra;
210 
211  public:
212  TypedCallback(const Target & target, PObject & notifier, ParamType extra)
213  : m_target(target)
214  , m_notifier(notifier)
215  , m_extra(extra)
216  { }
217 
218  virtual void Call()
219  {
220  m_target.AsyncCall(m_notifier, m_extra);
221  }
222  };
223 };
224 
225 
229 #define PDECLARE_ASYNC_NOTIFIER2(notifier, notifiee, func, ParamType) \
230  PDECLARE_NOTIFIER_COMMON1(notifier, notifiee, func, ParamType, PAsyncNotifierFunction<ParamType>) \
231  { PAsyncNotifierCallback::Queue(m_targetID, new TypedCallback<func##_PNotifier>(*this, note, extra)); } \
232  void AsyncCall(PObject & note, ParamType extra) const \
233  PDECLARE_NOTIFIER_COMMON2(notifier, notifiee, func, ParamType, PAsyncNotifierFunction<ParamType>) \
234 
235 
236 #define PDECLARE_ASYNC_NOTIFIER(notifier, notifiee, func) \
237  PDECLARE_ASYNC_NOTIFIER2(notifier, notifiee, func, P_INT_PTR)
238 
239 
241 
244 template <typename ParamType>
246 {
247  PCLASSINFO(PNotifierListTemplate, PObject);
248  private:
250  typedef std::list<Notifier> List;
251  List m_list;
252 
253  public:
255  PINDEX GetSize() const { return this->m_list.size(); }
256 
258  void Add(const Notifier & handler)
259  {
260  this->m_list.push_back(handler);
261  }
262 
264  void Remove(const Notifier & handler)
265  {
266  this->m_list.remove(handler);
267  }
268 
269  class IsObj : public std::unary_function<PObject, bool>
270  {
271  public:
273  IsObj(PObject * obj) : m_obj(obj) { }
274  bool operator()(Notifier & test) const { return m_obj == test.GetObject(); }
275  };
276 
278  void RemoveTarget(PObject * obj)
279  {
280  this->m_list.remove_if(IsObj(obj));
281  }
282 
284  bool operator()(PObject & obj, ParamType param)
285  {
286  if (this->m_list.empty())
287  return false;
288  for (typename List::iterator it = this->m_list.begin(); it != this->m_list.end() ; ++it)
289  (*it)(obj, param);
290  return true;
291  }
292 };
293 
295 
296 
297 #endif // PTLIB_NOTIFIER_EXT_H
298 
299 
300 // End of File ///////////////////////////////////////////////////////////////