Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-04-17 23:39:53

I have uploaded,
which is an implementation of an intrusive reference-counted
smart pointer that was shamelessly copied from boost::shared_ptr.

I myself have a need to create smart pointers from 'this', and
I can afford to have an intrusive count. I didn't totally follow the
explanation of how to use weak_ptr to accomplish this, but
the idea of storing an extra pointer just to be able to create
shared_ptrs from 'this' doesn't sound like an elegant solution
to me. I also like the fact that the count does not have to be
separately allocated on the heap (perhaps a minor or insignificant
benefit, but at least it introduces no penalties).

I stripped out any support for multi-threading, as I have no real
experience with it, and would not be able to make the code
thread-safe. I also eliminated the custom deleter code, since
I didn't totally understand what it was for, or how best to support
it (it's probably trivial to add it back in). I took out the MSVC
compatibility hacks, because I don't use MSVC, and I wouldn't
know how to make my code work with it.

Obviously, I intend intrusive_ptr to be an alternative to shared_ptr
only where appropriate. I myself am an enthusiastic user of
shared_ptr, and will continue to be. However, there are
a few cases where I think intrusive_ptr is a more elegant
solution, which is why I wrote it.

I don't pretend that the code is of great quality, and the testing
was absolutely minimal (I'll include my test program below to
illustrate usage); but I would at least like comments on how to
make it better, or why it doesn't even need to exist. I would like
to have a policy-based smart pointer like Loki::SmartPtr; but
I would like to have something that I can use now, rather than
when Boost gets around to making these hard decisions. As
such, I am aware that this code would probably be obsoleted
in a short time. Even so, perhaps others with non-critical
projects could make use of it like I am.

I'm not really interested in having the code reviewed as a
submission. Rather, I would like to see some productive
dialog on what needs to be resolved in order to get a full
policy-based smart pointer in Boost. Hopefully, I can also
learn a thing or two about library construction on the way;
and if others find the code useful, even better.


Test program illustrating usage:
#include <iostream>
#include "intrusive_ptr.hpp"

class TItem : public virtual boost::ref_counted
    int x_;
    TItem(int x) : x_(x) { }
    virtual ~TItem(void) { };

int main(void)
    typedef boost::intrusive_ptr<TItem> PItem;
    typedef std::vector<PItem> Items;
    PItem p;
    TItem m(-1);
        Items v(10);
        for (int i = 0; i < 10; ++i)
            v[i] = PItem(new TItem(10 - i));
        p = v[5];
        m = *v[3];
        std::cout << "m: " << m.use_count() << std::endl;
        for (int i = 0; i < 10; ++i)
            std::cout << "v[" << i << "]: " << v[i]->x_
                << ", count: " << v[i].use_count() << std::endl;
    std::cout << "p: " << p->x_ << std::endl;

The interface is as close to boost::shared_ptr as possible.
I believe the only differences in usage are that you obviously
need to derive from the base ref_counted class, and that
you can construct intrusive_ptr's from raw pointers (like 'this')
without problems.

Boost list run by bdawes at, gregod at, cpdaniel at, john at