PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_NOTIFIER_H
28 #define PTLIB_NOTIFIER_H
29 
30 #include <ptlib.h>
31 #include <ptlib/smartptr.h>
32 
34 // General notification mechanism from one object to another
35 
58 template <typename ParamType>
60 {
62 
63  protected:
66  void * target,
67  void * = NULL
68  ) { m_target = PAssertNULL(target); }
69 
70  public:
74  virtual void Call(
75  PObject & notifier,
76  ParamType extra
77  ) const = 0;
78 
79  virtual void * GetTarget() const { return this->m_target; }
80 
81  protected:
82  // Member variables
84  void * m_target;
85 };
86 
88 
89 
108 template <typename ParamType>
110 {
111  PCLASSINFO(PNotifierTemplate, PSmartPointer);
112 
113  public:
115 
118  FunctionPtr func = NULL
119  ) : PSmartPointer(func) { }
120 
126  virtual void operator()(
127  PObject & notifier,
128  ParamType extra
129  ) const {
130  FunctionPtr ptr = dynamic_cast<FunctionPtr>(GetObject());
131  if (PAssertNULL(ptr) != NULL)
132  ptr->Call(notifier, extra);
133  }
134 
135  void * GetTarget() const
136  {
137  FunctionPtr ptr = dynamic_cast<FunctionPtr>(GetObject());
138  return ptr != NULL ? ptr->GetTarget() : NULL;
139  }
140 };
141 
146 
147 
148 #define PDECLARE_NOTIFIER_COMMON1(notifierType, notifiee, func, ParamType, BaseClass) \
149  class func##_PNotifier : public BaseClass { \
150  public: \
151  func##_PNotifier(notifiee * target) : BaseClass(target, target) { } \
152  virtual void Call(PObject & note, ParamType extra) const \
153 
154 #define PDECLARE_NOTIFIER_COMMON2(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, BaseClass) \
155  { notifiee * target = reinterpret_cast<notifiee *>(this->GetTarget()); \
156  if (target != NULL) \
157  target->func(reinterpret_cast<notifierType &>(note), extra); \
158  } \
159  static PNotifierTemplate<ParamType> Create(notifiee * obj) { return new func##_PNotifier(obj); } \
160  static PNotifierTemplate<ParamType> Create(notifiee & obj) { return new func##_PNotifier(&obj); } \
161  }; \
162  friend class func##_PNotifier; \
163  virtual void func(notifierType & notifierArg, ParamType ParamArg) \
164 
165 #define PDECLARE_NOTIFIER_COMMON(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, BaseClass) \
166  PDECLARE_NOTIFIER_COMMON1(notifierType, notifiee, func, ParamType, BaseClass) \
167  PDECLARE_NOTIFIER_COMMON2(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, BaseClass)
168 
169 
194 #define PDECLARE_NOTIFIER_EXT(notifierType, notifierArg, notifiee, func, ParamType, ParamArg) \
195  PDECLARE_NOTIFIER_COMMON(notifierType, notifierArg, notifiee, func, ParamType, ParamArg, PNotifierFunctionTemplate<ParamType>)
196 
198 #define PDECLARE_NOTIFIER2(notifierType, notifiee, func, ParamType ) \
199  PDECLARE_NOTIFIER_EXT(notifierType, , notifiee, func, ParamType, )
200 
202 #define PDECLARE_NOTIFIER(notifierType, notifiee, func) \
203  PDECLARE_NOTIFIER2(notifierType, notifiee, func, P_INT_PTR)
204 
205 
214 #define PCREATE_NOTIFIER2_EXT(obj, notifiee, func, type) notifiee::func##_PNotifier::Create(obj)
215 
217 #define PCREATE_NOTIFIER_EXT( obj, notifiee, func) notifiee::func##_PNotifier::Create(obj)
218 
219 
228 #define PCREATE_NOTIFIER2(func, type) P_DISABLE_MSVC_WARNINGS(4355, func##_PNotifier::Create(this))
229 
231 #define PCREATE_NOTIFIER(func) P_DISABLE_MSVC_WARNINGS(4355, func##_PNotifier::Create(this))
232 
233 
234 #endif // PTLIB_NOTIFIER_H
235 
236 
237 // End Of File ///////////////////////////////////////////////////////////////
PNotifierTemplate(FunctionPtr func=NULL)
Create a new notification function smart pointer.
Definition: notifier.h:117
PNotifierFunctionTemplate(void *target, void *=NULL)
Create a notification function instance.
Definition: notifier.h:65
void * m_target
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:111
virtual void operator()(PObject &notifier, ParamType extra) const
Execute the call to the actual notification function on the object instance contained in this object...
Definition: notifier.h:126
virtual void Call(PObject &notifier, ParamType extra) const =0
Execute the call to the actual notification function on the object instance contained in this object...
The PNotifier and PNotifierFunction classes build a completely type safe mechanism for calling arbitr...
Definition: notifier.h:109
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:428
PNotifierFunctionTemplate< ParamType > * FunctionPtr
Definition: notifier.h:114
virtual void * GetTarget() const
Definition: notifier.h:79
PNotifierTemplate< P_INT_PTR > PNotifier
Definition: notifier.h:145
void * GetTarget() const
Definition: notifier.h:135
This is the base class for objects that use the smart pointer system.
Definition: smartptr.h:49
PNotifierFunctionTemplate< P_INT_PTR > PNotifierFunction
Definition: notifier.h:87
This is an abstract class for which a descendent is declared for every function that may be called...
Definition: notifier.h:59
PSmartObject * GetObject() const
Get the current value if the internal smart object pointer.
Definition: smartptr.h:185
Ultimate parent class for all objects in the class library.
Definition: object.h:2204