From 95e4dfc625359100aeae219b11d3d751a6642f8f Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 22 Mar 2013 18:03:36 +0100 Subject: [PATCH] eoserial: packing and unpacking of map --- eo/src/serial/Utils.h | 44 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/eo/src/serial/Utils.h b/eo/src/serial/Utils.h index 4d943bf63..b6ea360b3 100644 --- a/eo/src/serial/Utils.h +++ b/eo/src/serial/Utils.h @@ -29,6 +29,7 @@ Authors: # include "Traits.h" # include +# include /** * @file Utils.h @@ -37,8 +38,6 @@ Authors: * * @todo encapsulate implementations. * - * @todo provide more composite implementations (map) - * * Example * * @code @@ -98,6 +97,12 @@ int main() vec.push_back(7); o["vec"] = eoserial::pack( vec ); + std::map str2int; + str2int["one"] = 1; + str2int["two"] = 2; + str2int["answer"] = 42; + o["map"] = eoserial::pack( str2int ); + // print it o.print( std::cout ); @@ -120,6 +125,10 @@ int main() } std::cout << std::endl; + std::map< std::string, int > readMap; + eoserial::unpack( o, "map", readMap ); + std::cout << "The answer is " << readMap["answer"] << std::endl; + obj.value = -1; // unpack object the same way eoserial::unpack( o, "obj", obj ); @@ -174,6 +183,21 @@ namespace eoserial unpackBasePushBack( obj, l ); } + /** + * @brief Unpack method for std::map< std::string, T > + */ + template< class T > + inline void unpackBase( const Entity* entity, std::map & m ) + { + const Object* obj = static_cast< const Object* >( entity ); + for( auto it = obj->begin(), end = obj->end(); + it != end; + ++it ) + { + unpackBase( it->second, m[ it->first ] ); + } + } + /** * @brief Unpack implementation for non eoserial::Persistent objects. * @@ -301,6 +325,22 @@ namespace eoserial return packIterable( l ); } + /** + * @brief Pack method for std::map< std::string, T > + */ + template + inline Entity* pack( const std::map& map ) + { + Object* obj = new Object; + for( auto it = map.begin(), end = map.end(); + it != end; + ++it ) + { + (*obj)[ it->first ] = pack( it->second ); + } + return obj; + } + /** * @brief Universal pack method. *