PacketHandler.hh

Go to the documentation of this file.
00001 /*
00002 ** TowBowlTactics, a turn-based strategy football game.
00003 ** 
00004 ** Copyright (C) 2006-2010 The TBT Team.
00005 ** 
00006 ** This program is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU General Public License
00008 ** as published by the Free Software Foundation; either version 2
00009 ** of the License, or (at your option) any later version.
00010 ** 
00011 ** The complete GNU General Public Licence Notice can be found as the
00012 ** `NOTICE' file in the root directory.
00013 ** 
00014 ** The TBT Team consists of people listed in the `AUTHORS' file.
00015 */
00016 
00017 #ifndef PACKETHANDLER_HH_
00018 # define PACKETHANDLER_HH_
00019 
00021 
00022 # include "tools.hh"
00023 # include "Packet.hh"
00024 # include "EventProcess.hh"
00025 
00031 class BasePacketHandler
00032 {
00033 public:
00034   virtual ~BasePacketHandler() {}
00035   virtual void handle(const Packet* p) = 0;
00036   virtual int getCstValue() const = 0;
00037   virtual void dispatchUIEvent(const EventProcess* evp,
00038                                const Packet& pkt) = 0;
00039 };
00040 
00043 template <int N, typename T>
00044 class PacketHandler : public BasePacketHandler
00045 {
00046 };
00047 
00067 # define DECLARE_PACKET(Cst, PClass)                            \
00068 struct PClass;                                                  \
00069 template <typename T>                                           \
00070 class PacketHandler<Cst, T> : public BasePacketHandler          \
00071 {                                                               \
00072 public:                                                         \
00073   typedef void (T::*fct_t)(const PClass*);                      \
00074   PacketHandler<Cst, T>(T* obj, fct_t f)                        \
00075     : obj_(obj), f_(f) {}                                       \
00076   virtual void handle(const Packet* p)                          \
00077   {                                                             \
00078     LOG5("<- `" #Cst "' (id: %1)",                              \
00079          p->client_id);                                         \
00080     (obj_->*f_)(reinterpret_cast<const PClass*>(p));            \
00081   }                                                             \
00082   virtual int getCstValue() const { return Cst; }               \
00083   virtual void dispatchUIEvent(const EventProcess* evp,         \
00084                                const Packet& pkt)               \
00085   {                                                             \
00086     evp->dispatch(reinterpret_cast<const PClass&>(pkt));        \
00087   }                                                             \
00088 private:                                                        \
00089   T* obj_;                                                      \
00090   fct_t f_;                                                     \
00091 };                                                              \
00092 struct PClass : public Packet                                   \
00093 {                                                               \
00094   PClass(int client_id = -1)                                    \
00095    : Packet(Cst, client_id) { data_size = sizeof(*this); }
00096 
00097 // Ugly. To have balanced braces.
00098 # define END_PACKET };
00099 
00101 # define DECLARE_EMPTY_PACKET(Cst, PClass)      \
00102   DECLARE_PACKET(Cst, PClass)                   \
00103   END_PACKET 
00104 
00105 # define HANDLE_WITH(Token, Class, Object, Method, When)                \
00106   handleWith(new PacketHandler<Token, Class>(Object, &Class::Method), When)
00107 
00108 
00109 #endif /* !PACKETHANDLER_HH_ */
Generated on Mon Apr 5 21:17:12 2010 for Stechec/TBT by  doxygen 1.6.3