📄 notifier.h
字号:
/*************************************************************************** * * Oxford Semiconductor Proprietary and Confidential Information * * Copyright: Oxford Semiconductor Ltd, 2003 * * Description: * * $Log: Notifier.h,v $ * Revision 1.11 2004/06/28 14:02:11 clarke * Fixed bugzilla bugs 348, 349, 350, 375, moved Vector from Misc to Utils, added initial support for signals for Tao port work - known issue with not clearing pending signal flag * * Revision 1.10 2004/06/15 10:16:52 ford * efficiency and definition changes. * * Revision 1.9 2004/01/09 12:44:38 clarke * Support IIB stats gathering * * Revision 1.8 2003/10/03 15:34:13 andy * line endings * * Revision 1.7 2003/09/23 08:00:15 paul * *** empty log message *** * ***************************************************************************/#ifndef NOTIFIER_H#define NOTIFIER_H#include "SysLib/Lock.h"#include "Utils/Vector.h"#include "SysLib/oxsemi_sem.h"#include <algorithm>#include <functional>#include <utility>namespace oxsemi{ template <class T> class Notifier { public: virtual ~Notifier() = 0; bool AddListener(T* listener); void RemoveListener(T* listener); protected: explicit Notifier(unsigned maxListeners); void NotifyListeners(); virtual void DoNotify(T* listener) = 0; typename Vector<T*>::size_type GetNumberOfListeners(); private: template <class U> class NotifyFunctor : public std::unary_function<U*, void> { public: NotifyFunctor<U>(Notifier<U>* notifier) : notifier_(notifier) { } void operator()(U* listener) { notifier_->DoNotify(listener); } private: Notifier<U>* notifier_; }; typedef Vector<T*> ListenerContainerType; syslib::oxsemi_bsem semaphore_; ListenerContainerType listeners_; }; template<class T> Notifier<T>::Notifier(unsigned maxListeners) : listeners_(maxListeners) { } template <class T> Notifier<T>::~Notifier() { } template<class T> inline bool Notifier<T>::AddListener(T* listener) { bool status = true; syslib::Lock lock(semaphore_); if (listeners_.size() >= listeners_.max_size()) { status = false; } else { listeners_.insert(std::lower_bound(listeners_.begin(), listeners_.end(), listener), listener); } return status; } template<class T> inline typename Vector<T*>::size_type Notifier<T>::GetNumberOfListeners() { syslib::Lock lock(semaphore_); return listeners_.size(); } template<class T> inline void Notifier<T>::NotifyListeners() { syslib::Lock lock(semaphore_); std::for_each(listeners_.begin(), listeners_.end(), NotifyFunctor<T>(this)); } template<class T> inline void Notifier<T>::RemoveListener(T* listener) { if ( !listener ) { // nothing to remove. return; } syslib::Lock lock(semaphore_); if (listeners_.size() > 0) { std::pair<typename ListenerContainerType::iterator, typename ListenerContainerType::iterator> searchResult = std::equal_range(listeners_.begin(), listeners_.end(), listener); if (searchResult.first != searchResult.second) { listeners_.erase(searchResult.first); } } }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -