From 5ab91c61392cf42f8d96839013cb7aacd09d3700 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 22 Mar 2013 17:05:42 +0100 Subject: [PATCH] eoserial: merged former Serialize.h with Utils.h: pack / unpack. --- eo/src/serial/Utils.h | 132 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 2 deletions(-) diff --git a/eo/src/serial/Utils.h b/eo/src/serial/Utils.h index 13b6877b1..8046bb557 100644 --- a/eo/src/serial/Utils.h +++ b/eo/src/serial/Utils.h @@ -26,6 +26,22 @@ Authors: # include "SerialObject.h" # include "SerialString.h" +# include "Traits.h" + +# include + +/** + * @file Utils.h + * + * @brief Contains utilities for simple serialization and deserialization. + * + * @todo comment new version. + * + * @todo encapsulate implementations. + * + * @todo provide more composite implementations (map) + */ + namespace eoserial { /* *************************** @@ -42,11 +58,123 @@ namespace eoserial */ template< class T > - inline void unpack( const Object & obj, const std::string & key, T & value ) + inline void unpackBasePushBack( const Entity* obj, T& container ) { - static_cast( obj.find( key )->second )->deserialize( value ); + const Array* arr = static_cast( obj ); + for( auto it = arr->begin(), end = arr->end(); + it != end; + ++it ) + { + typename T::value_type item; + unpackBase( *it, item ); + container.push_back( item ); + } } + template< class T > + inline void unpackBase( const Entity* obj, std::vector& v ) + { + unpackBasePushBack( obj, v ); + } + + template< class T > + inline void unpackBase( const Entity* obj, std::list& l ) + { + unpackBasePushBack( obj, l ); + } + + template + struct UnpackImpl + { + void operator()( const Entity* obj, T& value ) + { + static_cast( obj )->deserialize( value ); + } + }; + + template + struct UnpackImpl + { + void operator()( const Entity* obj, T& value ) + { + value.unpack( static_cast(obj) ); + } + }; + + template + inline void unpackBase( const Entity* obj, T& value ) + { + UnpackImpl< T, IsDerivedFrom< T, Persistent >::value > impl; + impl( obj, value ); + } + + template + inline void unpack( const Object& obj, const std::string& key, T& value ) + { + unpackBase( obj.find(key)->second, value ); + } + + /* ******************* + * SERIALIZATION ***** + ********************/ + + template + struct PackImpl + { + Entity* operator()( const T& value ) + { + std::stringstream ss; + ss.precision(std::numeric_limits::digits10 + 1); + ss << value; + return new String(ss.str()); + } + }; + + template + struct PackImpl + { + Entity* operator()( const T& value ) + { + return value.pack(); + } + }; + + template + inline Entity* pack( const T& value ); + + template + inline Entity* packIterable( const T& container ) + { + Array* arr = new Array; + for( auto it = container.begin(), end = container.end(); + it != end; + ++it ) + { + arr->push_back( pack(*it) ); + } + return arr; + } + + template + inline Entity* pack( const std::vector& v ) + { + return packIterable( v ); + } + + template + inline Entity* pack( const std::list& l ) + { + return packIterable( l ); + } + + template + inline Entity* pack( const T& value ) + { + PackImpl::value> impl; + return impl( value ); + } + + // Kept for compatibility inline void unpackObject( const Object & obj, const std::string & key, Persistent & value ) { static_cast( obj.find( key )->second )->deserialize( value );