posts - 36, comments - 30, trackbacks - 0, articles - 0

Dispatech typelist实现

Posted on 2008-02-20 17:22 vcommon 阅读(258) 评论(0)  编辑 收藏 引用

TypeList 的Dispatech 好处和坏处
1:用处不大,
编译期决定将某些event送入某些处理函数,所以限制了函数一般会用全局的函数,更适合C语言
2优点
和delegate一样,在一个总的整体控制 的模式下,使外边看结构很简洁

只需要把相应的event发给control就可以了,而且control的扩展性很强,随意增加event和相应的处理


// Dispatech
 // TL为TypeList
 // F<int >为实现函数的类
 template<typename TL,template <int> class F,typename FUNC>
 class Dispetcher
 {
 map<int,FUNC> m_function;

 template<typename H>
 void Build()
 {
 pair<int,FUNC> t_pair;
 t_pair.first=H::Head::value;
 t_pair.second=F<H::Head::value>::Update;

 m_function.insert(t_pair);

 Build<H::Tail>();
 };
 template<>
 void Build<NullType>(){};
 public:
 Dispetcher()
 {
 Build<TL>();
 };
 FUNC GetFunc(int id)
 {
 map<int,FUNC>::iterator t_iterator;

 t_iterator=m_function.find(id);

 if (t_iterator!=m_function.end())
 return t_iterator->second;

 return NULL;;
 };
 };


typedef HRESULT (* TEST_FUNC)(long);


template<int>
class CEvent_Test
{
public:
 static HRESULT Update(long param);
};

void test()
{
typedef Typelist<Int2Type<EVENT_TEST>, ::NullType> typelist_eventtest;

 Dispetcher<typelist_eventtest,CEvent_Test,TEST_FUNC> disps;
}

只有注册用户登录后才能发表评论。