ultimatepp/bazaar/Dispatcher/Dispatcher.cpp
kohait 416bb85ad2 bazaar: Dispatcher: Link version, Gen: Visiting interface, WhenEnterAction: more flexible solution,
git-svn-id: svn://ultimatepp.org/upp/trunk@3098 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2011-01-26 09:52:56 +00:00

102 lines
No EOL
2 KiB
C++

#include "Dispatcher.hpp"
DispatcherGen::DispatcherGen()
{
}
DispatcherGen::~DispatcherGen()
{
//ASSERT(dests.IsEmpty()); //FIXME, DispatcherGen does keep the Dispatcher<T> instances, cause Dispatchable<T> are Register()ed there (forwarded) and do Unregister() only there as well.
}
//Callback variant
DispatcherCBGen::DispatcherCBGen()
{
}
DispatcherCBGen::~DispatcherCBGen()
{
//ASSERT(dests.IsEmpty()); //FIXME, DispatcherGen does keep the Dispatcher<T> instances, cause Dispatchable<T> are Register()ed there (forwarded) and do Unregister() only there as well.
}
//Dispatcher0
Dispatcher0::Dispatcher0()
{
}
Dispatcher0::~Dispatcher0()
{
ASSERT(dests.IsEmpty());
}
void Dispatcher0::DoDispatch() const
{
if(!R::IsEnabled()) return;
for(int i = 0; i < dests.GetCount(); i++)
{
const Callback & dest = dests.operator[](i);
dest();
}
}
void Dispatcher0::Register(Callback d, unsigned key)
{
int i = dests.Find(key);
if(i>=0) return;
dests.Add(key) = d;
}
void Dispatcher0::Unregister(unsigned key)
{
int i = dests.Find(key);
if(i<0) return;
dests.Remove(i);
}
Callback* Dispatcher0::GetDispatchable(unsigned key)
{
int i = dests.Find(key);
if(i<0) return NULL;
Callback & dest = dests.operator[](i);
return &dest;
}
//DispatcherL0
DispatcherL0::DispatcherL0()
{
}
DispatcherL0::~DispatcherL0()
{
ASSERT(dests.IsEmpty());
}
void DispatcherL0::DoDispatch() const
{
int c = 0;
const Handler *list = dests.GetNext(), *e = list;
do
{
++c;
e->h();
e = e->GetNext();
}
while(e != list);
}
#if 0 //for compile / debug only
template class Dispatchable<int>;
template class Dispatcher<int>;
template void DispatcherGen::DoDispatch<int>(const int & o, unsigned param) const;
template void DispatcherGen::Register<int>(Dispatchable<int> & d, unsigned key);
template void DispatcherGen::Unregister<int>(Dispatchable<int> & d, unsigned key);
DispatcherGen gen;
DispatcherCBGen gencb;
#endif