Author: gsim
Date: Thu Aug 20 13:25:06 2009
New Revision: 806162

URL: http://svn.apache.org/viewvc?rev=806162&view=rev
Log:
Add List class to handle encoding of AMQP 0-10 list type.
Fill out accessors for different FieldValue types.


Added:
    qpid/trunk/qpid/cpp/include/qpid/framing/List.h
    qpid/trunk/qpid/cpp/src/qpid/framing/List.cpp
Modified:
    qpid/trunk/qpid/cpp/include/qpid/framing/FieldTable.h
    qpid/trunk/qpid/cpp/include/qpid/framing/FieldValue.h
    qpid/trunk/qpid/cpp/src/Makefile.am
    qpid/trunk/qpid/cpp/src/qpid/framing/FieldTable.cpp
    qpid/trunk/qpid/cpp/src/qpid/framing/FieldValue.cpp
    qpid/trunk/qpid/cpp/src/tests/FieldTable.cpp

Modified: qpid/trunk/qpid/cpp/include/qpid/framing/FieldTable.h
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/include/qpid/framing/FieldTable.h?rev=806162&r1=806161&r2=806162&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/include/qpid/framing/FieldTable.h (original)
+++ qpid/trunk/qpid/cpp/include/qpid/framing/FieldTable.h Thu Aug 20 13:25:06 
2009
@@ -51,6 +51,8 @@
     typedef boost::shared_ptr<FieldValue> ValuePtr;
     typedef std::map<std::string, ValuePtr> ValueMap;
     typedef ValueMap::iterator iterator;
+    typedef ValueMap::const_reference const_reference;
+    typedef ValueMap::value_type value_type;
 
     QPID_COMMON_EXTERN FieldTable() {};
     QPID_COMMON_EXTERN FieldTable(const FieldTable& ft);
@@ -97,12 +99,16 @@
     QPID_COMMON_EXTERN bool operator==(const FieldTable& other) const;
 
     // Map-like interface.
-    // TODO: may need to duplicate into versions that return mutable iterator
     ValueMap::const_iterator begin() const { return values.begin(); }
     ValueMap::const_iterator end() const { return values.end(); }
     ValueMap::const_iterator find(const std::string& s) const { return 
values.find(s); }
 
+    ValueMap::iterator begin() { return values.begin(); }
+    ValueMap::iterator end() { return values.end(); }
+    ValueMap::iterator find(const std::string& s) { return values.find(s); }
+
     std::pair <ValueMap::iterator, bool> insert(const ValueMap::value_type&);
+    ValueMap::iterator insert(ValueMap::iterator, const ValueMap::value_type&);
     void clear() { values.clear(); }
 
     // ### Hack Alert

Modified: qpid/trunk/qpid/cpp/include/qpid/framing/FieldValue.h
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/include/qpid/framing/FieldValue.h?rev=806162&r1=806161&r2=806162&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/include/qpid/framing/FieldValue.h (original)
+++ qpid/trunk/qpid/cpp/include/qpid/framing/FieldValue.h Thu Aug 20 13:25:06 
2009
@@ -24,6 +24,7 @@
 #include "qpid/Exception.h"
 #include "qpid/framing/amqp_types.h"
 #include "qpid/framing/Buffer.h"
+#include "qpid/framing/Endian.h"
 #include "qpid/framing/FieldTable.h"
 #include "qpid/CommonImportExport.h"
 
