From eadb91781ab0d6e786500a3798f1bf2a83293a05 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Tue, 11 Mar 2008 22:13:10 +0000 Subject: rubygen/0-10/specification.rb: - Simplified enum mapping/encoding. - struct encoding - ostream << operators src/qpid/Serializer.h - free funciton serialization - separate Encoder/Decoder for const correctness - split() to allow separate encode/decode for complex cases. src/qpid/amqp_0_10/Assembly.cpp, Assembly.h: AMQP 0-10 final Assembly src/qpid/amqp_0_10/Codec.h - Replaced enable_if with overloads, simpler & more flexible. src/qpid/amqp_0_10/Frame.cpp, .h: AMQP 0-10 final frame. src/qpid/amqp_0_10/Holder.h: - provide const and non-const apply src/qpid/amqp_0_10/Segment.cpp, .h: AMQP 0-10 final Segment. src/qpid/amqp_0_10/apply.h - ConstApplyFunctor for const apply. src/qpid/amqp_0_10/built_in_types.h - SerializableString encoding src/qpid/amqp_0_10/complex_types.cpp, .h - const application - Action base class for command/control. src/qpid/framing/AMQBody.h - removed 0-10 final changes, moving integration point down the stack. src/qpid/sys/Shlib.h - removed unused and uncompilable (on some compilers) function. src/qpid/sys/Time.h, .cpp - ostream << for AbsTime and Duration. src/tests/Assembly.cpp, Segment.cpp, apply.cpp, serialize.cpp: testing new code. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@636126 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/Serializer.h | 84 +++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 36 deletions(-) (limited to 'cpp/src/qpid/Serializer.h') diff --git a/cpp/src/qpid/Serializer.h b/cpp/src/qpid/Serializer.h index 34b95d3ffd..e0e29e24fd 100644 --- a/cpp/src/qpid/Serializer.h +++ b/cpp/src/qpid/Serializer.h @@ -23,59 +23,71 @@ */ #include -#include -#include -#include +#include namespace qpid { +namespace serialize { -// FIXME aconway 2008-03-03: Doc - esp decoding -template class Serializer { - public: - typedef Serializer result_type; // unary functor requirement. +// FIXME aconway 2008-03-03: Document. +// Encoder/Decoder concept: add op() for primitive types, raw(), +// op()(Iter, Iter). Note split, encode, decode. +// - static const bool IS_DECODER=false; +// FIXME aconway 2008-03-09: document - non-intrusive serialzation. +// Default rule calls member. Enums must provide an override rule. - /** Generic handler for class objects, call serialize() */ - template - typename boost::enable_if, Derived&>::type - operator()(T& t) { - t.serialize(self()); - return self(); - } +/** Overload for types that do not provide a serialize() member.*/ +template T& serializable(T& t) { return t; } + +template class Encoder { + public: + typedef Derived& result_type; // unary functor requirement. - /** Generic handler for const class objects, call serialize() */ + /** Default op() calls serializable() free function */ template - typename boost::enable_if, Derived&>::type - operator()(const T& t) { - assert(!Derived::IS_DECODER); // We won't modify the value. - // const_cast so we don't need 2 serialize() members for every class. - const_cast(t).serialize(self()); - return self(); + Derived& operator()(const T& t) { + serializable(const_cast(t)).serialize(self()); return self(); } - template struct Split { - Split(Derived& s, T& t) { t.encode(s); } - }; - - template struct Split { - Split(Derived& s, T& t) { t.decode(s); } - }; - /** - * Called by classes that want to receive separate - * encode()/decode() calls. - */ + /** Split serialize() into encode()/decode() */ template - void split(T& t) { Split(self(),t); } - + Derived& split(const T& t) { t.encode(self()); return self(); } + private: Derived& self() { return *static_cast(this); } }; +template class Decoder { + public: + typedef Derived& result_type; // unary functor requirement. + + /** Default op() calls serializable() free function */ + template + Derived& operator()(T& t) { + serializable(t).serialize(self()); return self(); + } + /** Split serialize() into encode()/decode() */ + template + Derived& split(T& t) { t.decode(self()); return self(); } + + private: + Derived& self() { return *static_cast(this); } +}; +/** Serialize a type by converting it to/from another type */ +template +struct SerializeAs { + Type& value; + SerializeAs(Type & t) : value(t) {} + template void serialize(S& s) { s.split(*this); } + template void encode(S& s) const { s(AsType(value)); } + template void decode(S& s) { AsType x; s(x); value=x; } +}; -} // namespace qpid +}} // namespace qpid::serialize +// FIXME aconway 2008-03-09: rename to serialize.h +// #endif /*!QPID_SERIALIZER_H*/ -- cgit v1.2.1