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 putFloat(float f);
00083 void putDouble(double f);
00084 void putBin128(uint8_t* b);
00085
00086 uint8_t getOctet();
00087 uint16_t getShort();
00088 uint32_t getLong();
00089 uint64_t getLongLong();
00090 float getFloat();
00091 double getDouble();
00092
00093 template <int n>
00094 uint64_t getUInt();
00095
00096 template <int n>
00097 void putUInt(uint64_t);
00098
00099 void putShortString(const string& s);
00100 void putMediumString(const string& s);
00101 void putLongString(const string& s);
00102 void getShortString(string& s);
00103 void getMediumString(string& s);
00104 void getLongString(string& s);
00105 void getBin128(uint8_t* b);
00106
00107 void putRawData(const string& s);
00108 void getRawData(string& s, uint32_t size);
00109
00110 void putRawData(const uint8_t* data, size_t size);
00111 void getRawData(uint8_t* data, size_t size);
00112
00113 template <class T> void put(const T& data) { data.encode(*this); }
00114 template <class T> void get(T& data) { data.decode(*this); }
00115
00116 void dump(std::ostream&) const;
00117 };
00118
00119 std::ostream& operator<<(std::ostream&, const Buffer&);
00120
00121 }}
00122
00123
00124 #endif