PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
notifier.h
Go to the documentation of this file.
1 /*
2  * notifier.h
3  *
4  * Notified type safe function pointer class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
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 Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 24177 $
27  * $Author: rjongbloed $
28  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
29  */
30 
31 #ifndef PTLIB_NOTIFIER_H
32 #define PTLIB_NOTIFIER_H
33 
34 #include <ptlib.h>
35 #include <ptlib/smartptr.h>
36 
38 // General notification mechanism from one object to another
39 
62 template <typename ParmType>
64 {
66 
67  public:
70  void * obj
71  ) { object = PAssertNULL(obj); }
72 
76  virtual void Call(
77  PObject & notifier,
78  ParmType extra
79  ) const = 0;
80 
81  protected:
82  // Member variables
84  void * object;
85 };
86 
88 
89 
108 template <typename ParmType>
110 {
111  PCLASSINFO(PNotifierTemplate, PSmartPointer);
112 
113  public:
117  ) : PSmartPointer(func) { }
118 
124  virtual void operator()(
125  PObject & notifier,
126  ParmType extra
127  ) const {
128  if (PAssertNULL(object) != NULL)
129  ((PNotifierFunctionTemplate<ParmType>*)object)->Call(notifier,extra);
130  }
131 };
132 
137 
138 
163 #define PDECLARE_NOTIFIER2(notifier, notifiee, func, type) \
164  class func##_PNotifier : public PNotifierFunctionTemplate<type> { \
165  public: \
166  func##_PNotifier(notifiee * obj) : PNotifierFunctionTemplate<type>(obj) { } \
167  virtual void Call(PObject & note, type extra) const \
168  { ((notifiee*)object)->func((notifier &)note, extra); } \
169  }; \
170  friend class func##_PNotifier; \
171  virtual void func(notifier & note, type extra)
172 
174 #define PDECLARE_NOTIFIER(notifier, notifiee, func) \
175  PDECLARE_NOTIFIER2(notifier, notifiee, func, INT)
176 
177 
186 #define PCREATE_NOTIFIER2_EXT(obj, notifiee, func, type) PNotifierTemplate<type>(new notifiee::func##_PNotifier(obj))
187 
189 #define PCREATE_NOTIFIER_EXT( obj, notifiee, func) PCREATE_NOTIFIER2_EXT(obj, notifiee, func, INT)
190 
191 
200 #define PCREATE_NOTIFIER2(func, type) PNotifierTemplate<type>(new func##_PNotifier(this))
201 
203 #define PCREATE_NOTIFIER(func) PCREATE_NOTIFIER2(func, INT)
204 
205 
206 #endif // PTLIB_NOTIFIER_H
207 
208 
209 // End Of File ///////////////////////////////////////////////////////////////
PNotifierTemplate(PNotifierFunctionTemplate< ParmType > *func=NULL)
Create a new notification function smart pointer.
Definition: notifier.h:115
void * object
Object instance to receive the notification function call.
Definition: notifier.h:84
This is the class for pointers to objects that use the smart pointer system.
Definition: smartptr.h:88
virtual void Call(PObject &notifier, ParmType extra) const =0
Execute the call to the actual notification function on the object instance contained in this object...
PNotifierFunctionTemplate< INT > PNotifierFunction
Definition: notifier.h:87
PNotifierTemplate< INT > PNotifier
Definition: notifier.h:136
The PNotifier and PNotifierFunction classes build a completely type safe mechanism for calling arbitr...
Definition: notifier.h:109
PNotifierFunctionTemplate(void *obj)
Create a notification function instance.
Definition: notifier.h:69
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:220
This is the base class for objects that use the smart pointer system.
Definition: smartptr.h:52
This is an abstract class for which a descendent is declared for every function that may be called...
Definition: notifier.h:63
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
virtual void operator()(PObject &notifier, ParmType extra) const
Execute the call to the actual notification function on the object instance contained in this object...
Definition: notifier.h:124