diff options
Diffstat (limited to 'qpid/cpp')
-rw-r--r-- | qpid/cpp/include/qpid/amqp_0_10/Codecs.h | 10 | ||||
-rw-r--r-- | qpid/cpp/src/qpid/messaging/Message.cpp | 73 |
2 files changed, 47 insertions, 36 deletions
diff --git a/qpid/cpp/include/qpid/amqp_0_10/Codecs.h b/qpid/cpp/include/qpid/amqp_0_10/Codecs.h index 78b0aa671b..62370a0e5e 100644 --- a/qpid/cpp/include/qpid/amqp_0_10/Codecs.h +++ b/qpid/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/qpid/cpp/src/qpid/messaging/Message.cpp b/qpid/cpp/src/qpid/messaging/Message.cpp index bbbb257b18..753d3cec1b 100644 --- a/qpid/cpp/src/qpid/messaging/Message.cpp +++ b/qpid/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 <class C> 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 <class T> static void decode(const Message& message, T& object, const std::string& encoding) + { + checkEncoding(message, encoding); + C::decode(message.getContent(), object); + } + + template <class T> 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<qpid::amqp_0_10::MapCodec>::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<qpid::amqp_0_10::ListCodec>::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<qpid::amqp_0_10::MapCodec>::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<qpid::amqp_0_10::ListCodec>::encode(list, message, encoding); } }} // namespace qpid::messaging |