Sign in to follow this  
undersan

Serialization and Plain Old Data types

Recommended Posts

Most of the data structures in my game are Plain Old Data types (PODs) or STL containers of PODs. So, I have this StreamWriter class (and similar StreamReader class) for serialization:
class StreamWriterC
{
public:
    virtual char* AllocForWrite( int iSize ) = 0;

    template <class T>
    void WritePOD( T ItemToWrite )
    {
        *(T*)AllocForWrite( sizeof(T) ) = ItemToWrite;
    }

    template <class T>
    void WriteSTLVector( std::vector<T>& SourceContainer )
    {
        int iContainerSize = SourceContainer.size();
        Write( iContainerSize );
        for( int iItem = 0; iItem < iContainerSize; iItem++ )
        {
            WritePOD( SourceContainer[iItem] );
        }
    }

    // plus WriteSTLString, WriteSTLList, etc.
}
For the occasional non-POD object, I can't use StreamWriter.WritePOD( Object ). Instead, I have to implement a class method, i.e., Object.WriteToStream( StreamWriter ). However, sometimes I forget and use WritePOD for a non-POD object. Such code will compile, run, and silently fail. This is bad. Any suggestions? Is there any way to get WritePOD to issue a warning (at compile time or run time) for non-POD objects? Some template trickery? FYI, I'm using .NET 2002.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you don't mind using boost.. there's the boost serialization library (http://www.boost.org/libs/serialization/doc/index.html). It does *exactly* what you're trying to do. and yea.. even to zip files ;) i think it would a very painless process to integrate it into your project!
that's enough from me for now, got to catch some sleep. i'll check back tomorrow

Share this post


Link to post
Share on other sites
From a quick glance at the boost::type_traits (http://www.boost.org) documentation the type_trait is_pod, might do what you want - but only MSVC8 supports the required compiler intrinsics to detect whether a class or struct is POD.

As an alternative, you may want to consider using boost::serialization to solve this problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this