|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2006-04-17 18:00:47
Peter Dimov wrote:
> Eric Niebler wrote:
>
>> The implementation is quite trivial (see below).
>
> Seems pretty complicated to me. The "canonical" counted_base is (assuming
> namespace boost):
>
> class counted_base
> {
> private:
>
> mutable detail::atomic_count count_;
>
> protected:
>
> counted_base(): count_( 0 ) {}
> virtual ~counted_base() {}
Using CRTP (as in the code I posted) avoids the need for a virtual
destructor. I see no need to force the virtual on people.
> counted_base( counted_base const & ): count_( 0 ) {}
> counted_base& operator=( counted_base const & ) { return *this; }
Huh. I was thinking it was safer to make these guys noncopyable, but ok.
> public:
>
> inline friend void intrusive_ptr_add_ref( counted_base const * p )
> {
> ++p->count_;
> }
>
> inline friend void intrusive_ptr_release( counted_base const * p )
> {
> if( --p->count_ == 0 ) delete p;
> }
Tried that. Under certain circumstances, VC7.1 wasn't finding the friend
functions. Hence my counted_base_access hack, and the
intrusive_ptr_add_ref/release functions at namespace scope.
> long use_count() const { return count_; }
Sure.
> };
>
> Seeing so many variations of it is a pretty good indication that we need it
> in Boost. :-)
So you're going to add it? :-)
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk