00001 #ifndef QPID_AMQP_0_10_ARRAY_H
00002 #define QPID_AMQP_0_10_ARRAY_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 "qpid/amqp_0_10/TypeForCode.h"
00026 #include "qpid/amqp_0_10/CodeForType.h"
00027 #include "qpid/amqp_0_10/UnknownType.h"
00028 #include "qpid/amqp_0_10/exceptions.h"
00029 #include "qpid/amqp_0_10/Codec.h"
00030 #include <vector>
00031 #include <ostream>
00032
00033 namespace qpid {
00034 namespace amqp_0_10 {
00035
00036 template <class T> class ArrayDomain : public std::vector<T> {
00037 public:
00038 template <class S> void serialize(S& s) { s.split(*this); }
00039
00040 template <class S> void encode(S& s) const {
00041 s(contentSize())(CodeForType<T>::value)(uint32_t(this->size()));
00042 s(this->begin(), this->end());
00043 }
00044
00045 void encode(Codec::Size& s) const { s.raw(0, contentSize() + 4); }
00046
00047 template <class S> void decode(S& s) {
00048 uint32_t size; uint8_t type; uint32_t count;
00049 s(size);
00050 typename S::ScopedLimit l(s, size);
00051 s(type);
00052 if (type != CodeForType<T>::value)
00053 throw InvalidArgumentException(QPID_MSG("Array domain expected type " << CodeForType<T>::value << " but found " << type));
00054 s(count);
00055 this->resize(count);
00056 s(this->begin(), this->end());
00057 }
00058
00059 private:
00060 uint32_t contentSize() const {
00061 return Codec::size(this->begin(), this->end()) + sizeof(uint32_t) + sizeof(uint8_t) ;
00062 }
00063 };
00064
00065 template <class T>
00066 std::ostream& operator<<(std::ostream& o, const ArrayDomain<T>& ad) {
00067 std::ostream_iterator<T> i(o, " ");
00068 o << "Array<" << typeName(CodeForType<T>::value) << ">[";
00069 std::copy(ad.begin(), ad.end(), i);
00070 o << "]";
00071 return o;
00072 }
00073
00077 template<> class ArrayDomain<UnknownType> : public std::vector<UnknownType> {
00078 public:
00079 ArrayDomain(uint8_t type_=0) : type(type_) {}
00080
00081 template <class S> void serialize(S& s) { s.split(*this); }
00082
00083 template <class S> void encode(S& s) const {
00084 s(contentSize())(type)(uint32_t(this->size()));
00085 s(this->begin(), this->end());
00086 }
00087
00088 void encode(Codec::Size& s) const { s.raw(0, contentSize() + 4); }
00089
00090 template <class S> void decode(S& s) {
00091 uint32_t size; uint32_t count;
00092 s(size);
00093 typename S::ScopedLimit l(s, size);
00094 s(type)(count);
00095 this->clear();
00096 this->resize(count, UnknownType(type));
00097 s(this->begin(), this->end());
00098 }
00099
00100 uint8_t getType() const { return type; }
00101
00102 private:
00103 uint32_t contentSize() const {
00104 return Codec::size(this->begin(), this->end()) + sizeof(uint32_t) + sizeof(uint8_t) ;
00105 }
00106 uint8_t type;
00107 };
00108
00109 std::ostream& operator<<(std::ostream& o, const Array& a);
00110
00111
00112
00113
00114 namespace session { class CommandFragment; }
00115 namespace dtx { class Xid; }
00116
00117 template <> struct ArrayDomain<session::CommandFragment> : public Void {};
00118 template <> struct ArrayDomain<dtx::Xid> : public Void {};
00119 inline std::ostream& operator<<(std::ostream& o, const ArrayDomain<session::CommandFragment>&) { return o; }
00120 inline std::ostream& operator<<(std::ostream& o, const ArrayDomain<dtx::Xid>&) { return o; }
00121
00122 }}
00123
00124 #endif