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