00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "amqp_types.h"
00022 #include "qpid/Exception.h"
00023 #include <boost/iterator/iterator_facade.hpp>
00024
00025 #ifndef _Buffer_
00026 #define _Buffer_
00027
00028 namespace qpid {
00029 namespace framing {
00030
00031 struct OutOfBounds : qpid::Exception {};
00032
00033 class Content;
00034 class FieldTable;
00035
00036 class Buffer
00037 {
00038 uint32_t size;
00039 char* data;
00040 uint32_t position;
00041 uint32_t r_position;
00042
00043 void checkAvailable(uint32_t count) { if (position + count > size) throw OutOfBounds(); }
00044
00045 public:
00046
00050 class Iterator : public boost::iterator_facade<
00051 Iterator, char, boost::random_access_traversal_tag>
00052 {
00053 public:
00054 Iterator(Buffer& b) : buffer(&b) {}
00055
00056 private:
00057 friend class boost::iterator_core_access;
00058 char& dereference() const { return buffer->data[buffer->position]; }
00059 void increment() { ++buffer->position; }
00060 bool equal(const Iterator& x) const { return buffer == x.buffer; }
00061
00062 Buffer* buffer;
00063 };
00064
00065 friend class Iterator;
00066
00067 Buffer(char* data=0, uint32_t size=0);
00068
00069 void record();
00070 void restore(bool reRecord = false);
00071 void reset();
00072
00073 uint32_t available() { return size - position; }
00074 uint32_t getSize() { return size; }
00075 uint32_t getPosition() { return position; }
00076 Iterator getIterator() { return Iterator(*this); }
00077
00078 void putOctet(uint8_t i);
00079 void putShort(uint16_t i);
00080 void putLong(uint32_t i);
00081 void putLongLong(uint64_t i);
00082 void putInt8(int8_t i);
00083 void putInt16(int16_t i);
00084 void putInt32(int32_t i);
00085 void putInt64(int64_t i);
00086 void putFloat(float f);
00087 void putDouble(double f);
00088 void putBin128(uint8_t* b);
00089
00090 uint8_t getOctet();
00091 uint16_t getShort();
00092 uint32_t getLong();
00093 uint64_t getLongLong();
00094 int8_t getInt8();
00095 int16_t getInt16();
00096 int32_t getInt32();
00097 int64_t getInt64();
00098 float getFloat();
00099 double getDouble();
00100
00101 template <int n>
00102 uint64_t getUInt();
00103
00104 template <int n>
00105 void putUInt(uint64_t);
00106
00107 void putShortString(const string& s);
00108 void putMediumString(const string& s);
00109 void putLongString(const string& s);
00110 void getShortString(string& s);
00111 void getMediumString(string& s);
00112 void getLongString(string& s);
00113 void getBin128(uint8_t* b);
00114
00115 void putRawData(const string& s);
00116 void getRawData(string& s, uint32_t size);
00117
00118 void putRawData(const uint8_t* data, size_t size);
00119 void getRawData(uint8_t* data, size_t size);
00120
00121 template <class T> void put(const T& data) { data.encode(*this); }
00122 template <class T> void get(T& data) { data.decode(*this); }
00123
00124 void dump(std::ostream&) const;
00125 };
00126
00127 std::ostream& operator<<(std::ostream&, const Buffer&);
00128
00129 }}
00130
00131
00132 #endif