PacketHandler.hh

Go to the documentation of this file.
00001 /*
00002 ** TowBowlTactics, an adaptation of the tabletop game Blood Bowl.
00003 ** 
00004 ** Copyright (C) 2006, 2007 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 "EventProcess.hh"
00024 
00030 class BasePacketHandler
00031 {
00032 public:
00033   virtual ~BasePacketHandler() {}
00034   virtual void handle(const Packet* p) = 0;
00035   virtual int getCstValue() const = 0;
00036   virtual void dispatchUIEvent(const EventProcess* evp,
00037                                const Packet& pkt) = 0;
00038 };
00039 
00042 template <int N, typename T>
00043 class PacketHandler : public BasePacketHandler
00044 {
00045 };
00046 
00066 # define DECLARE_PACKET(Cst, PClass)                            \
00067 struct PClass;                                                  \
00068 template <typename T>                                           \
00069 class PacketHandler<Cst, T> : public BasePacketHandler          \
00070 {                                                               \
00071 public:                                                         \
00072   typedef void (T::*fct_t)(const PClass*);                      \
00073   PacketHandler<Cst, T>(T* obj, fct_t f)                        \
00074     : obj_(obj), f_(f) {}                                       \
00075   virtual void handle(const Packet* p)                          \
00076   {                                                             \
00077     LOG5("<- `" #Cst "' (id: %1)",                              \
00078          p->client_id);                                         \
00079     (obj_->*f_)(reinterpret_cast<const PClass*>(p));            \
00080   }                                                             \
00081   virtual int getCstValue() const { return Cst; }               \
00082   virtual void dispatchUIEvent(const EventProcess* evp,         \
00083                                const Packet& pkt)               \
00084   {                                                             \
00085     evp->dispatch(reinterpret_cast<const PClass&>(pkt));        \
00086   }                                                             \
00087 private:                                                        \
00088   T* obj_;                                                      \
00089   fct_t f_;                                                     \
00090 };                                                              \
00091 struct PClass : public Packet                                   \
00092 {                                                               \
00093   PClass(int client_id = -1)                                    \
00094    : Packet(Cst, client_id) { data_size = sizeof(*this); }
00095 
00096 // Ugly. To have balanced braces.
00097 # define END_PACKET };
00098 
00100 # define DECLARE_EMPTY_PACKET(Cst, PClass)      \
00101   DECLARE_PACKET(Cst, PClass)                   \
00102   END_PACKET 
00103 
00104 # define HANDLE_WITH(Token, Class, Object, Method, When)                \
00105   handleWith(new PacketHandler<Token, Class>(Object, &Class::Method), When)
00106 
00107 
00108 #endif /* !PACKETHANDLER_HH_ */

Generated on Sat Jun 23 16:07:23 2007 for Stechec/TBT by  doxygen 1.4.7