Boost logo

Boost :

From: hicks (hicks_at_[hidden])
Date: 2002-05-14 12:40:29


1. change to weak_ptr.hpp to compile under Builder C++ 5
2. change to shared_counter.hpp to run under CodeGuard in Builder C++ 5
without error.
3. Proposal for addition to shared/weak pointer library.

Attn: Peter Dimov

---------------------------
changes to weak_ptr.hpp

typename detail::shared_ptr_traits<T>::reference operator* () const //
never throws
{
#ifdef __BORLANDC__
element_type * p = get();
#else
T * p = get();
#endif
BOOST_ASSERT(p != 0);
return *p;
}

T * operator-> () const // never throws
{
#ifdef __BORLANDC__
element_type * p = get();
#else
T * p = get();
#endif
BOOST_ASSERT(p != 0);
return p;
}

---------------------------
Also made changes to shared_count.hpp, not to compile, but to run under
Builder C++ 5
native debugger CodeGuard, without reporting error. It was confused by
empty deletor.
Maybe this could be added with a #define condition for Code Guard?

#if defined(__BORLANDC__) && defined (CODE_GUARD)
counted_base_impl(P p, D&, long initial_use_count, long initial_weak_count):
counted_base(initial_use_count, initial_weak_count), ptr(p)
#else
counted_base_impl(P p, D d, long initial_use_count, long
initial_weak_count):
counted_base(initial_use_count, initial_weak_count), ptr(p), del(d)
#endif
{
}

virtual void dispose() // nothrow
{
#if defined(__BORLANDC__) && defined (CODE_GUARD)
del()(ptr);
#else
del(ptr);
#endif
}

---------------------------
Proposal for addition to shared/weak pointer library.

"eigen_ptr" is embedded in a structure as a base class.
Note the constructor/copy/assign semantics

#include "boost\smart_ptr.hpp"
#include "boost\weak_ptr.hpp"

namespace detail
{
template <class T>
struct eigen_ptr_deletor
{
void operator()(T*& ptr) { ptr=0; }
};
}

template <class T>
class eigen_ptr
{
protected:
typedef boost::shared_ptr<T> own_shared_ptr_type;
own_shared_ptr_type sp;

// constructor passes pointer to derived class and empty destructor
// copy constructor makes new copy
// assign makes new copy

eigen_ptr() : sp((T*)this, detail::eigen_ptr_deletor<T>()) {}
void renew() { this->~eigen_ptr<T>(); new (this) eigen_ptr<T>(); }
eigen_ptr(const eigen_ptr<T>& s) { renew(); }
eigen_ptr<T>& operator = (const eigen_ptr<T>& s) { renew(); }
public:
typedef boost::weak_ptr<T> weak_ptr_type;
weak_ptr_type make_weak_ptr() { return sp; }

// for test/debug only , derived object should pass pointer to self
bool check_eigen_ptr(T* p) { return (sp.get()==p); }
};

Usage::
MyClass : ...., public eigen_ptr<MyClass>, ....
{
        ....
};

MyClass s;
boost::weak_ptr<MyClass> wp = s.make_weak_ptr();

(It's called "eigen_ptr" because self_ptrlooks like it could cause a lot of conflicts)



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