summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2007-07-04 05:26:26 +0000
committerAlan Conway <aconway@apache.org>2007-07-04 05:26:26 +0000
commitd4be469092c558ca9031d82b963b8b845fa1e1bd (patch)
tree745ccfec0f180932ab2f3a9abe31a708a49c020c /cpp
parent90e275cc082f3ec71c4b879c2ea4d037d4128278 (diff)
downloadqpid-python-d4be469092c558ca9031d82b963b8b845fa1e1bd.tar.gz
Encode/decode for UUIDs.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@553083 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rw-r--r--cpp/src/qpid/framing/Buffer.cpp10
-rw-r--r--cpp/src/qpid/framing/Buffer.h3
-rw-r--r--cpp/src/qpid/framing/Uuid.cpp16
-rw-r--r--cpp/src/qpid/framing/Uuid.h5
-rw-r--r--cpp/src/tests/Uuid.cpp11
5 files changed, 44 insertions, 1 deletions
diff --git a/cpp/src/qpid/framing/Buffer.cpp b/cpp/src/qpid/framing/Buffer.cpp
index 2a14e854d0..6c6b2661bd 100644
--- a/cpp/src/qpid/framing/Buffer.cpp
+++ b/cpp/src/qpid/framing/Buffer.cpp
@@ -181,3 +181,13 @@ void qpid::framing::Buffer::getRawData(string& s, uint32_t len){
s.assign(data + position, len);
position += len;
}
+
+void qpid::framing::Buffer::putRawData(const uint8_t* s, size_t len){
+ memcpy(data + position, s, len);
+ position += len;
+}
+
+void qpid::framing::Buffer::getRawData(uint8_t* s, size_t len){
+ memcpy(s, data + position, len);
+ position += len;
+}
diff --git a/cpp/src/qpid/framing/Buffer.h b/cpp/src/qpid/framing/Buffer.h
index e1a3fb065a..d35935ad19 100644
--- a/cpp/src/qpid/framing/Buffer.h
+++ b/cpp/src/qpid/framing/Buffer.h
@@ -78,6 +78,9 @@ public:
void putRawData(const string& s);
void getRawData(string& s, uint32_t size);
+ void putRawData(const uint8_t* data, size_t size);
+ void getRawData(uint8_t* data, size_t size);
+
};
}} // namespace qpid::framing
diff --git a/cpp/src/qpid/framing/Uuid.cpp b/cpp/src/qpid/framing/Uuid.cpp
index 47010ba515..b1523f0d61 100644
--- a/cpp/src/qpid/framing/Uuid.cpp
+++ b/cpp/src/qpid/framing/Uuid.cpp
@@ -17,7 +17,11 @@
*/
#include "Uuid.h"
-#include "uuid/uuid.h"
+
+#include "qpid/QpidError.h"
+#include "qpid/framing/Buffer.h"
+
+#include <uuid/uuid.h>
namespace qpid {
namespace framing {
@@ -30,6 +34,16 @@ Uuid::Uuid(uint8_t* uu) { uuid_copy(c_array(),uu); }
static const size_t UNPARSED_SIZE=36;
+void Uuid::encode(Buffer& buf) {
+ buf.putRawData(data(), size());
+}
+
+void Uuid::decode(Buffer& buf) {
+ if (buf.available() < size())
+ THROW_QPID_ERROR(FRAMING_ERROR, "Not enough data for UUID.");
+ buf.getRawData(c_array(), size());
+}
+
ostream& operator<<(ostream& out, const Uuid& uuid) {
char unparsed[UNPARSED_SIZE + 1];
uuid_unparse(uuid.data(), unparsed);
diff --git a/cpp/src/qpid/framing/Uuid.h b/cpp/src/qpid/framing/Uuid.h
index 8fa5abd4dc..a2f415b118 100644
--- a/cpp/src/qpid/framing/Uuid.h
+++ b/cpp/src/qpid/framing/Uuid.h
@@ -26,6 +26,8 @@
namespace qpid {
namespace framing {
+class Buffer;
+
/**
* A UUID is represented as a boost::array of 16 bytes.
*
@@ -41,6 +43,9 @@ struct Uuid : public boost::array<uint8_t, 16> {
// Default op= and copy ctor are fine.
// boost::array gives us ==, < etc.
+
+ void encode(framing::Buffer& buf);
+ void decode(framing::Buffer& buf);
};
/** Print in format 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb */
diff --git a/cpp/src/tests/Uuid.cpp b/cpp/src/tests/Uuid.cpp
index 97e7518c85..43d1cbcbba 100644
--- a/cpp/src/tests/Uuid.cpp
+++ b/cpp/src/tests/Uuid.cpp
@@ -17,6 +17,7 @@
*/
#include "qpid/framing/Uuid.h"
+#include "qpid/framing/Buffer.h"
#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>
@@ -58,3 +59,13 @@ BOOST_AUTO_TEST_CASE(testUuidOstream) {
BOOST_CHECK(out.good());
BOOST_CHECK_EQUAL(out.str(), sampleStr);
}
+
+BOOST_AUTO_TEST_CASE(testUuidEncodeDecode) {
+ Buffer buf(Uuid::size());
+ Uuid uuid;
+ uuid.encode(buf);
+ buf.flip();
+ Uuid decoded;
+ decoded.decode(buf);
+ BOOST_CHECK(uuid==decoded);
+}