PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_NOTIFIER_EXT_H
28 #define PTLIB_NOTIFIER_EXT_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include <list>
35 
36 
38 
39 typedef unsigned long PNotifierIdentifer;
40 
57 {
58  public:
60  virtual ~PValidatedNotifierTarget();
61 
62  // Do not copy the id across
65 
66  static bool Exists(PNotifierIdentifer id);
67 
68  private:
69  PNotifierIdentifer m_validatedNotifierId;
70 
71  template <typename ParmType> friend class PValidatedNotifierFunction;
72 };
73 
74 
82 template <typename ParamType>
84 {
87 
88  public:
89  PValidatedNotifierFunction(void * voidTarget, PValidatedNotifierTarget * notifierTarget)
90  : Parent(voidTarget)
91  , m_targetID(notifierTarget->m_validatedNotifierId)
92  { }
93 
94  virtual void * GetTarget() const
95  {
96  return PValidatedNotifierTarget::Exists(this->m_targetID) ? this->m_target : NULL;
97  }
98 
99  protected:
101 };
102 
103 
107 #define PDECLARE_VALIDATED_NOTIFIER_EXT(notifierType, notifierArg, notifiee, func, ParamType, ParamArg) \
108  PDECLARE_NOTIFIER_COMMON(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, PValidatedNotifierFunction<ParamType>)
109 
113 #define PDECLARE_VALIDATED_NOTIFIER2(notifierType, notifiee, func, ParamType ) \
114  PDECLARE_VALIDATED_NOTIFIER_EXT(notifierType, , notifiee, func, ParamType, )
115 
117 #define PDECLARE_VALIDATED_NOTIFIER(notifierType, notifiee, func) \
118  PDECLARE_VALIDATED_NOTIFIER2(notifierType, notifiee, func, P_INT_PTR)
119 
120 
122 
124 {
126  virtual void Call() = 0;
127 
128  static void Queue(PNotifierIdentifer id, PAsyncNotifierCallback * callback);
129 };
130 
131 
146 {
147  public:
149  virtual ~PAsyncNotifierTarget();
150 
158  const PTimeInterval & wait = 0
159  );
160 
169  virtual void AsyncNotifierSignal();
170 
171  private:
172  PNotifierIdentifer m_asyncNotifierId;
173 
174  template <typename ParmType> friend class PAsyncNotifierFunction;
175 };
176 
177 
190 template <typename ParamType>
192 {
194  PCLASSINFO(PAsyncNotifierFunction, Parent);
195 
196  public:
197  PAsyncNotifierFunction(void * voidTarget, PAsyncNotifierTarget * notifierTarget)
198  : Parent(voidTarget)
199  , m_targetID(notifierTarget->m_asyncNotifierId)
200  { }
201 
202  protected:
204 
205  template <class Target>
207  {
208  private:
209  const Target & m_target;
210  PObject & m_notifier;
211  ParamType m_extra;
212 
213  public:
214  TypedCallback(const Target & target, PObject & notifier, ParamType extra)
215  : m_target(target)
216  , m_notifier(notifier)
217  , m_extra(extra)
218  { }
219 
220  virtual void Call()
221  {
222  m_target.AsyncCall(m_notifier, m_extra);
223  }
224  };
225 };
226 
227 
231 #define PDECLARE_ASYNC_NOTIFIER_EXT(notifierType, notifierArg, notifiee, func, ParamType, ParamArg) \
232  PDECLARE_NOTIFIER_COMMON1(notifierType, notifiee, func, ParamType, PAsyncNotifierFunction<ParamType>) \
233  { PAsyncNotifierCallback::Queue(m_targetID, new TypedCallback<func##_PNotifier>(*this, note, extra)); } \
234  void AsyncCall(PObject & note, ParamType extra) const \
235  PDECLARE_NOTIFIER_COMMON2(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, PAsyncNotifierFunction<ParamType>)
236 
240 #define PDECLARE_ASYNC_NOTIFIER2(notifierType, notifiee, func, ParamType) \
241  PDECLARE_ASYNC_NOTIFIER_EXT(notifierType, , notifiee, func, ParamType, )
242 
244 #define PDECLARE_ASYNC_NOTIFIER(notifierType, notifiee, func) \
245  PDECLARE_ASYNC_NOTIFIER2(notifierType, notifiee, func, P_INT_PTR)
246 
247 
249 
252 template <typename ParamType>
254 {
255  PCLASSINFO(PNotifierListTemplate, PObject);
256  private:
258  typedef std::list<Notifier> List;
259  List m_list;
260 
261  public:
263  PINDEX GetSize() const { return this->m_list.size(); }
264 
266  bool Add(const Notifier & handler)
267  {
268  if (std::find(this->m_list.begin(), this->m_list.end(), handler) != this->m_list.end())
269  return false;
270  this->m_list.push_back(handler);
271  return true;
272  }
273 
275  bool Remove(const Notifier & handler)
276  {
277  typename List::iterator it = std::find(this->m_list.begin(), this->m_list.end(), handler);
278  if (it == this->m_list.end())
279  return false;
280  this->m_list.erase(it);
281  return true;
282  }
283 
285  bool Contains(const Notifier & handler) const
286  {
287  return std::find(this->m_list.begin(), this->m_list.end(), handler) != this->m_list.end();
288  }
289 
291  bool IsEmpty() const
292  {
293  return this->m_list.empty();
294  }
295 
296  struct IsTarget : public std::unary_function<PObject, bool>
297  {
299  IsTarget(PObject * obj) : m_obj(obj) { }
300  bool operator()(Notifier & test) const { return m_obj == test.GetTarget(); }
301  };
302 
304  void RemoveTarget(PObject * obj)
305  {
306  this->m_list.remove_if(IsTarget(obj));
307  }
308 
310  bool operator()(PObject & obj, ParamType param)
311  {
312  if (this->m_list.empty())
313  return false;
314  for (typename List::iterator it = this->m_list.begin(); it != this->m_list.end() ; ++it)
315  (*it)(obj, param);
316  return true;
317  }
318 };
319 
321 
322 
323 #endif // PTLIB_NOTIFIER_EXT_H
324 
325 
326 // End of File ///////////////////////////////////////////////////////////////
PINDEX GetSize() const
Indicate the number of notifiers in list.
Definition: notifier_ext.h:263
PAsyncNotifierFunction(void *voidTarget, PAsyncNotifierTarget *notifierTarget)
Definition: notifier_ext.h:197
TypedCallback(const Target &target, PObject &notifier, ParamType extra)
Definition: notifier_ext.h:214
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
Definition: notifier_ext.h:296
Validated PNotifier class.
Definition: notifier_ext.h:56
static bool Exists(PNotifierIdentifer id)
This is an abstract class for which a descendent is declared for every function that may be called...
Definition: notifier_ext.h:83
PNotifierIdentifer m_targetID
Definition: notifier_ext.h:100
PObject * m_obj
Definition: notifier_ext.h:298
bool Add(const Notifier &handler)
Add a notifier to the list.
Definition: notifier_ext.h:266
virtual void AsyncNotifierSignal()
Signal the target that there are notifications pending.
virtual void Call()
Definition: notifier_ext.h:220
void * m_target
Object instance to receive the notification function call.
Definition: notifier.h:84
This is an abstract class for which a descendent is declared for every function that may be called...
Definition: notifier_ext.h:191
void RemoveTarget(PObject *obj)
Remove all notifiers that use the specified target object.
Definition: notifier_ext.h:304
bool AsyncNotifierExecute(const PTimeInterval &wait=0)
Execute any notifications queued.
Definition: notifier_ext.h:206
virtual void * GetTarget() const
Definition: notifier_ext.h:94
bool IsEmpty() const
Indicate notifier list is empty.
Definition: notifier_ext.h:291
virtual ~PAsyncNotifierCallback()
Definition: notifier_ext.h:125
Maintain a list of notifiers to be called all at once.
Definition: notifier_ext.h:253
bool operator()(PObject &obj, ParamType param)
Execute all notifiers in the list.
Definition: notifier_ext.h:310
The PNotifier and PNotifierFunction classes build a completely type safe mechanism for calling arbitr...
Definition: notifier.h:109
bool Remove(const Notifier &handler)
Remove notifier from teh list.
Definition: notifier_ext.h:275
PValidatedNotifierFunction(void *voidTarget, PValidatedNotifierTarget *notifierTarget)
Definition: notifier_ext.h:89
virtual void Call()=0
Asynchronous PNotifier class.
Definition: notifier_ext.h:145
void * GetTarget() const
Definition: notifier.h:135
PNotifierListTemplate< P_INT_PTR > PNotifierList
Definition: notifier_ext.h:320
unsigned long PNotifierIdentifer
Definition: notifier_ext.h:39
static void Queue(PNotifierIdentifer id, PAsyncNotifierCallback *callback)
bool Contains(const Notifier &handler) const
Determine if notifier is in list.
Definition: notifier_ext.h:285
PNotifierIdentifer m_targetID
Definition: notifier_ext.h:203
Definition: notifier_ext.h:123
This is an abstract class for which a descendent is declared for every function that may be called...
Definition: notifier.h:59
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
virtual ~PValidatedNotifierTarget()
virtual ~PAsyncNotifierTarget()
void operator=(const PValidatedNotifierTarget &)
Definition: notifier_ext.h:64
IsTarget(PObject *obj)
Definition: notifier_ext.h:299
bool operator()(Notifier &test) const
Definition: notifier_ext.h:300