#ifndef CHAIN_1_STRUCT_H #define CHAIN_1_STRUCT_H #include #include #include namespace handler_chain { template struct NodeBase { private: typedef void (*FP1)(void* , void*); public: typedef typename ADAPTER::RET_TYPE RET_TYPE; typedef typename ADAPTER::VALUE_TYPE VALUE_TYPE; NodeBase() : p_(0), fn_(0) {} template void setNext(T& x) { p_ = &x; fn_ = &functions::processImpl; } void operator()(VALUE_TYPE* v) { if( ADAPTER()(NODE(), v ) ) return; if (p_==0 || fn_== 0) { std::cout << "No Result Found\n"; return; } fn_(p_, v); } private: template struct functions { static void processImpl( void* a , void* v) { (*static_cast(a))(static_cast(v)); } }; void* p_; FP1 fn_; }; template struct Adapter { typedef R RET_TYPE; typedef V VALUE_TYPE; template RET_TYPE operator()( const H& h, VALUE_TYPE* v) const { return h.handleEvent(v); } }; struct Connection { template bool handleEvent(V* v ) const { std::cout << "Connection\n" ; return *v < 1 ? true : false; } }; struct Lsp { template bool handleEvent(V* v ) const { std::cout << "Lsp\n" ; return *v < 3 ? true : false; } }; struct Path { template bool handleEvent( V* v ) const { std::cout << "Path\n" ; return *v < 5 ? true : false; } }; class INode { private: typedef void (*FP1)(void* , void*); public: template INode(T& x) : p_(&x), fn_(&functions::processImpl) {} template void operator()(V* v) { fn_(p_, v); } private: template struct functions { static void processImpl( void* a, void* v) { (*static_cast(a)) (static_cast(v) ); } }; void* p_; FP1 fn_; }; template INode* createChain( Head& head) { return new INode(head); } void test() { std::cout << "TESTING HANDLER_CHAIN1\n" ; typedef Adapter ADAPTER; NodeBase connection; NodeBase lsp; NodeBase path; connection.setNext(lsp); lsp.setNext(path); int k = 1; connection(&k); std::auto_ptr i(createChain(connection)); k = 10; (*i)(&k); } } #endif