PTLib  Version 2.14.3
 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: 30773 $
27  * $Author: rjongbloed $
28  * $Date: 2013-10-23 15:22:12 +1100 (Wed, 23 Oct 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_NOTIFIER_EXT(notifierType, notifierArg, notifiee, func, ParamType, ParamArg) \
112  PDECLARE_NOTIFIER_COMMON(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, PValidatedNotifierFunction<ParamType>)
113 
117 #define PDECLARE_VALIDATED_NOTIFIER2(notifierType, notifiee, func, ParamType ) \
118  PDECLARE_VALIDATED_NOTIFIER_EXT(notifierType, , notifiee, func, ParamType, )
119 
121 #define PDECLARE_VALIDATED_NOTIFIER(notifierType, notifiee, func) \
122  PDECLARE_VALIDATED_NOTIFIER2(notifierType, notifiee, func, P_INT_PTR)
123 
124 
126 
128 {
130  virtual void Call() = 0;
131 
132  static void Queue(PNotifierIdentifer id, PAsyncNotifierCallback * callback);
133 };
134 
135 
150 {
151  public:
153  virtual ~PAsyncNotifierTarget();
154 
162  const PTimeInterval & wait = 0
163  );
164 
173  virtual void AsyncNotifierSignal();
174 
175  private:
176  PNotifierIdentifer m_asyncNotifierId;
177 
178  template <typename ParmType> friend class PAsyncNotifierFunction;
179 };
180 
181 
194 template <typename ParamType>
196 {
198  PCLASSINFO(PAsyncNotifierFunction, Parent);
199 
200  public:
201  PAsyncNotifierFunction(void * voidTarget, PAsyncNotifierTarget * notifierTarget)
202  : Parent(voidTarget)
203  , m_targetID(notifierTarget->m_asyncNotifierId)
204  { }
205 
206  protected:
208 
209  template <class Target>
211  {
212  private:
213  const Target & m_target;
214  PObject & m_notifier;
215  ParamType m_extra;
216 
217  public:
218  TypedCallback(const Target & target, PObject & notifier, ParamType extra)
219  : m_target(target)
220  , m_notifier(notifier)
221  , m_extra(extra)
222  { }
223 
224  virtual void Call()
225  {
226  m_target.AsyncCall(m_notifier, m_extra);
227  }
228  };
229 };
230 
231 
235 #define PDECLARE_ASYNC_NOTIFIER_EXT(notifierType, notifierArg, notifiee, func, ParamType, ParamArg) \
236  PDECLARE_NOTIFIER_COMMON1(notifierType, notifiee, func, ParamType, PAsyncNotifierFunction<ParamType>) \
237  { PAsyncNotifierCallback::Queue(m_targetID, new TypedCallback<func##_PNotifier>(*this, note, extra)); } \
238  void AsyncCall(PObject & note, ParamType extra) const \
239  PDECLARE_NOTIFIER_COMMON2(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, PAsyncNotifierFunction<ParamType>)
240 
244 #define PDECLARE_ASYNC_NOTIFIER2(notifierType, notifiee, func, ParamType) \
245  PDECLARE_ASYNC_NOTIFIER_EXT(notifierType, , notifiee, func, ParamType, )
246 
248 #define PDECLARE_ASYNC_NOTIFIER(notifierType, notifiee, func) \
249  PDECLARE_ASYNC_NOTIFIER2(notifierType, notifiee, func, P_INT_PTR)
250 
251 
253 
256 template <typename ParamType>
258 {
259  PCLASSINFO(PNotifierListTemplate, PObject);
260  private:
262  typedef std::list<Notifier> List;
263  List m_list;
264 
265  public:
267  PINDEX GetSize() const { return this->m_list.size(); }
268 
270  void Add(const Notifier & handler)
271  {
272  this->m_list.push_back(handler);
273  }
274 
276  void Remove(const Notifier & handler)
277  {
278  this->m_list.remove(handler);
279  }
280 
281  class IsObj : public std::unary_function<PObject, bool>
282  {
283  public:
285  IsObj(PObject * obj) : m_obj(obj) { }
286  bool operator()(Notifier & test) const { return m_obj == test.GetObject(); }
287  };
288 
290  void RemoveTarget(PObject * obj)
291  {
292  this->m_list.remove_if(IsObj(obj));
293  }
294 
296  bool operator()(PObject & obj, ParamType param)
297  {
298  if (this->m_list.empty())
299  return false;
300  for (typename List::iterator it = this->m_list.begin(); it != this->m_list.end() ; ++it)
301  (*it)(obj, param);
302  return true;
303  }
304 };
305 
307 
308 
309 #endif // PTLIB_NOTIFIER_EXT_H
310 
311 
312 // End of File ///////////////////////////////////////////////////////////////