Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2004-10-07 00:08:55


Peter Dimov wrote:
> This is a bit harder.
>
> class A
> {
> private:
>
> WORD m_wData;
>
> private:
>
> A();
>
> public:
>
> static shared_ptr<A> CreateA() { return shared_ptr<A>(new A); }
>
> friend shared_ptr<WORD> GetData( shared_ptr<A> this_ )
> {
> return shared_ptr<WORD>(
> &this_->m_wData,
> boost::bind( &shared_ptr<A>::reset, this_ )
> );
> }
> };
>
> int main()
> {
> shared_ptr<A> pa = A::CreateA();
> shared_ptr<WORD> pw = GetData(pa);
> pa.reset();
> std::cout << "*pw: " << *pw << '\n';
> }

I'm implementing a mixture of smart_ptr and boost.langbinding libraries.
It's in early stage and I don't know yet what it's best for. Candidate
number one is a transaction framework.
Pointers to objects are smart, of course. They always point to full
object and they store dynamic type of the object. Plus, smart pointers
hold a pointer to static type.
You can clone and swap pointee object. And you can extact a member from
it.

object_ptr<Y> py(new Y);
object_ptr<X> px = py.get_member(&Y::x);

// Works even though static types are unknown:
object_ptr<void> pvy(py);
object_ptr<void> pvx py.get_member(&Y::x);

// Other operations:
object_ptr<void> copy = pvx.clone();
copy.swap_object(pvx);

-- 
Alexander Nasonov

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