eoserial: packing and unpacking of map<string, T>
This commit is contained in:
parent
6c148f8ae2
commit
95e4dfc625
1 changed files with 42 additions and 2 deletions
|
|
@ -29,6 +29,7 @@ Authors:
|
|||
# include "Traits.h"
|
||||
|
||||
# include <list>
|
||||
# include <map>
|
||||
|
||||
/**
|
||||
* @file Utils.h
|
||||
|
|
@ -37,8 +38,6 @@ Authors:
|
|||
*
|
||||
* @todo encapsulate implementations.
|
||||
*
|
||||
* @todo provide more composite implementations (map<String, T>)
|
||||
*
|
||||
* Example
|
||||
*
|
||||
* @code
|
||||
|
|
@ -98,6 +97,12 @@ int main()
|
|||
vec.push_back(7);
|
||||
o["vec"] = eoserial::pack( vec );
|
||||
|
||||
std::map<std::string, int> 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<std::string, T> & 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<class T>
|
||||
inline Entity* pack( const std::map<std::string, T>& 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.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue