Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET | Apache Qpid Documentation |
00001 /* 00002 * 00003 * Licensed to the Apache Software Foundation (ASF) under one 00004 * or more contributor license agreements. See the NOTICE file 00005 * distributed with this work for additional information 00006 * regarding copyright ownership. The ASF licenses this file 00007 * to you under the Apache License, Version 2.0 (the 00008 * "License"); you may not use this file except in compliance 00009 * with the License. You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, 00014 * software distributed under the License is distributed on an 00015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 00016 * KIND, either express or implied. See the License for the 00017 * specific language governing permissions and limitations 00018 * under the License. 00019 * 00020 */ 00021 #include "qpid/framing/amqp_types.h" 00022 #include "qpid/Exception.h" 00023 #include "qpid/CommonImportExport.h" 00024 #include <boost/iterator/iterator_facade.hpp> 00025 00026 #ifndef _Buffer_ 00027 #define _Buffer_ 00028 00029 namespace qpid { 00030 namespace framing { 00031 00032 struct OutOfBounds : qpid::Exception { 00033 OutOfBounds() : qpid::Exception(std::string("Out of Bounds")) {} 00034 }; 00035 00036 class Content; 00037 class FieldTable; 00038 00039 class Buffer 00040 { 00041 uint32_t size; 00042 char* data; 00043 uint32_t position; 00044 uint32_t r_position; 00045 00046 public: 00047 void checkAvailable(uint32_t count) { if (position + count > size) throw OutOfBounds(); } 00048 00052 class Iterator : public boost::iterator_facade< 00053 Iterator, char, boost::random_access_traversal_tag> 00054 { 00055 public: 00056 Iterator(Buffer& b) : buffer(&b) {} 00057 00058 private: 00059 friend class boost::iterator_core_access; 00060 char& dereference() const { return buffer->data[buffer->position]; } 00061 void increment() { ++buffer->position; } 00062 bool equal(const Iterator& x) const { return buffer == x.buffer; } 00063 00064 Buffer* buffer; 00065 }; 00066 00067 friend class Iterator; 00068 00069 QPID_COMMON_EXTERN Buffer(char* data=0, uint32_t size=0); 00070 00071 QPID_COMMON_EXTERN void record(); 00072 QPID_COMMON_EXTERN void restore(bool reRecord = false); 00073 QPID_COMMON_EXTERN void reset(); 00074 00075 QPID_COMMON_EXTERN uint32_t available() { return size - position; } 00076 QPID_COMMON_EXTERN uint32_t getSize() { return size; } 00077 QPID_COMMON_EXTERN uint32_t getPosition() { return position; } 00078 QPID_COMMON_EXTERN void setPosition(uint32_t p) { position = p; } 00079 QPID_COMMON_EXTERN Iterator getIterator() { return Iterator(*this); } 00080 QPID_COMMON_EXTERN char* getPointer() { return data; } 00081 00082 QPID_COMMON_EXTERN void putOctet(uint8_t i); 00083 QPID_COMMON_EXTERN void putShort(uint16_t i); 00084 QPID_COMMON_EXTERN void putLong(uint32_t i); 00085 QPID_COMMON_EXTERN void putLongLong(uint64_t i); 00086 QPID_COMMON_EXTERN void putInt8(int8_t i); 00087 QPID_COMMON_EXTERN void putInt16(int16_t i); 00088 QPID_COMMON_EXTERN void putInt32(int32_t i); 00089 QPID_COMMON_EXTERN void putInt64(int64_t i); 00090 QPID_COMMON_EXTERN void putFloat(float f); 00091 QPID_COMMON_EXTERN void putDouble(double f); 00092 QPID_COMMON_EXTERN void putBin128(const uint8_t* b); 00093 00094 QPID_COMMON_EXTERN uint8_t getOctet(); 00095 QPID_COMMON_EXTERN uint16_t getShort(); 00096 QPID_COMMON_EXTERN uint32_t getLong(); 00097 QPID_COMMON_EXTERN uint64_t getLongLong(); 00098 QPID_COMMON_EXTERN int8_t getInt8(); 00099 QPID_COMMON_EXTERN int16_t getInt16(); 00100 QPID_COMMON_EXTERN int32_t getInt32(); 00101 QPID_COMMON_EXTERN int64_t getInt64(); 00102 QPID_COMMON_EXTERN float getFloat(); 00103 QPID_COMMON_EXTERN double getDouble(); 00104 00105 template <int n> 00106 QPID_COMMON_EXTERN uint64_t getUInt(); 00107 00108 template <int n> 00109 QPID_COMMON_EXTERN void putUInt(uint64_t); 00110 00111 QPID_COMMON_EXTERN void putShortString(const string& s); 00112 QPID_COMMON_EXTERN void putMediumString(const string& s); 00113 QPID_COMMON_EXTERN void putLongString(const string& s); 00114 QPID_COMMON_EXTERN void getShortString(string& s); 00115 QPID_COMMON_EXTERN void getMediumString(string& s); 00116 QPID_COMMON_EXTERN void getLongString(string& s); 00117 QPID_COMMON_EXTERN void getBin128(uint8_t* b); 00118 00119 QPID_COMMON_EXTERN void putRawData(const string& s); 00120 QPID_COMMON_EXTERN void getRawData(string& s, uint32_t size); 00121 00122 QPID_COMMON_EXTERN void putRawData(const uint8_t* data, size_t size); 00123 QPID_COMMON_EXTERN void getRawData(uint8_t* data, size_t size); 00124 00125 template <class T> void put(const T& data) { data.encode(*this); } 00126 template <class T> void get(T& data) { data.decode(*this); } 00127 00128 QPID_COMMON_EXTERN void dump(std::ostream&) const; 00129 }; 00130 00131 std::ostream& operator<<(std::ostream&, const Buffer&); 00132 00133 }} // namespace qpid::framing 00134 00135 00136 #endif