|
Boost : |
From: Borgerding, Mark A. (MarkAB_at_[hidden])
Date: 2000-03-15 16:30:47
> -----Original Message-----
> From: Miki Jovanovic [mailto:miki_at_[hidden]]
> Sent: Wednesday, March 15, 2000 4:02 PM
> To: boost_at_[hidden]
> Subject: [boost] Small linked_ptr improvements
>
>
>
> Hi all, hi Mark,
>
> I've been spending some serious time in the linked_ptr and I basically
> have two small suggestions:
>
> 1) dynamic_cast_from might behave little better is written as below.
> What I did not like with the previous version was that if the
> contained
> object destructor threw an exception, the value of the smart pointer
> would have beed NULL. Version below would actually do the assignment
> correctly, even if the exception was thrown. Also, I there should be a
> small performance improvement.
>
> template <class T2>
> element_type * dynamic_cast_from(const linked_ptr<T2> & other) {
> if (ptr != other.get()) {// also checks for assignment to self
> element_type *pToDelete = ptr;
> ptr = dynamic_cast<element_type*>(other.get());
> if (ptr) {
> if (leave_list(&other))
> delete pToDelete;
> }
> else if (leave_list(this))
> delete pToDelete;
> }
> return get();
> }
Nice catch. I can't see any downside to this. Do you want to change the
version in the vault, or shall I?
>
> 2) This second suggestion is way more contraversial. The idea is to
> improve slightly the performance of linked_ptr. How slightly? Very
> slightly. What I like in the shared_ptr is that the contained
> object is
> actually at the same address as the shared_ptr:
This was my original implementation I sent to the group on 1/28. I admit I
didn't consider the performance characteristics of having an offset from the
"this" pointer. My original hope was to be able to nicely integrate into
varargs C code that took pointers ,e.g. printf("%s",...) . But I realized
that this would probably not work since the the sizeof(linked_ptr<T>) is not
the same as sizeof(T*).
The main reason I moved the ptr down into the typed subclass was to avoid
the static_cast. I think it is more readable, better organized and safer
without the void*. The safety concern comes from relying on the subclass to
verify type safety. If a malicious or clueless user subclassed the
linked_base, they could introduce different types into the mix.
As you point out, the performance difference is very slight. I doubt it
would be noticeable even in benchmarks.
In short, I prefer keeping the ptr in the subclass.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk