From be01dfd2d74634abf3387e8eb363d2c90b31cf0d Mon Sep 17 00:00:00 2001 From: Gordon Sim Date: Thu, 8 Apr 2010 09:47:21 +0000 Subject: QPID-664: set content-type on encoded messages, removed some duplication and added in a typedef to codecs that may be useful git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@931851 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/include/qpid/amqp_0_10/Codecs.h | 10 +++-- cpp/src/qpid/messaging/Message.cpp | 73 +++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 36 deletions(-) (limited to 'cpp') diff --git a/cpp/include/qpid/amqp_0_10/Codecs.h b/cpp/include/qpid/amqp_0_10/Codecs.h index 78b0aa671b..62370a0e5e 100644 --- a/cpp/include/qpid/amqp_0_10/Codecs.h +++ b/cpp/include/qpid/amqp_0_10/Codecs.h @@ -37,8 +37,9 @@ namespace amqp_0_10 { class QPID_COMMON_EXTERN MapCodec { public: - static void encode(const qpid::types::Variant::Map&, std::string&); - static void decode(const std::string&, qpid::types::Variant::Map&); + typedef qpid::types::Variant::Map ObjectType; + static void encode(const ObjectType&, std::string&); + static void decode(const std::string&, ObjectType&); static const std::string contentType; private: }; @@ -50,8 +51,9 @@ class QPID_COMMON_EXTERN MapCodec class QPID_COMMON_EXTERN ListCodec { public: - static void encode(const qpid::types::Variant::List&, std::string&); - static void decode(const std::string&, qpid::types::Variant::List&); + typedef qpid::types::Variant::List ObjectType; + static void encode(const ObjectType&, std::string&); + static void decode(const std::string&, ObjectType&); static const std::string contentType; private: }; diff --git a/cpp/src/qpid/messaging/Message.cpp b/cpp/src/qpid/messaging/Message.cpp index bbbb257b18..753d3cec1b 100644 --- a/cpp/src/qpid/messaging/Message.cpp +++ b/cpp/src/qpid/messaging/Message.cpp @@ -83,56 +83,65 @@ size_t Message::getContentSize() const return impl->getBytes().size(); } - EncodingException::EncodingException(const std::string& msg) : qpid::Exception(msg) {} const std::string BAD_ENCODING("Unsupported encoding: %1% (only %2% is supported at present)."); -bool checkEncoding(const std::string& requested, const std::string& supported) +template struct MessageCodec { - if (requested.size()) { - if (requested == supported) return true; - else throw EncodingException((boost::format(BAD_ENCODING) % requested % supported).str()); - } else { - return false; + static bool checkEncoding(const std::string& requested) + { + if (requested.size()) { + if (requested == C::contentType) return true; + else throw EncodingException((boost::format(BAD_ENCODING) % requested % C::contentType).str()); + } else { + return false; + } + } + + /* + * Currently only support a single encoding type for both list and + * map, based on AMQP 0-10, though wider support is anticipated in the + * future. This method simply checks that the desired encoding (if one + * is specified, either through the message-content or through an + * override) is indeed supported. + */ + static void checkEncoding(const Message& message, const std::string& requested) + { + checkEncoding(requested) || checkEncoding(message.getContentType()); } -} -/* - * Currently only support a single encoding type for both list and - * map, based on AMQP 0-10, though wider support is anticipated in the - * future. This method simply checks that the desired encoding (if one - * is specified, either through the message-content or through an - * override) is indeed supported. - */ -void checkEncoding(const Message& message, const std::string& requested, const std::string& supported) -{ - checkEncoding(requested, supported) || checkEncoding(message.getContentType(), supported); -} - + template static void decode(const Message& message, T& object, const std::string& encoding) + { + checkEncoding(message, encoding); + C::decode(message.getContent(), object); + } + + template static void encode(const T& map, Message& message, const std::string& encoding) + { + checkEncoding(message, encoding); + std::string content; + C::encode(map, content); + message.setContentType(C::contentType); + message.setContent(content); + } +}; + void decode(const Message& message, Variant::Map& map, const std::string& encoding) { - checkEncoding(message, encoding, qpid::amqp_0_10::MapCodec::contentType); - qpid::amqp_0_10::MapCodec::decode(message.getContent(), map); + MessageCodec::decode(message, map, encoding); } void decode(const Message& message, Variant::List& list, const std::string& encoding) { - checkEncoding(message, encoding, qpid::amqp_0_10::ListCodec::contentType); - qpid::amqp_0_10::ListCodec::decode(message.getContent(), list); + MessageCodec::decode(message, list, encoding); } void encode(const Variant::Map& map, Message& message, const std::string& encoding) { - checkEncoding(message, encoding, qpid::amqp_0_10::MapCodec::contentType); - std::string content; - qpid::amqp_0_10::MapCodec::encode(map, content); - message.setContent(content); + MessageCodec::encode(map, message, encoding); } void encode(const Variant::List& list, Message& message, const std::string& encoding) { - checkEncoding(message, encoding, qpid::amqp_0_10::ListCodec::contentType); - std::string content; - qpid::amqp_0_10::ListCodec::encode(list, content); - message.setContent(content); + MessageCodec::encode(list, message, encoding); } }} // namespace qpid::messaging -- cgit v1.2.1