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  * $Log: notifier_ext.h,v $
00027  * Revision 1.6  2007/04/02 05:29:54  rjongbloed
00028  * Tidied some trace logs to assure all have a category (bit before a tab character) set.
00029  *
00030  * Revision 1.5  2005/11/30 12:47:37  csoutheren
00031  * Removed tabs, reformatted some code, and changed tags for Doxygen
00032  *
00033  * Revision 1.4  2004/05/17 11:02:39  csoutheren
00034  * Added extra documentation
00035  *
00036  * Revision 1.3  2004/05/09 07:23:48  rjongbloed
00037  * More work on XMPP, thanks Federico Pinna and Reitek S.p.A.
00038  *
00039  * Revision 1.2  2004/04/26 01:34:58  rjongbloed
00040  * Change nofier list to be able to used in containers, thanks Federico Pinna, Reitek S.p.A.
00041  *
00042  * Revision 1.1  2004/04/22 12:31:00  rjongbloed
00043  * Added PNotifier extensions and XMPP (Jabber) support,
00044  *   thanks to Federico Pinna and Reitek S.p.A.
00045  *
00046  *
00047  */
00048 
00049 #ifndef _PNOTIFIER_EXT
00050 #define _PNOTIFIER_EXT
00051 
00052 #ifdef P_USE_PRAGMA
00053 #pragma interface
00054 #endif
00055 
00064 class PSmartNotifieeRegistrar
00065 {
00066   public:
00067     PSmartNotifieeRegistrar() : m_ID(P_MAX_INDEX) {}
00068     ~PSmartNotifieeRegistrar() { UnregisterNotifiee(m_ID); }
00069 
00070     void        Init(void * obj)        { if (m_ID == P_MAX_INDEX) m_ID = RegisterNotifiee(obj); }
00071     unsigned    GetID() const           { return m_ID; }
00072 
00073     static unsigned    RegisterNotifiee(void * obj);
00074     static BOOL        UnregisterNotifiee(unsigned id);
00075     static BOOL        UnregisterNotifiee(void * obj);
00076     static void *      GetNotifiee(unsigned id);
00077 
00078   protected:
00079     unsigned m_ID;
00080 };
00081 
00082 class PSmartNotifierFunction : public PNotifierFunction
00083 {
00084     PCLASSINFO(PSmartNotifierFunction, PNotifierFunction);
00085 
00086   protected:
00087     unsigned m_NotifieeID;
00088 
00089   public:
00090     PSmartNotifierFunction(unsigned id) : PNotifierFunction(&id), m_NotifieeID(id) { }
00091     unsigned GetNotifieeID() const { return m_NotifieeID; }
00092     void * GetNotifiee() const { return PSmartNotifieeRegistrar::GetNotifiee(m_NotifieeID); }
00093     BOOL IsValid() const { return GetNotifiee() != 0; }
00094 };
00095 
00096 #define PDECLARE_SMART_NOTIFIEE \
00097     PSmartNotifieeRegistrar   m_Registrar; \
00098 
00099 #define PCREATE_SMART_NOTIFIEE m_Registrar.Init(this)
00100 
00101 #define PDECLARE_SMART_NOTIFIER(notifier, notifiee, func) \
00102   class func##_PSmartNotifier : public PSmartNotifierFunction { \
00103     public: \
00104       func##_PSmartNotifier(unsigned id) : PSmartNotifierFunction(id) { } \
00105       virtual void Call(PObject & note, INT extra) const \
00106       { \
00107           void * obj = GetNotifiee(); \
00108           if (obj) \
00109             ((notifiee*)obj)->func((notifier &)note, extra); \
00110           else \
00111             PTRACE(2, "PWLib\tInvalid notifiee"); \
00112       } \
00113   }; \
00114   friend class func##_PSmartNotifier; \
00115   virtual void func(notifier & note, INT extra)
00116 
00117 #define PCREATE_SMART_NOTIFIER(func) PNotifier(new func##_PSmartNotifier(m_Registrar.GetID()))
00118 
00119 
00120 class PNotifierList : public PObject
00121 {
00122   PCLASSINFO(PNotifierList, PObject);
00123   private:
00124     PLIST(_PNotifierList, PNotifier);
00125 
00126     _PNotifierList m_TheList;
00127 
00128     // Removes smart pointers to deleted objects
00129     void   Cleanup();
00130 
00131   public:
00132     PINDEX GetSize() const { return m_TheList.GetSize(); }
00133 
00134     void Add(PNotifier * handler)       { m_TheList.Append(handler); }
00135     void Remove(PNotifier * handler)    { m_TheList.Remove(handler); }
00136     BOOL RemoveTarget(PObject * obj);
00137     BOOL Fire(PObject& obj, INT val = 0);
00138 
00139     // Moves all the notifiers in "that" to "this"
00140     void  Move(PNotifierList& that);
00141 };
00142 
00143 
00144 #endif  // _PNOTIFIER_EXT
00145 
00146 // End of File ///////////////////////////////////////////////////////////////
00147 
00148 
00149 

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1