$search
00001 #ifndef CASTOR_ENCODING_H 00002 #define CASTOR_ENCODING_H 1 00003 00004 #include <endian.h> 00005 00006 #include <vector> 00007 00008 #include "NetAddress.h" 00009 #include "Types.h" 00010 00011 namespace castor { 00012 00013 // typedef std::vector<char> Bytes; 00014 // typedef boost::shared_ptr<Bytes> BytesPtr; 00015 typedef enum { endian_undefined = -1, endian_little = 0, endian_big = 1 } Endianness; 00016 00017 class Encoder; 00018 typedef boost::shared_ptr<Encoder> EncoderPtr; 00019 00020 class Decoder; 00021 typedef boost::shared_ptr<Decoder> DecoderPtr; 00022 00023 template <class T> 00024 class Encoding { 00025 00026 protected: 00027 00028 Encoding() { 00029 } 00030 00031 public: 00032 00033 static EncoderPtr createEncoder() { 00034 return EncoderPtr(new T); 00035 } 00036 00037 static DecoderPtr createDecoder(const BytesPtr data, size_t offset = 0) { 00038 return DecoderPtr(new T(data, offset)); 00039 } 00040 00041 static DecoderPtr createDecoder(const char *data, size_t length) { 00042 return DecoderPtr(new T(data, length)); 00043 } 00044 00045 static DecoderPtr createDecoder(const char *data, size_t offset, size_t length) { 00046 return DecoderPtr(new T(data, offset, length)); 00047 } 00048 00049 }; 00050 00051 Endianness endianness(); 00052 00053 class Encoder { 00054 00055 protected: 00056 00057 EncoderPtr next; 00058 00059 public: 00060 00061 Encoder() : next() { 00062 } 00063 00064 virtual ~Encoder() { 00065 } 00066 00067 virtual EncoderPtr createEncoder() = 0; 00068 00069 virtual DecoderPtr createDecoder(const BytesPtr x, size_t offset = 0) = 0; 00070 virtual DecoderPtr createDecoder(const char *x, size_t length) = 0; 00071 virtual DecoderPtr createDecoder(const char *x, size_t offset, size_t length) = 0; 00072 00073 void setNext(EncoderPtr value) { 00074 this->next = value; 00075 } 00076 00077 EncoderPtr getNext() { 00078 return this->next; 00079 } 00080 00081 virtual void switchEndianness(bool yes) = 0; 00082 00083 virtual const char *getBuffer() = 0; 00084 00085 virtual size_t getLength() = 0; 00086 virtual size_t getPosition() = 0; 00087 00088 virtual BytesPtr getBytes() = 0; 00089 00090 virtual void mark() = 0; 00091 virtual void commit() = 0; 00092 virtual void rollback() = 0; 00093 virtual void reset() = 0; 00094 00095 virtual void seek(long offset) = 0; 00096 virtual void encLength(size_t x) = 0; 00097 virtual void encBool(bool x) = 0; 00098 virtual void encInt8(char x) = 0; 00099 virtual void encUInt8(unsigned char x) = 0; 00100 virtual void encInt16(short x) = 0; 00101 virtual void encUInt16(unsigned short x) = 0; 00102 virtual void encInt32(long x) = 0; 00103 virtual void encUInt32(unsigned long x) = 0; 00104 virtual void encInt64(long long x) = 0; 00105 virtual void encUInt64(unsigned long long x) = 0; 00106 virtual void encFloat(float x) = 0; 00107 virtual void encDouble(double x) = 0; 00108 virtual void encString(const std::string x) = 0; 00109 virtual void encAddress(const castor::net::NetAddress &x) = 0; 00110 virtual void encBytes(const Bytes &x) = 0; 00111 virtual void encBytes(const Bytes &x, size_t offset, size_t length) = 0; 00112 virtual void encBytes(const char *x, size_t length) = 0; 00113 virtual void encBytes(const char *x, size_t offset, size_t length) = 0; 00114 }; 00115 00116 class Decoder { 00117 00118 public: 00119 00120 Decoder() { 00121 } 00122 00123 Decoder(const BytesPtr x, size_t offset = 0); 00124 Decoder(const char *x, size_t length); 00125 Decoder(const char *x, size_t offset, size_t length); 00126 00127 virtual ~Decoder() { 00128 } 00129 00130 virtual EncoderPtr createEncoder() = 0; 00131 00132 virtual DecoderPtr createDecoder(const BytesPtr x, size_t offset = 0) = 0; 00133 virtual DecoderPtr createDecoder(const char *x, size_t length) = 0; 00134 virtual DecoderPtr createDecoder(const char *x, size_t offset, size_t length) = 0; 00135 00136 virtual void switchEndianness(bool yes) = 0; 00137 00138 virtual const char *getBuffer() = 0; 00139 00140 virtual size_t getLength() = 0; 00141 virtual size_t getPosition() = 0; 00142 00143 virtual BytesPtr getBytes() = 0; 00144 00145 virtual void mark() = 0; 00146 virtual void commit() = 0; 00147 virtual void rollback() = 0; 00148 virtual void reset() = 0; 00149 00150 virtual void seek(long offset) = 0; 00151 00152 virtual size_t decLength() = 0; 00153 virtual bool decBool() = 0; 00154 virtual char decInt8() = 0; 00155 virtual unsigned char decUInt8() = 0; 00156 virtual short decInt16() = 0; 00157 virtual unsigned short decUInt16() = 0; 00158 virtual long decInt32() = 0; 00159 virtual unsigned long decUInt32() = 0; 00160 virtual long long decInt64() = 0; 00161 virtual unsigned long long decUInt64() = 0; 00162 virtual float decFloat() = 0; 00163 virtual double decDouble() = 0; 00164 virtual std::string decString() = 0; 00165 virtual castor::net::NetAddress decAddress() = 0; 00166 virtual BytesPtr decBytes(size_t length) = 0; 00167 00168 }; 00169 00170 00171 } 00172 00173 #endif /* CASTOR_ENCODING_H */ 00174