Boost logo

Boost :

From: Scott Woods (scottw_at_[hidden])
Date: 2005-08-21 17:23:41


Hi,

I've been using a small family of templates that simplify the
use of serialization. It's nothing tricky but it has a nice effect
on application-level code.

Rather than (from serialization tutorial);

int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");
    boost::archive::text_oarchive oa(ofs);

    // create class instance
    const gps_position g(35, 59, 24.567f);
    // write class instance to archive
    oa << g;
    // close archive
    ofs.close();

    // ... some time later restore the class instance to its orginal state
    // create and open an archive for input
    std::ifstream ifs("filename", std::ios::binary);
    boost::archive::text_iarchive ia(ifs);
    // read class state from archive
    gps_position newg;
    ia >> newg;
    // close archive
    ifs.close();
    return 0;
}

It could instead be expressed;

int main() {
     // create and open a character archive for output
     // create class instance
     // write class instance to archive
     // close archive

    persistent<gps_position>( "filename", gps_position( 35, 59, 24.567f ) );

    // ... some time later restore the class instance to its orginal state
    // create and open an archive for input
    // read class state from archive

    persistent<gps_position> g( "filename" );

    // do something with g

    return 0;
}

Basic technique goes like;

template<typename T>
struct persistent : public T
{
    typedef T persistent_type;

    std::string name;

    persistent( const std::string &s ) :
    name( s )
    {
        load();
    }

    persistent( const std::string &s, const persistent_type &initial ) :
    persistent_type( initial ),
    name( s )
    {
        save();
    }

    virtual
    ~persistent()
    {
        save();
    }

    bool
    load()
    {
        std::ifstream ifs(name.c_str(), std::ios::binary);
        boost::archive::text_iarchive ia(ifs);
        ...
    }

    bool
    save()
    {
        std::ofstream ofs(name.c_str()); // Binary flag?
        boost::archive::text_oarchive oa(ofs);
        ...
    }
};

I haven't listed my own code as it is much more lengthy. It includes
finer control over the loading+saving automation (the implementation
above is simplistic) and adds to the set of methods available,
e.g. "blank" (delete the underlying file image). Its also non-boostified.

This is just syntactic sugar for serialization (that would be
caramelization?).
Its also seems to be a useful example of generic coding.

If anyone is interested in my header just mail me directly.

Cheers,
Scott

ps:
Some typos in serialization docs;

1. Strange grammar?
"Depending on this context, this might used implement object persistence"

2.Spelling
"are needed to associate a data item name with the correspoding xml tag"


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk