Boost logo

Boost Users :

From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2005-05-04 11:42:58


I find sometimes the journey is as entertaining as the destination, so
let me indulge with how I got to what I'm thinking...
So, I'm looking at some code. there is an object like FooManager,
that manages a list<Foo *>. It has functions like RemoveFoo(Foo *),
AddFoo(Foo *), etc. (Ah, so many thoughts - why not just "Remove",
instead of "RemoveFoo", since we know the context, why even wrap the
list at all (this one is easy - the FooManager does more than just
wrap a list), etc).
Anyhow, RemoveFoo(Foo *) linearly searches the list for the matching
Foo * (by pointer comparison).

1. So I think, hey, really, this should be a set, not a list. And by
looking at what else we do with the list, yeah, a set would be better.
2. And really, shouldn't it be a list of smart_ptr<Foo> or somesuch?
(probably intrusive_ptr, as Foo has its own ref-counting, but
whatever.)
3. And what about this pointer comparison?
4. etc. Just so much rewriting could be done. How do you decide where to stop!

So, about the pointer comparison:

Since an allocator can (and, surprisingly to some, often does) reuse
the same pointer that was previously allocated and freed, how do I
even know that the Foo * that should be removed, is really the "same"
Foo * that is in the list. (Well, in my case, if I assume decent
programming, and since the Foo's are ref-counted, no one should have a
pointer to a deleted Foo, but that is assuming decent programming).

So, my first thought is that each Foo should have a unique,
non-recycled ID (GUID, ever incrementing int/long/etc, or something).
Then I would know if I _really_ had the same Foo. And so I could do
that. But we are now actually at the point where I will probably skip
the whole idea and assume decent programming exists and there is no
real problem.

Then, I started wondering - I've had this problem before, thus where's
the general solution. And what would it be? And *should* it be (ie
should it exist)?

So that's my question. What would it be. Would it be an allocator
that included space for, and initialized, the ID, and then also
special pointer type, where the pointer comparison checked the ID? Is
it just dumb to try to be robust against bad programming (to this
extent)? Would the Object ID be useful beyond that - ie to
distinquish between:

1. these 2 objects are equal
and
2. these 2 objects are the same object (ie there is only 1 object, not 2)

Thoughts?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net