summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/framing/Array.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/qpid/framing/Array.cpp')
-rw-r--r--cpp/src/qpid/framing/Array.cpp59
1 files changed, 31 insertions, 28 deletions
diff --git a/cpp/src/qpid/framing/Array.cpp b/cpp/src/qpid/framing/Array.cpp
index f0b6331ff3..d95e0d167d 100644
--- a/cpp/src/qpid/framing/Array.cpp
+++ b/cpp/src/qpid/framing/Array.cpp
@@ -18,9 +18,9 @@
* under the License.
*
*/
-#include "Array.h"
-#include "Buffer.h"
-#include "FieldValue.h"
+#include "qpid/framing/Array.h"
+#include "qpid/framing/Buffer.h"
+#include "qpid/framing/FieldValue.h"
#include "qpid/Exception.h"
#include "qpid/framing/reply_exceptions.h"
#include <assert.h>
@@ -28,25 +28,26 @@
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::size() const {
+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*/);
for(ValueVector::const_iterator i = values.begin(); i != values.end(); ++i) {
- len += (*i)->getData().size();
+ len += (*i)->getData().encodedSize();
}
return len;
}
@@ -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(size() - 4);//size added only when array is a top-level type
- buffer.putOctet(typeOctet);
+ buffer.putLong(encodedSize() - 4);//size added only when array is a top-level type
+ buffer.putOctet(type);
buffer.putLong(count());
for (ValueVector::const_iterator i = values.begin(); i!=values.end(); ++i) {
(*i)->getData().encode(buffer);
@@ -74,6 +75,7 @@ void Array::encode(Buffer& buffer) const{
}
void Array::decode(Buffer& buffer){
+ values.clear();
uint32_t size = buffer.getLong();//size added only when array is a top-level type
uint32_t available = buffer.available();
if (available < size) {
@@ -81,21 +83,21 @@ 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().size()) {
+ if (available < count * dummy.getData().encodedSize()) {
throw IllegalArgumentException(QPID_MSG("Not enough data for array, expected "
- << count << " items of " << dummy.getData().size()
+ << count << " items of " << dummy.getData().encodedSize()
<< " bytes each but only " << available << " bytes available"));
}
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 +106,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 +116,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, expected " << typeOctet));
+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);
}