MessageHelper.h

00001 /* START_LICENSE_HEADER
00002 
00003 Copyright (C) 2000 Martin Piper, original design and program code
00004 Copyright (C) 2001 Replica Software
00005 
00006 This program file is copyright (C) Replica Software and can only be used under license.
00007 For more information visit: http://www.replicanet.com/
00008 Or email: info@replicanet.com
00009 
00010 END_LICENSE_HEADER */
00011 #ifndef __MESSAGEHELPER_H__
00012 #define __MESSAGEHELPER_H__
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 #include <string>
00015 
00016 namespace RNReplicaNet
00017 {
00018 
00019 #define MESSAGEHELPER_ADDVARIABLE(x)    \
00020 AddVariable(&(x),sizeof(x));
00021 
00022 #define MESSAGEHELPER_GETVARIABLE(x)    \
00023 GetVariable(&(x),sizeof(x));
00024 
00025 #define MESSAGEHELPER_ADDVARIABLEp(y,x) \
00026 y.AddVariable(&(x),sizeof(x));
00027 
00028 #define MESSAGEHELPER_GETVARIABLEp(y,x) \
00029 y.GetVariable(&(x),sizeof(x));
00030 
00035 class REPNETEXPORTAPI MessageHelper
00036 {
00037 public:
00038     class Exception
00039     {
00040     public:
00041         Exception() {}
00042         virtual ~Exception() {}
00043     };
00047     MessageHelper();
00048 
00052     virtual ~MessageHelper();
00053 
00058     void SetBuffer(void *const buffer);
00059 
00064     void *GetBuffer(void) const;
00065 
00070     void *GetCurrentPosition(void) const;
00071 
00077     virtual void AddVariable(const void *variable,const int size);
00078 
00084     virtual void AddData(const void *data,const int size);
00085 
00091     void GetVariable(void *const variable,const int size);
00092 
00098     void GetData(void *const data,const int size);
00099 
00104     int GetSize(void) const;
00105 
00110     void SetSize(const int size);
00111 
00116     void AddInteger(const int value);
00117 
00122     void AddFloat(const float value);
00123 
00128     int GetInteger(void);
00129 
00135     float GetFloat(void);
00136 
00140     static std::string DumpAsHex(const void *data,const int size,const bool csv = true);
00141 
00142     std::string DumpAsHex(const bool csv = true)
00143     {
00144         return DumpAsHex(GetBuffer(),GetSize(),csv);
00145     }
00146 
00151     void SetGuardSize(const int size = -1);
00152 
00153 private:
00154     unsigned char *mFirstPtr;           
00155     unsigned char *mBufferPtr;          
00156     int mGuardSize;
00157 };
00158 
00162 class REPNETEXPORTAPI DynamicMessageHelper : public MessageHelper
00163 {
00164 public:
00168     DynamicMessageHelper();
00169 
00173     virtual ~DynamicMessageHelper();
00174 
00175 private:
00179     void SetBuffer(void *const) {}
00180 public:
00181 
00187     void SetBufferSize(const int size = 1024);
00188 
00193     int GetBufferSize(void);
00194 
00199     void SetSize(const int size);
00200 
00201 
00202     void AddVariable(const void *variable,const int size);
00203 
00204     void AddData(const void *data,const int size);
00205 
00209     void FreeBuffer(void);
00210 
00214     void EnsureBufferAllocated(void);
00215 
00219     bool Write(const char *filename);
00220 
00224     bool Read(const char *filename);
00225 
00226     // Define operator<< and operator>> to make using this class a little easier.
00227     // For all operator>> the rhs is replaced with the data in lhs.
00228     // Generic cases
00229     template <typename T>
00230     void operator<<(const T& rhs)
00231     {
00232         MESSAGEHELPER_ADDVARIABLE(rhs);
00233     }
00234     template <typename T>
00235     void operator>>(T& rhs)
00236     {
00237         MESSAGEHELPER_GETVARIABLE(rhs);
00238     }
00239 
00240     // Special cases
00241     void operator<<(const char *rhs);
00242     void operator<<(const std::string &rhs);
00243     void operator>>(std::string &rhs);
00244 
00245     void operator<<(const DynamicMessageHelper &rhs);
00246     void operator>>(DynamicMessageHelper &rhs);
00247 
00248     // Tests using GetSize() for the length of the data to test.
00249     bool operator==(const DynamicMessageHelper &rhs) const;
00250     bool operator!=(const DynamicMessageHelper &rhs) const;
00251 
00252 
00256     bool ReadAsHex(const std::string data);
00257 
00258 private:
00259     void CheckBufferSize(const int newSize);
00260     void *mBuffer;
00261     int mSize;
00262 };
00263 
00264 } // namespace RNReplicaNet
00265 
00266 #endif

Generated on Sat Jun 28 22:02:16 2008 for ReplicaNet and RNLobby by  doxygen 1.5.3