# notifier.h

Go to the documentation of this file.

    00001 /*
    00002  * notifier.h
    00003  *
    00004  * Notified type safe function pointer class.
    00005  *
    00006  * Portable Tools Library
    00007  *
    00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
    00009  *
    00010  * The contents of this file are subject to the Mozilla Public License
    00011  * Version 1.0 (the "License"); you may not use this file except in
    00012  * compliance with the License. You may obtain a copy of the License at
    00013  * http://www.mozilla.org/MPL/
    00014  *
    00015  * Software distributed under the License is distributed on an "AS IS"
    00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00017  * the License for the specific language governing rights and limitations
    00018  * under the License.
    00019  *
    00020  * The Original Code is Portable Windows Library.
    00021  *
    00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
    00023  *
    00024  * Contributor(s): ______________________________________.
    00025  *
    00026  * $Revision: 24177 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
    00029  */
    00030
    00031 #ifndef PTLIB_NOTIFIER_H
    00032 #define PTLIB_NOTIFIER_H
    00033
    00034 #include <ptlib.h>
    00035 #include <ptlib/smartptr.h>
    00036
    00038 // General notification mechanism from one object to another
    00039
    00062 template <typename ParmType>
    00063 class PNotifierFunctionTemplate : public PSmartObject
    00064 {
    00065   PCLASSINFO(PNotifierFunctionTemplate, PSmartObject);
    00066
    00067   public:
    00069     PNotifierFunctionTemplate(
    00070       void * obj
    00071     ) { object = PAssertNULL(obj); }
    00072
    00076     virtual void Call(
    00077       PObject & notifier,
    00078       ParmType extra
    00079     ) const = 0;
    00080
    00081   protected:
    00082     // Member variables
    00084     void * object;
    00085 };
    00086
    00087 typedef PNotifierFunctionTemplate<INT> PNotifierFunction;
    00088
    00089
    00108 template <typename ParmType>
    00109 class PNotifierTemplate : public PSmartPointer
    00110 {
    00111   PCLASSINFO(PNotifierTemplate, PSmartPointer);
    00112
    00113   public:
    00115     PNotifierTemplate(
    00116       PNotifierFunctionTemplate<ParmType> * func = NULL
    00117     ) : PSmartPointer(func) { }
    00118
    00124     virtual void operator()(
    00125       PObject & notifier,
    00126       ParmType extra
    00127     ) const {
    00128       if (PAssertNULL(object) != NULL)
    00129         ((PNotifierFunctionTemplate<ParmType>*)object)->Call(notifier,extra);
    00130     }
    00131 };
    00132
    00136 typedef PNotifierTemplate<INT> PNotifier;
    00137
    00138
    00163 #define PDECLARE_NOTIFIER2(notifier, notifiee, func, type) \
    00164   class func##_PNotifier : public PNotifierFunctionTemplate<type> { \
    00165     public: \
    00166       func##_PNotifier(notifiee * obj) : PNotifierFunctionTemplate<type>(obj) { } \
    00167       virtual void Call(PObject & note, type extra) const \
    00168         { ((notifiee*)object)->func((notifier &)note, extra); } \
    00169   }; \
    00170   friend class func##_PNotifier; \
    00171   virtual void func(notifier & note, type extra)
    00172
    00174 #define PDECLARE_NOTIFIER(notifier, notifiee, func) \
    00175   PDECLARE_NOTIFIER2(notifier, notifiee, func, INT)
    00176
    00177
    00186 #define PCREATE_NOTIFIER2_EXT(obj, notifiee, func, type) PNotifierTemplate<type>(new notifiee::func##_PNotifier(obj))
    00187
    00189 #define PCREATE_NOTIFIER_EXT( obj, notifiee, func) PCREATE_NOTIFIER2_EXT(obj, notifiee, func, INT)
    00190
    00191
    00200 #define PCREATE_NOTIFIER2(func, type) PNotifierTemplate<type>(new func##_PNotifier(this))
    00201
    00203 #define PCREATE_NOTIFIER(func) PCREATE_NOTIFIER2(func, INT)
    00204
    00205
    00206 #endif // PTLIB_NOTIFIER_H
    00207
    00208
    00209 // End Of File ///////////////////////////////////////////////////////////////