|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2006-04-17 13:01:09
Andreas Huber wrote:
> Eric Niebler wrote:
> [snipped]
>
> FWIW, I rolled my own counted_base class for statechart. It can be found
> here:
> http://cvs.sourceforge.net/viewcvs.py/boost/boost/boost/statechart/detail/counted_base.hpp?view=markup
>
> I'd also be interested in intrusive_ptr providing a class along the
> lines of counted_base...
I've added something similar to your counted_base to xpressive, and it
works well. I think there is a clear need for this to be a standard part
of intrusive_ptr. A quick grep through the archives turns up several
requests for this functionality, including a recent request from Ulrich
Eckhardt.
The implementation is quite trivial (see below). Peter, can this (or
something like it) just be added to intrusive_ptr.hpp, or should we go
through a fast-track review? If you want to just add it, I can see what
I can do about tests and docs.
Open questions:
1) What about the name (counted_base)? Can we do better?
2) Should there be a way to get the actual ref-count, or is bool
unique() enough (like shared_ptr)?
<<< Begin Code >>
//////////////////////////////////////////////////////////////////////////////
// (c) Copyright Andreas Huber Doenni 2002-2005, Eric Niebler 2006
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006
#define BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006
#include <boost/assert.hpp>
#include <boost/noncopyable.hpp>
#include <boost/checked_delete.hpp>
#include <boost/detail/atomic_count.hpp>
namespace boost { namespace xpressive { namespace detail
{
template<typename Derived>
struct counted_base_access;
////////////////////////////////////////////////////////////////////
// counted_base
template<typename Derived>
struct counted_base
: private noncopyable
{
bool unique() const
{
return 1 == this->count_;
}
protected:
counted_base()
: count_(0)
{
}
private:
friend struct counted_base_access<Derived>;
mutable boost::detail::atomic_count count_;
};
////////////////////////////////////////////////////////////////////
// counted_base_access
template<typename Derived>
struct counted_base_access
{
static void add_ref(counted_base<Derived> const *that)
{
++that->count_;
}
static void release(counted_base<Derived> const *that)
{
BOOST_ASSERT(0 < that->count_);
if(0 == --that->count_)
{
boost::checked_delete(
static_cast<Derived const *>(that));
}
}
};
template<typename Derived>
inline void intrusive_ptr_add_ref(counted_base<Derived> const *that)
{
counted_base_access<Derived>::add_ref(that);
}
template<typename Derived>
inline void intrusive_ptr_release(counted_base<Derived> const *that)
{
counted_base_access<Derived>::release(that);
}
}}} // namespace boost::xpressive::detail
#endif
-- 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