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: 20385 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00029  */
00030 
00031 #ifndef _PNOTIFIER_EXT
00032 #define _PNOTIFIER_EXT
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  // _PNOTIFIER_EXT
00127 
00128 // End of File ///////////////////////////////////////////////////////////////
00129 
00130 
00131 

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1