All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
notifier_ext.h
Go to the documentation of this file.
/*
* notifier_ext.h
*
* Smart Notifiers and Notifier Lists
*
* Portable Windows Library
*
* Copyright (c) 2004 Reitek S.p.A.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Post Increment
*
* Contributor(s): ______________________________________.
*
* $Revision: 21788 $
* $Author: rjongbloed $
* $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
*/
#ifndef PTLIB_NOTIFIER_EXT_H
#define PTLIB_NOTIFIER_EXT_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
class PSmartNotifieeRegistrar
{
public:
PSmartNotifieeRegistrar() : m_ID(P_MAX_INDEX) {}
~PSmartNotifieeRegistrar() { UnregisterNotifiee(m_ID); }
void Init(void * obj) { if (m_ID == P_MAX_INDEX) m_ID = RegisterNotifiee(obj); }
unsigned GetID() const { return m_ID; }
static unsigned RegisterNotifiee(void * obj);
static PBoolean UnregisterNotifiee(unsigned id);
static PBoolean UnregisterNotifiee(void * obj);
static void * GetNotifiee(unsigned id);
protected:
unsigned m_ID;
};
class PSmartNotifierFunction : public PNotifierFunction
{
PCLASSINFO(PSmartNotifierFunction, PNotifierFunction);
protected:
unsigned m_NotifieeID;
public:
PSmartNotifierFunction(unsigned id) : PNotifierFunction(&id), m_NotifieeID(id) { }
unsigned GetNotifieeID() const { return m_NotifieeID; }
void * GetNotifiee() const { return PSmartNotifieeRegistrar::GetNotifiee(m_NotifieeID); }
PBoolean IsValid() const { return GetNotifiee() != 0; }
};
#define PDECLARE_SMART_NOTIFIEE \
PSmartNotifieeRegistrar m_Registrar; \
#define PCREATE_SMART_NOTIFIEE m_Registrar.Init(this)
#define PDECLARE_SMART_NOTIFIER(notifier, notifiee, func) \
class func##_PSmartNotifier : public PSmartNotifierFunction { \
public: \
func##_PSmartNotifier(unsigned id) : PSmartNotifierFunction(id) { } \
virtual void Call(PObject & note, INT extra) const \
{ \
void * obj = GetNotifiee(); \
if (obj) \
((notifiee*)obj)->func((notifier &)note, extra); \
else \
PTRACE(2, "PWLib\tInvalid notifiee"); \
} \
}; \
friend class func##_PSmartNotifier; \
virtual void func(notifier & note, INT extra)
#define PCREATE_SMART_NOTIFIER(func) PNotifier(new func##_PSmartNotifier(m_Registrar.GetID()))
class PNotifierList : public PObject
{
PCLASSINFO(PNotifierList, PObject);
private:
PLIST(_PNotifierList, PNotifier);
_PNotifierList m_TheList;
// Removes smart pointers to deleted objects
void Cleanup();
public:
PINDEX GetSize() const { return m_TheList.GetSize(); }
void Add(PNotifier * handler) { m_TheList.Append(handler); }
void Remove(PNotifier * handler) { m_TheList.Remove(handler); }
PBoolean RemoveTarget(PObject * obj);
PBoolean Fire(PObject& obj, INT val = 0);
// Moves all the notifiers in "that" to "this"
void Move(PNotifierList& that);
};
#endif // PTLIB_NOTIFIER_EXT_H
// End of File ///////////////////////////////////////////////////////////////
PSmartNotifieeRegistrar::~PSmartNotifieeRegistrar
~PSmartNotifieeRegistrar()
Definition: notifier_ext.h:50
PSmartNotifierFunction
Definition: notifier_ext.h:64
PSmartNotifierFunction::IsValid
PBoolean IsValid() const
Definition: notifier_ext.h:75
PSmartNotifierFunction::GetNotifieeID
unsigned GetNotifieeID() const
Definition: notifier_ext.h:73
PNotifier
Class specialisation for PNotifierTemplate<INT>
PSmartNotifieeRegistrar
Implements a function similar to the PNotifier, but uses an "id" to link the caller and callee rather...
Definition: notifier_ext.h:46
PNotifierList::RemoveTarget
PBoolean RemoveTarget(PObject *obj)
PSmartNotifieeRegistrar::PSmartNotifieeRegistrar
PSmartNotifieeRegistrar()
Definition: notifier_ext.h:49
PBoolean
BOOL PBoolean
Definition: object.h:102
PNotifierList::GetSize
PINDEX GetSize() const
Definition: notifier_ext.h:114
PSmartNotifieeRegistrar::m_ID
unsigned m_ID
Definition: notifier_ext.h:61
PNotifierList::Add
void Add(PNotifier *handler)
Definition: notifier_ext.h:116
PSmartNotifieeRegistrar::Init
void Init(void *obj)
Definition: notifier_ext.h:52
PSmartNotifierFunction::m_NotifieeID
unsigned m_NotifieeID
Definition: notifier_ext.h:69
PSmartNotifieeRegistrar::GetNotifiee
static void * GetNotifiee(unsigned id)
PSmartNotifieeRegistrar::RegisterNotifiee
static unsigned RegisterNotifiee(void *obj)
PNotifierList::Remove
void Remove(PNotifier *handler)
Definition: notifier_ext.h:117
PSmartNotifieeRegistrar::GetID
unsigned GetID() const
Definition: notifier_ext.h:53
PSmartNotifieeRegistrar::UnregisterNotifiee
static PBoolean UnregisterNotifiee(unsigned id)
PSmartNotifierFunction::GetNotifiee
void * GetNotifiee() const
Definition: notifier_ext.h:74
PNotifierFunctionTemplate
This is an abstract class for which a descendent is declared for every function that may be called...
Definition: notifier.h:63
PNotifierList
Definition: notifier_ext.h:102
PSmartNotifierFunction::PSmartNotifierFunction
PSmartNotifierFunction(unsigned id)
Definition: notifier_ext.h:72
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PNotifierList::Fire
PBoolean Fire(PObject &obj, INT val=0)
PNotifierList::Move
void Move(PNotifierList &that)