PTLib  Version 2.12.9
 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: 28935 $
27  * $Author: csoutheren $
28  * $Date: 2013-01-19 00:12:14 +1100 (Sat, 19 Jan 2013) $
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 ParamType>
64 {
66 
67  protected:
70  void * target,
71  void * = NULL
72  ) { m_target = PAssertNULL(target); }
73 
74  public:
78  virtual void Call(
79  PObject & notifier,
80  ParamType extra
81  ) const = 0;
82 
83  virtual void * GetTarget() const { return this->m_target; }
84 
85  protected:
86  // Member variables
88  void * m_target;
89 };
90 
92 
93 
112 template <typename ParamType>
114 {
115  PCLASSINFO(PNotifierTemplate, PSmartPointer);
116 
117  public:
119 
122  FunctionPtr func = NULL
123  ) : PSmartPointer(func) { }
124 
130  virtual void operator()(
131  PObject & notifier,
132  ParamType extra
133  ) const {
134  FunctionPtr ptr = dynamic_cast<FunctionPtr>(GetObject());
135  if (PAssertNULL(ptr) != NULL)
136  ptr->Call(notifier, extra);
137  }
138 
139  void * GetTarget() const
140  {
141  FunctionPtr ptr = dynamic_cast<FunctionPtr>(GetObject());
142  return ptr != NULL ? ptr->GetTarget() : NULL;
143  }
144 };
145 
150 
151 
152 #define PDECLARE_NOTIFIER_COMMON1(notifier, notifiee, func, ParamType, BaseClass) \
153  class func##_PNotifier : public BaseClass { \
154  public: \
155  func##_PNotifier(notifiee * target) : BaseClass(target, target) { } \
156  virtual void Call(PObject & note, ParamType extra) const \
157 
158 #define PDECLARE_NOTIFIER_COMMON2(notifier, notifiee, func, ParamType, BaseClass) \
159  { notifiee * target = reinterpret_cast<notifiee *>(this->GetTarget()); \
160  if (target != NULL) \
161  target->func(reinterpret_cast<notifier &>(note), extra); \
162  } \
163  static PNotifierTemplate<ParamType> Create(notifiee * obj) { return new func##_PNotifier(obj); } \
164  static PNotifierTemplate<ParamType> Create(notifiee & obj) { return new func##_PNotifier(&obj); } \
165  }; \
166  friend class func##_PNotifier; \
167  virtual void func(notifier & note, ParamType extra) \
168 
169 #define PDECLARE_NOTIFIER_COMMON(notifier, notifiee, func, ParamType, BaseClass) \
170  PDECLARE_NOTIFIER_COMMON1(notifier, notifiee, func, ParamType, BaseClass) \
171  PDECLARE_NOTIFIER_COMMON2(notifier, notifiee, func, ParamType, BaseClass)
172 
173 
198 #define PDECLARE_NOTIFIER2(notifier, notifiee, func, ParamType) \
199  PDECLARE_NOTIFIER_COMMON(notifier, notifiee, func, ParamType, PNotifierFunctionTemplate<ParamType>)
200 
202 #define PDECLARE_NOTIFIER(notifier, notifiee, func) \
203  PDECLARE_NOTIFIER2(notifier, 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 ///////////////////////////////////////////////////////////////