00001 #ifndef QPID_PACKER_H
00002 #define QPID_PACKER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <boost/optional.hpp>
00026 #include <boost/none.hpp>
00027 #include "qpid/amqp_0_10/built_in_types.h"
00028
00029 namespace qpid {
00030 namespace amqp_0_10 {
00031
00033 template <class T> struct SerializableOptional {
00034 boost::optional<T>& optional;
00035 SerializableOptional(boost::optional<T>& x) : optional(x) {}
00036 template <class S> void serialize(S& s) {
00037 if (optional)
00038 s(*optional);
00039 }
00040 };
00041
00042 }}
00043
00044
00045 namespace boost {
00046
00047 template <class T>
00048 qpid::amqp_0_10::SerializableOptional<T> serializable(boost::optional<T>& x) {
00049 return qpid::amqp_0_10::SerializableOptional<T>(x);
00050 }
00051
00052 }
00053
00054 namespace qpid {
00055 namespace amqp_0_10 {
00056
00060 class PackBits {
00061 public:
00062 PackBits() : bit(1), bits(0) {}
00063
00064 void setBit(bool b) { if (b) bits |= bit; bit <<= 1; }
00065 uint32_t getBits() { return bits; }
00066
00068 template <class T>
00069 PackBits& operator()(const T&) { setBit(1); return *this; }
00070
00072 template <class T> PackBits& operator()(const boost::optional<T>& opt) {
00073 setBit(opt); return *this;
00074 }
00075
00077 PackBits& operator()(Bit b) { setBit(b); return *this; }
00078
00079 private:
00080 uint32_t bit;
00081 uint32_t bits;
00082 };
00083
00085 template<class T> uint32_t packBits(const T& t) {
00086 PackBits pack;
00087 const_cast<T&>(t).serialize(pack);
00088 return pack.getBits();
00089 }
00090
00092 template <class Decoder, class Bits>
00093 class PackedDecoder {
00094 public:
00095 PackedDecoder(Decoder& d, Bits b) : decode(d), bits(b) {}
00096
00097 template <class T> PackedDecoder& operator()(T& t) {
00098 if (bits & 1)
00099 decode(t);
00100 else
00101 t = T();
00102
00103
00104
00105 bits >>= 1;
00106 return *this;
00107 }
00108
00109 template <class T> PackedDecoder& operator()(boost::optional<T>& opt) {
00110 if (bits & 1) {
00111 opt = T();
00112 decode(*opt);
00113 }
00114 else
00115 opt = boost::none;
00116 bits >>= 1;
00117 return *this;
00118 }
00119
00120 private:
00121 Decoder& decode;
00122 Bits bits;
00123 };
00124
00126 template <int Bytes> struct UintOfSize;
00127 template <> struct UintOfSize<1> { typedef uint8_t type; };
00128 template <> struct UintOfSize<2> { typedef uint16_t type; };
00129 template <> struct UintOfSize<4> { typedef uint32_t type; };
00130
00134 template <class T> class Packer
00135 {
00136 public:
00137 typedef typename UintOfSize<T::PACK>::type Bits;
00138
00139 Packer(T& t) : data(t) {}
00140
00141 template <class S> void serialize(S& s) { s.split(*this); }
00142
00143 template <class S> void encode(S& s) const {
00144 Bits bits = packBits(data);
00145 s.littleEnd(bits);
00146 data.serialize(s);
00147 }
00148
00149 template <class S> void decode(S& s) {
00150 Bits bits;
00151 s.littleEnd(bits);
00152 PackedDecoder<S, Bits> decode(s, bits);
00153 data.serialize(decode);
00154 }
00155
00156
00157 protected:
00158 T& data;
00159 };
00160
00161 template <class T, uint8_t=T::SIZE> struct SizedPacker : public Packer<T> {
00162 typedef typename UintOfSize<T::SIZE>::type Size;
00163
00164 SizedPacker(T& t) : Packer<T>(t) {}
00165
00166 template <class S> void serialize(S& s) {
00167 s.split(*this);
00168 }
00169
00170 template <class S> void encode(S& s) const {
00171 Codec::Size sizer;
00172 this->data.serialize(sizer);
00173 Size size=size_t(sizer)+T::PACK;
00174 s(size);
00175 Packer<T>::encode(s);
00176 }
00177
00178 template <class S> void decode(S& s) {
00179 Size size;
00180 s(size);
00181 typename S::ScopedLimit l(s, size);
00182 Packer<T>::decode(s);
00183 }
00184
00185 };
00186
00187 template <class T> struct SizedPacker<T,0> : public Packer<T> {
00188 SizedPacker(T& t) : Packer<T>(t) {}
00189 };
00190
00191 }}
00192
00193
00194
00195 #endif