diff options
| author | Alan Conway <aconway@apache.org> | 2008-11-04 19:52:49 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2008-11-04 19:52:49 +0000 |
| commit | eda249ff22edb3726243da81ff48c82e4d88e872 (patch) | |
| tree | 0939d790e6a1b0d86993c9c3804c1adaa369aeb8 /cpp/src/qpid/framing | |
| parent | 5d2471636928eff8b8031237c54348db0d5c388d (diff) | |
| download | qpid-python-eda249ff22edb3726243da81ff48c82e4d88e872.tar.gz | |
constants.rb: generate type code constants for AMQP types. Useful with Array.
framing/Array:
- added some std:::vector like functions & typedefs.
- use TypeCode enums, human readable ostream << operator.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@711365 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/framing')
| -rw-r--r-- | cpp/src/qpid/framing/Array.cpp | 42 | ||||
| -rw-r--r-- | cpp/src/qpid/framing/Array.h | 36 | ||||
| -rw-r--r-- | cpp/src/qpid/framing/FieldValue.cpp | 15 | ||||
| -rw-r--r-- | cpp/src/qpid/framing/FieldValue.h | 10 |
4 files changed, 65 insertions, 38 deletions
diff --git a/cpp/src/qpid/framing/Array.cpp b/cpp/src/qpid/framing/Array.cpp index 42d05f71c9..9f072f7b05 100644 --- a/cpp/src/qpid/framing/Array.cpp +++ b/cpp/src/qpid/framing/Array.cpp @@ -28,20 +28,21 @@ namespace qpid { namespace framing { -Array::Array() : typeOctet(0xF0/*void*/) {} +Array::Array() : type(TYPE_CODE_VOID) {} -Array::Array(uint8_t type) : typeOctet(type) {} +Array::Array(TypeCode t) : type(t) {} + +Array::Array(uint8_t t) : type(typeCode(t)) {} Array::Array(const std::vector<std::string>& in) { - typeOctet = 0xA4; + type = TYPE_CODE_STR16; for (std::vector<std::string>::const_iterator i = in.begin(); i != in.end(); ++i) { - ValuePtr value(new StringValue(*i)); + ValuePtr value(new Str16Value(*i)); values.push_back(value); } } - uint32_t Array::encodedSize() const { //note: size is only included when used as a 'top level' type uint32_t len(4/*size*/ + 1/*type*/ + 4/*count*/); @@ -55,18 +56,18 @@ int Array::count() const { return values.size(); } -std::ostream& operator<<(std::ostream& out, const Array& t) { - out << "{"; - for(Array::ValueVector::const_iterator i = t.values.begin(); i != t.values.end(); ++i) { - if (i != t.values.begin()) out << ", "; - out << *(i->get()); +std::ostream& operator<<(std::ostream& out, const Array& a) { + out << typeName(a.getType()) << "{"; + for(Array::ValueVector::const_iterator i = a.values.begin(); i != a.values.end(); ++i) { + if (i != a.values.begin()) out << ", "; + (*i)->print(out); } return out << "}"; } void Array::encode(Buffer& buffer) const{ buffer.putLong(encodedSize() - 4);//size added only when array is a top-level type - buffer.putOctet(typeOctet); + buffer.putOctet(type); buffer.putLong(count()); for (ValueVector::const_iterator i = values.begin(); i!=values.end(); ++i) { (*i)->getData().encode(buffer); @@ -81,11 +82,11 @@ void Array::decode(Buffer& buffer){ << size << " bytes but only " << available << " available")); } if (size) { - typeOctet = buffer.getOctet(); + type = TypeCode(buffer.getOctet()); uint32_t count = buffer.getLong(); FieldValue dummy; - dummy.setType(typeOctet); + dummy.setType(type); available = buffer.available(); if (available < count * dummy.getData().encodedSize()) { throw IllegalArgumentException(QPID_MSG("Not enough data for array, expected " @@ -95,7 +96,7 @@ void Array::decode(Buffer& buffer){ for (uint32_t i = 0; i < count; i++) { ValuePtr value(new FieldValue); - value->setType(typeOctet); + value->setType(type); value->getData().decode(buffer); values.push_back(ValuePtr(value)); } @@ -104,7 +105,7 @@ void Array::decode(Buffer& buffer){ bool Array::operator==(const Array& x) const { - if (typeOctet != x.typeOctet) return false; + if (type != x.type) return false; if (values.size() != x.values.size()) return false; for (ValueVector::const_iterator i = values.begin(), j = x.values.begin(); i != values.end(); ++i, ++j) { @@ -114,12 +115,13 @@ bool Array::operator==(const Array& x) const { return true; } -void Array::add(ValuePtr value) -{ - if (typeOctet != value->getType()) { - throw IllegalArgumentException(QPID_MSG("Wrong type of value in Array, expected " << typeOctet << " but found " << value->getType())); +void Array::insert(iterator i, ValuePtr value) { + if (type != value->getType()) { + // FIXME aconway 2008-10-31: put meaningful strings in this message. + throw Exception(QPID_MSG("Wrong type of value in Array, expected " << type + << " but found " << TypeCode(value->getType()))); } - values.push_back(value); + values.insert(i, value); } diff --git a/cpp/src/qpid/framing/Array.h b/cpp/src/qpid/framing/Array.h index d3ca04dd1d..183fcb6d5c 100644 --- a/cpp/src/qpid/framing/Array.h +++ b/cpp/src/qpid/framing/Array.h @@ -18,12 +18,12 @@ * under the License. * */ -#include <iostream> -#include <vector> -#include <boost/shared_ptr.hpp> -#include <map> #include "amqp_types.h" #include "FieldValue.h" +#include "qpid/framing/TypeCode.h" +#include <boost/shared_ptr.hpp> +#include <iostream> +#include <vector> #ifndef _Array_ #define _Array_ @@ -38,6 +38,8 @@ class Array public: typedef boost::shared_ptr<FieldValue> ValuePtr; typedef std::vector<ValuePtr> ValueVector; + typedef ValueVector::const_iterator const_iterator; + typedef ValueVector::iterator iterator; uint32_t encodedSize() const; void encode(Buffer& buffer) const; @@ -47,11 +49,30 @@ class Array bool operator==(const Array& other) const; Array(); + Array(TypeCode type); Array(uint8_t type); //creates a longstr array Array(const std::vector<std::string>& in); - void add(ValuePtr value); + TypeCode getType() const { return type; } + + // std collection interface. + const_iterator begin() const { return values.begin(); } + const_iterator end() const { return values.end(); } + iterator begin() { return values.begin(); } + iterator end(){ return values.end(); } + + ValuePtr front() const { return values.front(); } + ValuePtr back() const { return values.back(); } + size_t size() const { return values.size(); } + + void insert(iterator i, ValuePtr value); + void erase(iterator i) { values.erase(i); } + void push_back(ValuePtr value) { values.insert(end(), value); } + void pop_back() { values.pop_back(); } + + // Non-std interface + void add(ValuePtr value) { push_back(value); } template <class T> void collect(std::vector<T>& out) const @@ -60,12 +81,9 @@ class Array out.push_back((*i)->get<T>()); } } - - ValueVector::const_iterator begin() const { return values.begin(); } - ValueVector::const_iterator end() const { return values.end(); } private: - uint8_t typeOctet; + TypeCode type; ValueVector values; friend std::ostream& operator<<(std::ostream& out, const Array& body); diff --git a/cpp/src/qpid/framing/FieldValue.cpp b/cpp/src/qpid/framing/FieldValue.cpp index 9107ceeeea..ecf469236d 100644 --- a/cpp/src/qpid/framing/FieldValue.cpp +++ b/cpp/src/qpid/framing/FieldValue.cpp @@ -109,10 +109,10 @@ bool FieldValue::operator==(const FieldValue& v) const *data == *v.data; } -StringValue::StringValue(const std::string& v) : +Str8Value::Str8Value(const std::string& v) : FieldValue( - 0xA4, - new VariableWidthValue<4>( + TYPE_CODE_STR8, + new VariableWidthValue<1>( reinterpret_cast<const uint8_t*>(v.data()), reinterpret_cast<const uint8_t*>(v.data()+v.size()))) { @@ -168,4 +168,13 @@ ArrayValue::ArrayValue(const Array& a) : FieldValue(0xaa, new EncodedValue<Array { } +void FieldValue::print(std::ostream& out) const { + data->print(out); + out << TypeCode(typeOctet) << '('; + if (data->convertsToString()) out << data->getString(); + else if (data->convertsToInt()) out << data->getInt(); + else data->print(out); + out << ')'; +} + }} diff --git a/cpp/src/qpid/framing/FieldValue.h b/cpp/src/qpid/framing/FieldValue.h index 68081c5674..4f78d7f0f2 100644 --- a/cpp/src/qpid/framing/FieldValue.h +++ b/cpp/src/qpid/framing/FieldValue.h @@ -89,7 +89,8 @@ class FieldValue { void decode(Buffer& buffer); bool operator==(const FieldValue&) const; bool operator!=(const FieldValue& v) const { return !(*this == v); } - void print(std::ostream& out) const { out << "(0x" << std::hex << int(typeOctet) << ")"; data->print(out); } + + void print(std::ostream& out) const; template <typename T> bool convertsTo() const { return false; } template <typename T> T get() const { throw InvalidConversionException(); } @@ -239,12 +240,9 @@ class EncodedValue : public FieldValue::Data { void print(std::ostream& o) const { o << "[" << value << "]"; }; }; -/* - * Basic string value encodes as iso-8859-15 with 32 bit length - */ -class StringValue : public FieldValue { +class Str8Value : public FieldValue { public: - StringValue(const std::string& v); + Str8Value(const std::string& v); }; class Str16Value : public FieldValue { |
