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