Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2003-10-13 13:48:01


Dave Harris wrote:

>OK. And I'll return the favour by looking at what is possible using your
>approach.

>I now think the difference is merely one of efficiency. With your API we
>can load a vector with something like:

> template <typename Archive, typename Collection, typename T>
> void load_collection( Archive &ar, Collection &c, unsigned int version ) {
          .. // clear and get size
> std::auto_ptr<T> pt;
> ar >> pt; // Uses pointer load.
> c.push_back( *pt );
        
> for (--size; size != 0; --size) {
> ar >> *pt;
> c.push_back( *pt );
> }
> }

The current one looks like:

     template <typename Archive, typename Collection, typename T>
     void load_collection( Archive &ar, Collection &c, unsigned int version ) {
          .. // clear and get size
         T t;
         for (--size; size != 0; --size) {
            ar >> t;
            c.push_back( t);
         }
     }

which essentially the same but it doesn't use the heap.

Both of the above fail to address the point that I'm concerned about - the
requirement that for non-default constructors overloads have to be written in
two places and have to be kept in sync. Now that I see this I see it as
a real problem and will attempt to address it. FWIW the solution will
have to be similar in approach to your original suggestion. This will
be complicated by the requirements to support name/value pairs,
common access to users private data, and a transparent, intuitive
and explainable API

I'm now really thinking along the lines of your orginal idea - factoring
out construction so that the above would something like be

// overridable
template<class Archive, class T>
T & construct(Archive & ar, T *t){
        // load values required for construction
        int i;
        float j;
        ar >> i;
        ar >> j;
        new (t) T(i, j); // in place constructor
}

template <typename Archive, typename Collection, typename T>
void load_collection( Archive &ar, Collection &c, unsigned int version ) {
        .. // clear and get size - return if size == 0
        char x[sizeof(T)];
        T * tptr = static_cast<T*>(x);
        do{
                construct(ar, tptr);
                ar >> *tptr;
                c.push_back( *tpr);
        }
        while(--size > 0);
}

while load(... becomes - pretty much as your orginal suggestion

template<class Archive, class T>
void load(Archvie &ar, T * & t, unsigned int file_version){
        t = NULL;
        std::auto_ptr<T> tx = new(sizeof(T));
        construct(ar, tx.get());
        ar >> *tx;
        t = tx.release();
}

So, as I said before, I am happy to concede you point as gracefull as I
am able to do..

Implementing this will create a wider ripple effect than I would hope - all the way
through the examples, manual and test. oh well.

Robert Ramey


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