Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

yuriks

By: a guest on Dec 2nd, 2008  |  syntax: C++  |  size: 0.49 KB  |  views: 345  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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;