Boost logo

Boost :

From: Marcin Kalicinski (kalita_at_[hidden])
Date: 2005-04-03 19:02:28


> Can you post live_ptr.hpp so that I can run the test and play with it a
> bit?

#ifndef LIVE_PTR_INCLUDED
#define LIVE_PTR_INCLUDED

#include <boost/assert.hpp>

template<class T> class live_ptr;

///////////////////////////////////////////////////////////////////////////
// Class live_ptr_target

template<class T>
class live_ptr_target
{

    friend live_ptr<T>;

public:

    live_ptr_target();
    live_ptr_target(const live_ptr_target<T> &);
    ~live_ptr_target();

    live_ptr_target<T> &operator =(const live_ptr_target<T> &);

private:

    mutable live_ptr<T> *_list;

};

///////////////////////////////////////////////////////////////////////////
// Class live_ptr

template<class T>
class live_ptr
{

    friend live_ptr_target<T>;

public:

    live_ptr();
    live_ptr(T *);
    live_ptr(const live_ptr<T> &);
    ~live_ptr();

    live_ptr<T> &operator =(T *);
    live_ptr<T> &operator =(const live_ptr<T> &);
    T *operator ->() const;
    T &operator *() const;
    T *get() const;

private:

    T *_ptr;
    mutable live_ptr<T> *_prev, *_next;

};

// Implementation (originally in a separate file)

///////////////////////////////////////////////////////////////////////////
// Class live_ptr_target

template<class T>
live_ptr_target<T>::live_ptr_target():
    _list(0)
{
}

template<class T>
live_ptr_target<T>::live_ptr_target(const live_ptr_target<T> &):
    _list(0)
{
}

template<class T>
live_ptr_target<T>::~live_ptr_target()
{
    while (_list)
    {
        _list->_ptr = 0;
        _list = _list->_next;
    }
}

template<class T>
live_ptr_target<T> &live_ptr_target<T>::operator =(const live_ptr_target<T>
&)
{
    return *this;
}

///////////////////////////////////////////////////////////////////////////
// Class live_ptr

template<class T>
live_ptr<T>::live_ptr():
    _ptr(0)
{
}

template<class T>
live_ptr<T>::live_ptr(T *ptr):
    _ptr(ptr)
{
    if (_ptr)
    {
        live_ptr_target<T> *target = _ptr;
        if (target->_list)
            target->_list->_prev = this;
        _prev = 0;
        _next = target->_list;
        target->_list = this;
    }
}

template<class T>
live_ptr<T>::live_ptr(const live_ptr<T> &ptr):
    _ptr(ptr.get())
{
    if (_ptr)
    {
        live_ptr_target<T> *target = _ptr;
        if (target->_list)
            target->_list->_prev = this;
        _prev = 0;
        _next = target->_list;
        target->_list = this;
    }
}

template<class T>
live_ptr<T>::~live_ptr()
{
    if (_ptr)
    {
        if (_prev)
            _prev->_next = _next;
        else
        {
            live_ptr_target<T> *target = _ptr;
            target->_list = _next;
        }
        if (_next)
            _next->_prev = _prev;
    }
}

template<class T>
live_ptr<T> &live_ptr<T>::operator =(T *ptr)
{
    if (_ptr == ptr) return *this;
    if (_ptr)
    {
        if (_prev)
            _prev->_next = _next;
        else
        {
            live_ptr_target<T> *target = _ptr;
            target->_list = _next;
        }
        if (_next)
            _next->_prev = _prev;
    }
    _ptr = ptr;
    if (_ptr)
    {
        live_ptr_target<T> *target = _ptr;
        if (target->_list)
            target->_list->_prev = this;
        _prev = 0;
        _next = target->_list;
        target->_list = this;
    }
    return *this;
}

template<class T>
live_ptr<T> &live_ptr<T>::operator =(const live_ptr<T> &ptr)
{
    return operator =(ptr.get());
}

template<class T>
T *live_ptr<T>::operator ->() const
{
    BOOST_ASSERT(_ptr);
    return _ptr;
}

template<class T>
T &live_ptr<T>::operator *() const
{
    BOOST_ASSERT(_ptr);
    return *_ptr;
}

template<class T>
T *live_ptr<T>::get() const
{
    return _ptr;
}

#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk