00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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
00122 void Move(PNotifierList& that);
00123 };
00124
00125
00126 #endif // _PNOTIFIER_EXT
00127
00128
00129
00130
00131