Update of /cvsroot/pclasses/pclasses2/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15296/test
Added Files:
s11nTest.cpp
Log Message:
egg
--- NEW FILE: s11nTest.cpp ---
#include <pclasses/s11n/s11n.h>
#include <pclasses/s11n/s11n_node.h>
#include <pclasses/s11n/s11n_debuggering_macros.h>
#include <pclasses/s11n/list.h>
#include <pclasses/s11n/map.h>
#include <pclasses/s11n/pods_streamable.h>
#include <pclasses/Util/LexT.h>
#include <memory> // auto_ptr
#include <cassert>
#include <list>
#include <map>
#define NODE_TYPE ::P::s11n::s11n_node
#define SERIALIZE(Node,SType,SObj) ::P::s11n::serialize< NODE_TYPE >( Node, SObj )
#define DESERIALIZE(Node,SType) ::P::s11n::deserialize< NODE_TYPE, SType >( Node )
#define TRAITS ::P::s11n::node_traits< NODE_TYPE >
struct TheBase
{
virtual ~TheBase() {}
virtual std::string classname() const = 0;
virtual bool serialize( NODE_TYPE & dest ) const
{
TRAITS::class_name( dest, this->classname() );
return true;
}
virtual bool deserialize( const NODE_TYPE & dest )
{
CERR << "Deserializing " << TRAITS::class_name( dest ) << "\n";
return true;
}
};
struct AType : public TheBase
{
AType()
{
CERR << "AType()\n";
}
virtual ~AType()
{
CERR << "~AType()\n";
}
virtual std::string classname() const { return "AType"; }
};
struct BType : public AType
{
BType()
{
CERR << "BType()\n";
}
virtual ~BType()
{
CERR << "~BType()\n";
}
virtual std::string classname() const { return "BType"; }
};
struct TheBase_s11n
{
bool operator()( NODE_TYPE & dest, const TheBase & src ) const
{
return src.serialize( dest );
}
bool operator()( const NODE_TYPE & src, TheBase & dest ) const
{
return dest.deserialize( src );
}
};
#define PS11N_TYPE TheBase
#define PS11N_TYPE_IS_ABSTRACT
#define PS11N_TYPE_NAME "TheBase"
#define PS11N_SERIALIZE_FUNCTOR TheBase_s11n
#include <pclasses/s11n/reg_serializable_traits.h>
#define PS11N_TYPE AType
#define PS11N_TYPE_INTERFACE TheBase
#define PS11N_TYPE_NAME "AType"
#include <pclasses/s11n/reg_serializable_traits.h>
#define PS11N_TYPE BType
#define PS11N_TYPE_INTERFACE TheBase
#define PS11N_TYPE_NAME "BType"
#include <pclasses/s11n/reg_serializable_traits.h>
/// i wish i could make this a P::App...
int main( int argc, char ** argv )
{
CERR << "P::s11n tests...\n";
typedef std::auto_ptr<TheBase> BAP;
BAP b1 = BAP( new BType );
NODE_TYPE node;
assert( SERIALIZE(node,TheBase,b1.get() ) );
CERR << "Serialize Workie!\n";
BAP b2 = BAP( DESERIALIZE(node,TheBase) );
assert( b2.get() && "Deserialize failed :(" );
CERR << "Deser workie!\n";
TRAITS::clear(node);
using ::P::Util::LexT;
typedef std::list<LexT> ListT;
typedef std::map<int,LexT> MapT;
ListT list;
MapT map;
LexT tmpval;
for( int i = 0; i < 10; i++ )
{
tmpval = std::string("this is item #") + LexT(i).str();
CERR << "Adding ["<<tmpval<<"]\n";
list.push_back( tmpval );
map[i] = tmpval;
}
CERR << "list/map sizes == " << list.size() << " , " << map.size()<<"\n";
bool worked;
CERR << "Containers...\n";
assert( ::P::s11n::serialize(node,list) );
list.clear();
worked = ::P::s11n::deserialize( node, list );
assert( worked && "deser list failed :(" );
node.clear();
assert( ::P::s11n::serialize(node,map) );
map.clear();
worked = ::P::s11n::deserialize( node, map );
assert( worked && "deser map failed :(" );
CERR << "Deserialized:\n";
CERR << "deser list.size() == " << list.size()<<"\n";
CERR << "map.size() == " << map.size()<<"\n";
// now prove it...
ListT::const_iterator lit = list.begin();
size_t at = 0;
while( lit != list.end() )
{
CERR << "list["<<at++<<"] = " << (*(lit++))<<"\n";
}
MapT::const_iterator mit = map.begin();
while( mit != map.end() )
{
CERR << "map["<<(*mit).first<<"] = "<<(*mit).second<<"\n";
++mit;
}
return 0;
}
|