diff options
author | Gordon Sim <gsim@apache.org> | 2008-09-19 22:31:45 +0000 |
---|---|---|
committer | Gordon Sim <gsim@apache.org> | 2008-09-19 22:31:45 +0000 |
commit | 7c70d21ca2d788d4432cfa89851c9b928c9f30aa (patch) | |
tree | 01ec869bf97701c6de28cdf06f9db11f8782a793 /cpp | |
parent | ceababe179b3f80a9444eef358f7ff96c81f7a18 (diff) | |
download | qpid-python-7c70d21ca2d788d4432cfa89851c9b928c9f30aa.tar.gz |
Support floats and doubles in field tables.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@697269 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/qpid/framing/FieldTable.cpp | 29 | ||||
-rw-r--r-- | cpp/src/qpid/framing/FieldTable.h | 4 | ||||
-rw-r--r-- | cpp/src/qpid/framing/FieldValue.cpp | 8 | ||||
-rw-r--r-- | cpp/src/qpid/framing/FieldValue.h | 20 | ||||
-rw-r--r-- | cpp/src/tests/FieldTable.cpp | 35 |
5 files changed, 95 insertions, 1 deletions
diff --git a/cpp/src/qpid/framing/FieldTable.cpp b/cpp/src/qpid/framing/FieldTable.cpp index a85f5e0918..cf8f1b5eed 100644 --- a/cpp/src/qpid/framing/FieldTable.cpp +++ b/cpp/src/qpid/framing/FieldTable.cpp @@ -87,6 +87,14 @@ void FieldTable::setArray(const std::string& name, const Array& value) values[name] = ValuePtr(new ArrayValue(value)); } +void FieldTable::setFloat(const std::string& name, float value){ + values[name] = ValuePtr(new FloatValue(value)); +} + +void FieldTable::setDouble(const std::string& name, double value){ + values[name] = ValuePtr(new DoubleValue(value)); +} + FieldTable::ValuePtr FieldTable::get(const std::string& name) const { ValuePtr value; @@ -131,6 +139,27 @@ bool FieldTable::getArray(const std::string& name, Array& value) const { return getEncodedValue<Array>(get(name), value); } +template <class T, int width, uint8_t typecode> +bool getRawFixedWidthValue(FieldTable::ValuePtr vptr, T& value) +{ + if (vptr && vptr->getType() == typecode) { + FixedWidthValue<width>* fwv = dynamic_cast< FixedWidthValue<width>* >(&vptr->getData()); + if (fwv) { + fwv->copyInto(reinterpret_cast<uint8_t*>(&value)); + return true; + } + } + return false; +} + +bool FieldTable::getFloat(const std::string& name, float& value) const { + return getRawFixedWidthValue<float, 4, 0x23>(get(name), value); +} + +bool FieldTable::getDouble(const std::string& name, double& value) const { + return getRawFixedWidthValue<double, 8, 0x33>(get(name), value); +} + void FieldTable::encode(Buffer& buffer) const{ buffer.putLong(size() - 4); buffer.putLong(values.size()); diff --git a/cpp/src/qpid/framing/FieldTable.h b/cpp/src/qpid/framing/FieldTable.h index ba440b1432..3c56d1e81a 100644 --- a/cpp/src/qpid/framing/FieldTable.h +++ b/cpp/src/qpid/framing/FieldTable.h @@ -66,6 +66,8 @@ class FieldTable void setTimestamp(const std::string& name, uint64_t value); void setTable(const std::string& name, const FieldTable& value); void setArray(const std::string& name, const Array& value); + void setFloat(const std::string& name, float value); + void setDouble(const std::string& name, double value); //void setDecimal(string& name, xxx& value); std::string getString(const std::string& name) const; @@ -73,6 +75,8 @@ class FieldTable // uint64_t getTimestamp(const std::string& name) const; bool getTable(const std::string& name, FieldTable& value) const; bool getArray(const std::string& name, Array& value) const; + bool getFloat(const std::string& name, float& value) const; + bool getDouble(const std::string& name, double& value) const; // //void getDecimal(string& name, xxx& value); // //void erase(const std::string& name); diff --git a/cpp/src/qpid/framing/FieldValue.cpp b/cpp/src/qpid/framing/FieldValue.cpp index 3b3c2f2126..bbef9ebceb 100644 --- a/cpp/src/qpid/framing/FieldValue.cpp +++ b/cpp/src/qpid/framing/FieldValue.cpp @@ -138,6 +138,14 @@ IntegerValue::IntegerValue(int v) : { } +FloatValue::FloatValue(float v) : + FieldValue(0x23, new FixedWidthValue<4>(reinterpret_cast<uint8_t*>(&v))) +{} + +DoubleValue::DoubleValue(double v) : + FieldValue(0x33, new FixedWidthValue<8>(reinterpret_cast<uint8_t*>(&v))) +{} + TimeValue::TimeValue(uint64_t v) : FieldValue(0x32, new FixedWidthValue<8>(v)) { diff --git a/cpp/src/qpid/framing/FieldValue.h b/cpp/src/qpid/framing/FieldValue.h index a38b559239..232c4a5cad 100644 --- a/cpp/src/qpid/framing/FieldValue.h +++ b/cpp/src/qpid/framing/FieldValue.h @@ -126,6 +126,10 @@ class FixedWidthValue : public FieldValue::Data { public: FixedWidthValue() {} FixedWidthValue(const uint8_t (&data)[width]) : octets(data) {} + FixedWidthValue(const uint8_t* const data) + { + for (int i = 0; i < width; i++) octets[i] = data[i]; + } FixedWidthValue(uint64_t v) { for (int i = width; i > 0; --i) { @@ -133,7 +137,6 @@ class FixedWidthValue : public FieldValue::Data { } octets[0] = (uint8_t) (0xFF & v); } - uint32_t size() const { return width; } void encode(Buffer& buffer) { buffer.putRawData(octets, width); } void decode(Buffer& buffer) { buffer.getRawData(octets, width); } @@ -153,6 +156,10 @@ class FixedWidthValue : public FieldValue::Data { v |= octets[width-1]; return v; } + void copyInto(uint8_t* const data) const + { + for (uint i = 0; i < width; ++i) data[i] = octets[i]; + } void print(std::ostream& o) const { o << "F" << width << ":"; }; }; @@ -247,6 +254,16 @@ class Struct32Value : public FieldValue { Struct32Value(const std::string& v); }; +class FloatValue : public FieldValue +{ + public: + FloatValue(float f); +}; +class DoubleValue : public FieldValue +{ + public: + DoubleValue(double f); +}; /* * Basic integer value encodes as signed 32 bit @@ -285,6 +302,7 @@ bool getEncodedValue(FieldTable::ValuePtr vptr, T& value) return false; } + }} // qpid::framing #endif diff --git a/cpp/src/tests/FieldTable.cpp b/cpp/src/tests/FieldTable.cpp index 22db287140..953b853cd6 100644 --- a/cpp/src/tests/FieldTable.cpp +++ b/cpp/src/tests/FieldTable.cpp @@ -122,4 +122,39 @@ QPID_AUTO_TEST_CASE(testNestedValues) } } +QPID_AUTO_TEST_CASE(testFloatAndDouble) +{ + char buff[100]; + float f = 5.672; + double d = 56.720001; + { + FieldTable a; + a.setString("string", "abc"); + a.setInt("int", 5672); + a.setFloat("float", f); + a.setDouble("double", d); + + Buffer wbuffer(buff, 100); + wbuffer.put(a); + } + { + Buffer rbuffer(buff, 100); + FieldTable a; + rbuffer.get(a); + BOOST_CHECK(string("abc") == a.getString("string")); + BOOST_CHECK(5672 == a.getInt("int")); + float f2; + BOOST_CHECK(!a.getFloat("string", f2)); + BOOST_CHECK(!a.getFloat("int", f2)); + BOOST_CHECK(a.getFloat("float", f2)); + BOOST_CHECK(f2 == f); + + double d2; + BOOST_CHECK(!a.getDouble("string", d2)); + BOOST_CHECK(!a.getDouble("int", d2)); + BOOST_CHECK(a.getDouble("double", d2)); + BOOST_CHECK(d2 == d); + } +} + QPID_AUTO_TEST_SUITE_END() |