#ifndef _RYL_SERVERLIB_PACKET_DISPATHCH_TABLE_H_ #define _RYL_SERVERLIB_PACKET_DISPATHCH_TABLE_H_ #include #include template class CPacketDispatchTable { public: typedef std::pair DispatchType; typedef std::vector DispatchMap; FunctionType GetDispatch(unsigned long dwCmd); protected: struct PredFirstPairOnly { template bool operator () (const std::pair& lhs, const std::pair& rhs) { return lhs.first < rhs.first; } }; CPacketDispatchTable(size_t nReservedSize) { m_DispatchMap.reserve(nReservedSize); } bool AddDispatch(unsigned long dwCmd, FunctionType fnDispatch); void RemoveDispatch(unsigned long dwCmd); private: DispatchMap m_DispatchMap; PredFirstPairOnly m_PredFirstPairOnly; }; template FunctionType CPacketDispatchTable::GetDispatch(unsigned long dwCmd) { DispatchType findVal(dwCmd, FunctionType()); DispatchMap::iterator end = m_DispatchMap.end(); DispatchMap::iterator pos = std::lower_bound(m_DispatchMap.begin(), end, findVal, m_PredFirstPairOnly); return (pos != end && !m_PredFirstPairOnly(findVal, *pos)) ? pos->second : findVal.second; } template bool CPacketDispatchTable::AddDispatch(unsigned long dwCmd, FunctionType lpDispatch) { DispatchType addVal(dwCmd, lpDispatch); DispatchMap::iterator end = m_DispatchMap.end(); DispatchMap::iterator pos = std::lower_bound(m_DispatchMap.begin(), end, addVal, m_PredFirstPairOnly); if(pos == end || m_PredFirstPairOnly(addVal, *pos)) { // ¶È°°Àº Ä¿¸Çµå·Î »ðÀÔ ½Ãµµ. ±×³É ³Ö´Â´Ù. m_DispatchMap.insert(pos, addVal); return true; } return false; } template void CPacketDispatchTable::RemoveDispatch(unsigned long dwCmd) { DispatchType removeVal(dwCmd, FunctionType()); DispatchMap::iterator end = m_DispatchMap.end(); DispatchMap::iterator pos = std::lower_bound(m_DispatchMap.begin(), end, removeVal, m_PredFirstPairOnly); if(pos != end && !m_PredFirstPairOnly(removeVal, *pos)) { // ¸Ê¿¡¼­ Á¦°ÅÇÑ´Ù. m_DispatchMap.erase(pos); } } #endif