@@ -36,7 +37,6 @@
 namespace qpid {
 namespace framing {
 
-//class Array;
 /**
  * Exception that is the base exception for all field table errors.
  *
@@ -53,6 +53,8 @@
     InvalidConversionException() {}
 };
 
+class List;
+
 /**
  * Value that can appear in an AMQP field table
  *
@@ -96,12 +98,17 @@
     template <typename T> bool convertsTo() const { return false; }
     template <typename T> T get() const { throw InvalidConversionException(); }
 
+    template <class T, int W> T getIntegerValue() const;
+    template <class T, int W> T getFloatingPointValue() const;
+    template <class T> bool get(T&) const;
+
   protected:
     FieldValue(uint8_t t, Data* d): typeOctet(t), data(d) {}
 
   private:
     uint8_t typeOctet;
     std::auto_ptr<Data> data;
+
 };
 
 template <>
@@ -165,10 +172,52 @@
         return v;
     }
     uint8_t* rawOctets() { return octets; }
+    uint8_t* rawOctets() const { return octets; }
 
     void print(std::ostream& o) const { o << "F" << width << ":"; };
 };
 
+template <class T, int W>
+inline T FieldValue::getIntegerValue() const
+{
+    FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* 
const>(data.get());
+    if (fwv) {
+        uint8_t* octets = fwv->rawOctets();
+        T v = 0;
+        for (int i = 0; i < W-1; ++i) {
+            v |= octets[i]; v <<= 8;
+        }
+        v |= octets[W-1];
+        return v;
+    } else {
+        throw InvalidConversionException();
+    }
+}
+
+template <class T, int W>
+inline T FieldValue::getFloatingPointValue() const {
+    FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* 
const>(data.get());
+    if (fwv) {
+        T value;
+        uint8_t* const octets = Endian::convertIfRequired(fwv->rawOctets(), W);
+        uint8_t* const target = reinterpret_cast<uint8_t*>(&value);
+        for (uint i = 0; i < W; ++i) target[i] = octets[i];
+        return value;
+    } else {
+        throw InvalidConversionException();
+    }
+}
+
+template <>
+inline float FieldValue::get<float>() const {
+    return getFloatingPointValue<float, 4>();
+}
+
+template <>
+inline double FieldValue::get<double>() const {
+    return getFloatingPointValue<double, 8>();
+}
+
 template <>
 class FixedWidthValue<0> : public FieldValue::Data {
   public:
@@ -243,6 +292,27 @@
     void print(std::ostream& o) const { o << "[" << value << "]"; };
 };
 
+/**
+ * Accessor that can be used to get values of type FieldTable, Array
+ * and List.
+ */
+template <class T>
+inline bool FieldValue::get(T& t) const
+{
+    const EncodedValue<T>* v = dynamic_cast< EncodedValue<T>* >(data.get());   
 
+    if (v != 0) {
+        t = v->getValue();
+        return true;
+    } else {
+        try {
+            t = get<T>();
+            return true;
+        } catch (const InvalidConversionException&) {
+            return false;
+        }
+    }
+}
+
 class Str8Value : public FieldValue {
   public:
     QPID_COMMON_EXTERN Str8Value(const std::string& v);
@@ -294,6 +364,7 @@
 
 class FieldTableValue : public FieldValue {
   public:
+    typedef FieldTable ValueType;
     QPID_COMMON_EXTERN FieldTableValue(const FieldTable&);
 };
 
@@ -302,6 +373,49 @@
     QPID_COMMON_EXTERN ArrayValue(const Array&);
 };
 
+class VoidValue : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN VoidValue();
+};
+
+class BoolValue : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN BoolValue(bool);
+};
+
+class Unsigned8Value : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN Unsigned8Value(uint8_t);
+};
+
+class Unsigned16Value : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN Unsigned16Value(uint16_t);
+};
+
+class Unsigned32Value : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN Unsigned32Value(uint32_t);
+};
+
+class Integer8Value : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN Integer8Value(int8_t);
+};
+
+class Integer16Value : public FieldValue {
+  public:
+    QPID_COMMON_EXTERN Integer16Value(int16_t);
+};
+
+typedef IntegerValue Integer32Value;
+
+class ListValue : public FieldValue {
+  public:
+    typedef List ValueType;
+    QPID_COMMON_EXTERN ListValue(const List&);
+};
+
 template <class T>
 bool getEncodedValue(FieldTable::ValuePtr vptr, T& value)
 {
@@ -315,7 +429,6 @@
     return false;
 }
 
-
 }} // qpid::framing
 
 #endif

