SHARE
TWEET

yuriks

a guest Dec 2nd, 2008 433 Never
  1. // PacketFactory.cpp
  2.  
  3. template<class T> Packet *makePacket()
  4. {
  5.         return static_cast<Packet *>(new T());
  6. }
  7.  
  8. PacketFactory::PacketFactory()
  9. {
  10.         // Register packet types here
  11.         idMap[PacketFactory::PACKET_ACKNOWLEDGE] = makePacket<PacketAcknowledge>;
  12. }
  13.  
  14. Packet *PacketFactory::createFromId(PacketIds id)
  15. {
  16.         IdMapType::const_iterator mapping = idMap.find(id);
  17.  
  18.         if (mapping == idMap.end())
  19.                 throw UnknownPacketException();
  20.  
  21.         return mapping->second();
  22. }
  23.  
  24. PacketFactory globalPacketFactory;
RAW Paste Data
Top