00001 #ifndef QPID_AMQP_0_10_MAP_H
00002 #define QPID_AMQP_0_10_MAP_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/Exception.h"
00026 #include "qpid/amqp_0_10/built_in_types.h"
00027 #include "qpid/amqp_0_10/UnknownType.h"
00028 #include "qpid/amqp_0_10/CodeForType.h"
00029 #include "qpid/amqp_0_10/TypeForCode.h"
00030 #include "qpid/amqp_0_10/Codec.h"
00031 #include "qpid/framing/Blob.h"
00032 #include <map>
00033 #include <string>
00034 #include <iosfwd>
00035
00036 namespace qpid {
00037 namespace amqp_0_10 {
00038
00039 class Map;
00040
00041 class MapValue {
00042 public:
00043 struct BadTypeException : public Exception {};
00044
00045 template <class R> struct Visitor { typedef R result_type; };
00046
00047 MapValue();
00048 MapValue(const MapValue& x);
00049 template <class T> explicit MapValue(const T& t);
00050 template <class T> MapValue& operator=(const T& t);
00051
00052 template <class T> T* get();
00053 template <class T> const T* get() const;
00054
00055 template <class V> typename V::result_type apply_visitor(V&);
00056 template <class V> typename V::result_type apply_visitor(const V&);
00057
00058 uint8_t getCode() const { return code; }
00059
00060 bool operator==(const MapValue&) const;
00061
00062 template <class S> void serialize(S& s) { s(code); s.split(*this); }
00063 template <class S> void encode(S& s) const {
00064 const_cast<MapValue*>(this)->apply_visitor(s);
00065 }
00066 template <class S> void decode(S& s) {
00067 DecodeVisitor<S> dv(blob, s);
00068 qpid::amqp_0_10::apply_visitor(dv, code);
00069 }
00070
00071
00072 private:
00073
00074
00075
00076
00077 static const size_t SIZE=256;
00078 typedef framing::Blob<SIZE> Blob;
00079
00080 template <class V> struct VisitVisitor;
00081 template <class T> struct GetVisitor;
00082 template <class D> struct DecodeVisitor;
00083
00084 uint8_t code;
00085 Blob blob;
00086 };
00087
00088 class Map : public std::map<Str8, MapValue> {
00089 public:
00090 template <class S> void serialize(S& s) { s.split(*this); }
00091 template <class S> void encode(S& s) const;
00092
00093 void encode(Codec::Size& s) const { s.raw(0, contentSize() + 4); }
00094
00095 template <class S> void decode(S& s);
00096
00097 private:
00098 uint32_t contentSize() const;
00099 };
00100
00101 std::ostream& operator<<(std::ostream&, const MapValue&);
00102 std::ostream& operator<<(std::ostream&, const Map::value_type&);
00103 std::ostream& operator<<(std::ostream&, const Map&);
00104
00105 using framing::in_place;
00106
00107 template <class T> MapValue::MapValue(const T& t) : code(codeFor(t)), blob(in_place<t>()) {}
00108
00109 template <class T> MapValue& MapValue::operator=(const T& t) {
00110 code=codeFor(t);
00111 blob=t;
00112 return *this;
00113 }
00114
00115 template <class V> struct MapValue::VisitVisitor {
00116 typedef typename V::result_type result_type;
00117 V& visitor;
00118 Blob& blob;
00119 VisitVisitor(V& v, Blob& b) : visitor(v), blob(b) {}
00120
00121 template <class T> result_type operator()(T*) {
00122 return visitor(*reinterpret_cast<T*>(blob.get()));
00123 }
00124 };
00125
00126 template <class V> typename V::result_type MapValue::apply_visitor(V& v) {
00127 VisitVisitor<V> visitor(v, blob);
00128 return qpid::amqp_0_10::apply_visitor(visitor, code);
00129 }
00130
00131 template <class R> struct MapValue::GetVisitor {
00132 typedef R* result_type;
00133 const MapValue::Blob& blob;
00134
00135 GetVisitor(const MapValue::Blob& b) : blob(b) {}
00136
00137 R* operator()(R& r) { return &r; }
00138 template <class T> R* operator()(T&) { return 0; }
00139 };
00140
00141 template <class D> struct MapValue::DecodeVisitor {
00142 typedef void result_type;
00143 MapValue::Blob& blob;
00144 D& decoder;
00145 DecodeVisitor(Blob& b, D& d) : blob(b), decoder(d) {}
00146
00147 template <class T> void operator()(T*) {
00148 T t;
00149 decoder(t);
00150 blob = t;
00151 }
00152 };
00153
00154 template <class T> T* MapValue::get() { return apply_visitor(GetVisitor<T>(blob)); }
00155 template <class T> const T* MapValue::get() const { return apply_visitor(GetVisitor<const T>()); }
00156
00157 template <class V> typename V::result_type MapValue::apply_visitor(const V& v) {
00158 return apply_visitor(const_cast<V&>(v));
00159 }
00160
00161 template <class S> void Map::encode(S& s) const {
00162
00163
00164 s(contentSize());
00165 for (const_iterator i = begin(); i != end(); ++i)
00166 s(i->first)(i->second);
00167 }
00168
00169 template <class S> void Map::decode(S& s) {
00170 uint32_t decodedSize ;
00171
00172
00173
00174 s(decodedSize);
00175 typename S::ScopedLimit l(s, decodedSize);
00176
00177
00178 while (s.bytesRemaining() > 0) {
00179 key_type k; MapValue v;
00180 s(k)(v);
00181 insert(value_type(k,v));
00182 }
00183 }
00184
00185
00186 }}
00187
00188 #endif