#ifndef ARM_COMP_ATTACH_PT_T_H_INCLUDED
#define ARM_COMP_ATTACH_PT_T_H_INCLUDED
#include <vector>
#include "opencsd/ocsd_if_types.h"
#include "comp_attach_notifier_i.h"
template <class T>
class componentAttachPt {
public:
componentAttachPt();
virtual ~componentAttachPt();
virtual ocsd_err_t attach(T* component);
virtual ocsd_err_t detach(T* component);
virtual ocsd_err_t replace_first(T* component);
virtual void detach_all();
virtual T* first();
virtual T* next();
virtual int num_attached();
void set_notifier(IComponentAttachNotifier *notifier);
const bool enabled() const;
void set_enabled(const bool enable);
const bool hasAttached() const { return m_hasAttached; };
const bool hasAttachedAndEnabled() const { return m_hasAttached && m_enabled; };
protected:
bool m_enabled;
bool m_hasAttached;
IComponentAttachNotifier *m_notifier;
T *m_comp;
};
template<class T> componentAttachPt<T>::componentAttachPt()
{
m_comp = 0;
m_notifier = 0;
m_enabled = true;
m_hasAttached = false;
}
template<class T> componentAttachPt<T>::~componentAttachPt()
{
detach_all();
}
template<class T> ocsd_err_t componentAttachPt<T>::attach(T* component)
{
if(m_comp != 0)
return OCSD_ERR_ATTACH_TOO_MANY;
m_comp = component;
if(m_notifier) m_notifier->attachNotify(1);
m_hasAttached = true;
return OCSD_OK;
}
template<class T> ocsd_err_t componentAttachPt<T>::replace_first(T* component)
{
if(m_hasAttached)
detach(m_comp);
if(component == 0)
return OCSD_OK;
return attach(component);
}
template<class T> ocsd_err_t componentAttachPt<T>::detach(T* component)
{
if(m_comp != component)
return OCSD_ERR_ATTACH_COMP_NOT_FOUND;
m_comp = 0;
m_hasAttached = false;
if(m_notifier) m_notifier->attachNotify(0);
return OCSD_OK;
}
template<class T> T* componentAttachPt<T>::first()
{
return (m_enabled) ? m_comp : 0;
}
template<class T> T* componentAttachPt<T>::next()
{
return 0;
}
template<class T> int componentAttachPt<T>::num_attached()
{
return ((m_comp != 0) ? 1 : 0);
}
template<class T> void componentAttachPt<T>::detach_all()
{
m_comp = 0;
m_hasAttached = false;
if(m_notifier) m_notifier->attachNotify(0);
}
template<class T> void componentAttachPt<T>::set_notifier(IComponentAttachNotifier *notifier)
{
m_notifier = notifier;
}
template<class T> const bool componentAttachPt<T>::enabled() const
{
return m_enabled;
}
template<class T> void componentAttachPt<T>::set_enabled(const bool enable)
{
m_enabled = enable;
}
#endif