Added: qpid/trunk/qpid/cpp/include/qpid/framing/List.h
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/include/qpid/framing/List.h?rev=806162&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/include/qpid/framing/List.h (added)
+++ qpid/trunk/qpid/cpp/include/qpid/framing/List.h Thu Aug 20 13:25:06 2009
@@ -0,0 +1,76 @@
+#ifndef QPID_FRAMING_LIST_H
+#define QPID_FRAMING_LIST_H
+
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#include "qpid/CommonImportExport.h"
+#include "qpid/framing/amqp_types.h"
+#include <iostream>
+#include <list>
+#include <boost/shared_ptr.hpp>
+
+namespace qpid {
+namespace framing {
+
+class Buffer;
+class FieldValue;
+
+/**
+ * Representation of an AMQP 0-10 list
+ */
+class List
+{
+  public:
+    typedef boost::shared_ptr<FieldValue> ValuePtr;
+    typedef std::list<ValuePtr> Values;
+    typedef Values::const_iterator const_iterator;
+    typedef Values::iterator iterator;
+    typedef Values::const_reference const_reference;
+
+    QPID_COMMON_EXTERN uint32_t encodedSize() const;
+    QPID_COMMON_EXTERN void encode(Buffer& buffer) const;
+    QPID_COMMON_EXTERN void decode(Buffer& buffer);
+
+    QPID_COMMON_EXTERN bool operator==(const List& other) const;
+
+    // std collection interface.
+    QPID_COMMON_EXTERN const_iterator begin() const { return values.begin(); }
+    QPID_COMMON_EXTERN const_iterator end() const { return values.end(); }
+    QPID_COMMON_EXTERN iterator begin() { return values.begin(); }
+    QPID_COMMON_EXTERN iterator end(){ return values.end(); }
+
+    QPID_COMMON_EXTERN ValuePtr front() const { return values.front(); }
+    QPID_COMMON_EXTERN ValuePtr back() const { return values.back(); }
+    QPID_COMMON_EXTERN size_t size() const { return values.size(); }
+
+    QPID_COMMON_EXTERN iterator insert(iterator i, ValuePtr value) { return 
values.insert(i, value); }
+    QPID_COMMON_EXTERN void erase(iterator i) { values.erase(i); }
+    QPID_COMMON_EXTERN void push_back(ValuePtr value) { values.insert(end(), 
value); }
+    QPID_COMMON_EXTERN void pop_back() { values.pop_back(); }
+
+  private:
+    Values values;
+
+    friend QPID_COMMON_EXTERN std::ostream& operator<<(std::ostream& out, 
const List& list);
+};
+}} // namespace qpid::framing
+
+#endif  /*!QPID_FRAMING_LIST_H*/

Modified: qpid/trunk/qpid/cpp/src/Makefile.am
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/Makefile.am?rev=806162&r1=806161&r2=806162&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ qpid/trunk/qpid/cpp/src/Makefile.am Thu Aug 20 13:25:06 2009
@@ -378,6 +378,7 @@
   qpid/framing/InputHandler.h                  \
   qpid/framing/Invoker.h                       \
   qpid/framing/IsInSequenceSet.h               \
+  qpid/framing/List.cpp                                \
   qpid/framing/MethodBodyFactory.h             \
   qpid/framing/MethodContent.h                 \
   qpid/framing/ModelMethod.h                   \
@@ -723,6 +724,7 @@
   ../include/qpid/framing/Buffer.h             \
   ../include/qpid/framing/FieldTable.h         \
   ../include/qpid/framing/FieldValue.h         \
+  ../include/qpid/framing/List.h               \
   ../include/qpid/framing/ProtocolVersion.h    \
   ../include/qpid/framing/SequenceNumber.h     \
   ../include/qpid/framing/SequenceSet.h                \

Modified: qpid/trunk/qpid/cpp/src/qpid/framing/FieldTable.cpp
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/framing/FieldTable.cpp?rev=806162&r1=806161&r2=806162&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/framing/FieldTable.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/framing/FieldTable.cpp Thu Aug 20 13:25:06 2009
@@ -185,13 +185,8 @@
 bool getRawFixedWidthValue(FieldTable::ValuePtr vptr, T& value) 
 {
     if (vptr && vptr->getType() == typecode) {
-        FixedWidthValue<width>* fwv = dynamic_cast< FixedWidthValue<width>* 
>(&vptr->getData());
-        if (fwv) {
-            uint8_t* const octets = 
Endian::convertIfRequired(fwv->rawOctets(), width);
-            uint8_t* const target = reinterpret_cast<uint8_t*>(&value);
-            for (uint i = 0; i < width; ++i) target[i] = octets[i];
-            return true;
-        }
+        value = vptr->get<T>();
+        return true;
     }
     return false;
 }
