#ifndef LifecycleContext_h
#define LifecycleContext_h
#include "platform/LifecycleNotifier.h"
#include "platform/LifecycleObserver.h"
#include "wtf/HashSet.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
namespace WebCore {
template <typename T>
class LifecycleContext {
public:
typedef LifecycleNotifier<T> Notifier;
typedef LifecycleObserver<T> Observer;
LifecycleContext() { }
virtual ~LifecycleContext() { }
virtual bool isContextThread() const { return true; }
void wasObservedBy(Observer*);
void wasUnobservedBy(Observer*);
protected:
Notifier& lifecycleNotifier();
private:
PassOwnPtr<Notifier> createLifecycleNotifier();
OwnPtr<Notifier> m_lifecycleNotifier;
};
template<typename T>
inline void LifecycleContext<T>::wasObservedBy(typename LifecycleContext<T>::Observer* observer)
{
ASSERT(isContextThread());
lifecycleNotifier().addObserver(observer);
}
template<typename T>
inline void LifecycleContext<T>::wasUnobservedBy(typename LifecycleContext<T>::Observer* observer)
{
ASSERT(isContextThread());
lifecycleNotifier().removeObserver(observer);
}
template<typename T>
inline typename LifecycleContext<T>::Notifier& LifecycleContext<T>::lifecycleNotifier()
{
if (!m_lifecycleNotifier)
m_lifecycleNotifier = static_cast<T*>(this)->createLifecycleNotifier();
return *m_lifecycleNotifier;
}
template<typename T>
inline PassOwnPtr<typename LifecycleContext<T>::Notifier> LifecycleContext<T>::createLifecycleNotifier()
{
return LifecycleContext<T>::Notifier::create(static_cast<T*>(this));
}
}
#endif