Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-04-18 12:57:59


Another thing I'd like to see is what I call a serializable smart pointer.
This is just a smart pointer that stores either a pointer or an int, so it's
like a variant. I store object IDs, which is what I would load into the
pointer on initialization, and then do a fixup after all relevant objects
are loaded. Right now, I do this in a wrapper I wrote, which may
be the preferred way to do it. But it seems to me a bit more elegant
to do it in the smart pointer itself.

I used boost::shared_ptr as the smart pointer type in my wrapper.
Since it has a c'tor, d'tor, etc., I couldn't put it in a union with an
int. So I borrowed ideas from Andrei's value-based variant type,
and created my own variant<int, shared_ptr> that forwards some
of shared_ptr's interface, but allows me to store an int as well. If
the smart pointer itself implemented the two types, it *could* store
the int (or long) in a union with the raw pointer. This would avoid
a lot of messiness in dealing with a value-based discriminated union.

If there is a better way to save and load pointers, I'd be glad to
hear it. If this kind of feature just doesn't belong in a smart pointer,
I'd like to know why (I'm easy to convince, I just need to see a
good explanation). If this is possible to implement as a policy with
Loki::SmartPtr, I'd like to see a basic sketch. From what I can tell,
it isn't, since it would require a bit more interface than SmartPtr has
(one would need a way to store and fetch two different types). I
know Jens Maurer has written a persistence library, but it doesn't
seem to address pointer persistence directly (it looks like it assumes
that the user will deal with pointers herself).

I also know there has been a lot of discussion on persistence and
serialization; but again, they seem to be discussions that get stalled,
with no concrete results that I can use now. Without tackling
generalized persistence (which I can deal with myself right now),
is it possible to at least get a serializable pointer down?

Dave


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