@@ -370,5 +365,16 @@
         values.erase(name);
 }
 
+std::pair<FieldTable::ValueMap::iterator, bool> FieldTable::insert(const 
ValueMap::value_type& value)
+{
+    return values.insert(value);
+}
+
+FieldTable::ValueMap::iterator FieldTable::insert(ValueMap::iterator position, 
const ValueMap::value_type& value)
+{
+    return values.insert(position, value);
+}
+
+
 }
 }

Modified: qpid/trunk/qpid/cpp/src/qpid/framing/FieldValue.cpp
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/framing/FieldValue.cpp?rev=806162&r1=806161&r2=806162&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/framing/FieldValue.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/framing/FieldValue.cpp Thu Aug 20 13:25:06 2009
@@ -22,6 +22,7 @@
 #include "qpid/framing/Array.h"
 #include "qpid/framing/Buffer.h"
 #include "qpid/framing/Endian.h"
+#include "qpid/framing/List.h"
 #include "qpid/framing/reply_exceptions.h"
 
 namespace qpid {
@@ -37,6 +38,8 @@
     typeOctet = type;
     if (typeOctet == 0xA8) {
         data.reset(new EncodedValue<FieldTable>());
+    } else if (typeOctet == 0xA9) {
+        data.reset(new EncodedValue<List>());
     } else if (typeOctet == 0xAA) {
         data.reset(new EncodedValue<Array>());
     } else {    
@@ -164,10 +167,37 @@
 {
 }
 
+ListValue::ListValue(const List& l) : FieldValue(0xa9, new 
EncodedValue<List>(l))
+{
+}
+
 ArrayValue::ArrayValue(const Array& a) : FieldValue(0xaa, new 
EncodedValue<Array>(a))
 {
 }
 
+VoidValue::VoidValue() : FieldValue(0xf0, new FixedWidthValue<0>()) {}
+
+BoolValue::BoolValue(bool b) :
+    FieldValue(0x08, new FixedWidthValue<1>(b))
+{}
+
+Unsigned8Value::Unsigned8Value(uint8_t v) :
+    FieldValue(0x02, new FixedWidthValue<1>(v))
+{}
+Unsigned16Value::Unsigned16Value(uint16_t v) :
+    FieldValue(0x12, new FixedWidthValue<2>(v))
+{}
+Unsigned32Value::Unsigned32Value(uint32_t v) :
+    FieldValue(0x22, new FixedWidthValue<4>(v))
+{}
+
+Integer8Value::Integer8Value(int8_t v) :
+    FieldValue(0x01, new FixedWidthValue<1>(v))
+{}
+Integer16Value::Integer16Value(int16_t v) :
+    FieldValue(0x11, new FixedWidthValue<2>(v))
+{}
+
 void FieldValue::print(std::ostream& out) const {
     data->print(out);
     out << TypeCode(typeOctet) << '(';

Added: qpid/trunk/qpid/cpp/src/qpid/framing/List.cpp
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/framing/List.cpp?rev=806162&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/framing/List.cpp (added)
+++ qpid/trunk/qpid/cpp/src/qpid/framing/List.cpp Thu Aug 20 13:25:06 2009
@@ -0,0 +1,83 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#include "qpid/framing/List.h"
+#include "qpid/framing/Buffer.h"
+#include "qpid/framing/FieldValue.h"
+#include "qpid/Exception.h"
+#include "qpid/framing/reply_exceptions.h"
+
+namespace qpid {
+namespace framing {
+
+uint32_t List::encodedSize() const
+{
+    uint32_t len(4/*size*/ + 4/*count*/);
+    for(Values::const_iterator i = values.begin(); i != values.end(); ++i) {
+       len += (*i)->encodedSize();
+    }
+    return len;
+}
+
+void List::encode(Buffer& buffer) const
+{
+    buffer.putLong(encodedSize() - 4);
+    buffer.putLong(size());
+    for (Values::const_iterator i = values.begin(); i!=values.end(); ++i) {
+       (*i)->encode(buffer);
+    }
+}
+
+void List::decode(Buffer& buffer)
+{
+    values.clear();
+    uint32_t size = buffer.getLong();
+    uint32_t available = buffer.available();
+    if (available < size) {
+        throw IllegalArgumentException(QPID_MSG("Not enough data for list, 
expected " 
+                                                << size << " bytes but only " 
<< available << " available"));
+    }
+    if (size) {
+        uint32_t count = buffer.getLong();        
+        for (uint32_t i = 0; i < count; i++) {
+            ValuePtr value(new FieldValue);
+            value->decode(buffer);
+            values.push_back(value);
+        }    
+    }
+}
+
+
+bool List::operator==(const List& other) const {
+    return values.size() == other.values.size() && 
+        std::equal(values.begin(), values.end(), other.values.begin());
+}
+
+std::ostream& operator<<(std::ostream& out, const List& l)
+{
+    out << "{";
+    for(List::Values::const_iterator i = l.values.begin(); i != 
l.values.end(); ++i) {
+        if (i != l.values.begin()) out << ", ";
+        (*i)->print(out);
+    }
+    return out << "}";
+}
+
+}} // namespace qpid::framing

Modified: qpid/trunk/qpid/cpp/src/tests/FieldTable.cpp
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/tests/FieldTable.cpp?rev=806162&r1=806161&r2=806162&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/tests/FieldTable.cpp (original)
+++ qpid/trunk/qpid/cpp/src/tests/FieldTable.cpp Thu Aug 20 13:25:06 2009
@@ -22,6 +22,7 @@
 #include "qpid/framing/Array.h"
 #include "qpid/framing/FieldTable.h"
 #include "qpid/framing/FieldValue.h"
+#include "qpid/framing/List.h"
 #include "qpid/sys/alloca.h"
 
 #include "unit_test.h"
@@ -86,7 +87,9 @@
 
 QPID_AUTO_TEST_CASE(testNestedValues)
 {
-    char buff[100];
+    double d = 1.2345;
+    uint32_t u = 101;
+    char buff[1000];
     {
         FieldTable a;
         FieldTable b;
@@ -94,11 +97,17 @@
         items.push_back("one");
         items.push_back("two");
         Array c(items);
+        List list;
+        list.push_back(List::ValuePtr(new Str16Value("red")));
+        list.push_back(List::ValuePtr(new Unsigned32Value(u)));
+        list.push_back(List::ValuePtr(new Str8Value("yellow")));
+        list.push_back(List::ValuePtr(new DoubleValue(d)));
         
         a.setString("id", "A");
         b.setString("id", "B");
         a.setTable("B", b);
         a.setArray("C", c);
+        a.set("my-list", FieldTable::ValuePtr(new ListValue(list)));
 
 
         Buffer wbuffer(buff, 100);
@@ -119,6 +128,27 @@
         BOOST_CHECK((uint) 2 == items.size());
         BOOST_CHECK(string("one") == items[0]);
         BOOST_CHECK(string("two") == items[1]);
+
+        List list;
+        BOOST_CHECK(a.get("my-list")->get<List>(list));
+        List::const_iterator i = list.begin();
+        BOOST_CHECK(i != list.end());
+        BOOST_CHECK_EQUAL(std::string("red"), (*i)->get<std::string>());
+
+        i++;
+        BOOST_CHECK(i != list.end());
+        BOOST_CHECK_EQUAL(u, (uint32_t) (*i)->get<int>());
+
+        i++;
+        BOOST_CHECK(i != list.end());
+        BOOST_CHECK_EQUAL(std::string("yellow"), (*i)->get<std::string>());
+
+        i++;
+        BOOST_CHECK(i != list.end());
+        BOOST_CHECK_EQUAL(d, (*i)->get<double>());
+
+        i++;
+        BOOST_CHECK(i == list.end());
     }
 }
 



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscr...@qpid.apache.org

Reply via email to