# notifier_ext.h

Go to the documentation of this file.

    00001 /*
    00002  * notifier_ext.h
    00003  *
    00004  * Smart Notifiers and Notifier Lists
    00005  *
    00006  * Portable Windows Library
    00007  *
    00008  * Copyright (c) 2004 Reitek S.p.A.
    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 Post Increment
    00023  *
    00024  * Contributor(s): ______________________________________.
    00025  *
    00026  * $Revision: 21788 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
    00029  */
    00030
    00031 #ifndef PTLIB_NOTIFIER_EXT_H
    00032 #define PTLIB_NOTIFIER_EXT_H
    00033
    00034 #ifdef P_USE_PRAGMA
    00035 #pragma interface
    00036 #endif
    00037
    00046 class PSmartNotifieeRegistrar
    00047 {
    00048   public:
    00049     PSmartNotifieeRegistrar() : m_ID(P_MAX_INDEX) {}
    00050     ~PSmartNotifieeRegistrar() { UnregisterNotifiee(m_ID); }
    00051
    00052     void        Init(void * obj)        { if (m_ID == P_MAX_INDEX) m_ID = RegisterNotifiee(obj); }
    00053     unsigned    GetID() const           { return m_ID; }
    00054
    00055     static unsigned    RegisterNotifiee(void * obj);
    00056     static PBoolean        UnregisterNotifiee(unsigned id);
    00057     static PBoolean        UnregisterNotifiee(void * obj);
    00058     static void *      GetNotifiee(unsigned id);
    00059
    00060   protected:
    00061     unsigned m_ID;
    00062 };
    00063
    00064 class PSmartNotifierFunction : public PNotifierFunction
    00065 {
    00066     PCLASSINFO(PSmartNotifierFunction, PNotifierFunction);
    00067
    00068   protected:
    00069     unsigned m_NotifieeID;
    00070
    00071   public:
    00072     PSmartNotifierFunction(unsigned id) : PNotifierFunction(&id), m_NotifieeID(id) { }
    00073     unsigned GetNotifieeID() const { return m_NotifieeID; }
    00074     void * GetNotifiee() const { return PSmartNotifieeRegistrar::GetNotifiee(m_NotifieeID); }
    00075     PBoolean IsValid() const { return GetNotifiee() != 0; }
    00076 };
    00077
    00078 #define PDECLARE_SMART_NOTIFIEE \
    00079     PSmartNotifieeRegistrar   m_Registrar; \
    00080
    00081 #define PCREATE_SMART_NOTIFIEE m_Registrar.Init(this)
    00082
    00083 #define PDECLARE_SMART_NOTIFIER(notifier, notifiee, func) \
    00084   class func##_PSmartNotifier : public PSmartNotifierFunction { \
    00085     public: \
    00086       func##_PSmartNotifier(unsigned id) : PSmartNotifierFunction(id) { } \
    00087       virtual void Call(PObject & note, INT extra) const \
    00088       { \
    00089           void * obj = GetNotifiee(); \
    00090           if (obj) \
    00091             ((notifiee*)obj)->func((notifier &)note, extra); \
    00092           else \
    00093             PTRACE(2, "PWLib\tInvalid notifiee"); \
    00094       } \
    00095   }; \
    00096   friend class func##_PSmartNotifier; \
    00097   virtual void func(notifier & note, INT extra)
    00098
    00099 #define PCREATE_SMART_NOTIFIER(func) PNotifier(new func##_PSmartNotifier(m_Registrar.GetID()))
    00100
    00101
    00102 class PNotifierList : public PObject
    00103 {
    00104   PCLASSINFO(PNotifierList, PObject);
    00105   private:
    00106     PLIST(_PNotifierList, PNotifier);
    00107
    00108     _PNotifierList m_TheList;
    00109
    00110     // Removes smart pointers to deleted objects
    00111     void   Cleanup();
    00112
    00113   public:
    00114     PINDEX GetSize() const { return m_TheList.GetSize(); }
    00115
    00116     void Add(PNotifier * handler)       { m_TheList.Append(handler); }
    00117     void Remove(PNotifier * handler)    { m_TheList.Remove(handler); }
    00118     PBoolean RemoveTarget(PObject * obj);
    00119     PBoolean Fire(PObject& obj, INT val = 0);
    00120
    00121     // Moves all the notifiers in "that" to "this"
    00122     void  Move(PNotifierList& that);
    00123 };
    00124
    00125
    00126 #endif  // PTLIB_NOTIFIER_EXT_H
    00127
    00128
    00129 // End of File ///////////////////////////////////////////////////////////////