Boost logo

Boost :

From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2008-03-20 11:11:19


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 19 March 2008 17:41 pm, Peter Dimov wrote:
> Your _v3 patch looks pretty good. deleter_wrapper should probably be
> sp_deleter_wrapper. What is the purpose of owned()?

It indicates whether the object has had ownership passed to a shared_ptr yet.
I don't have a strong justification on why it needs to be public, maybe it
would be useful for debugging/diagnostic purposes. Internally, it's needed
to figure out if the lazy initialization of _internal_shared_this has been
performed yet, although it could be replaced by something that more directly
serves that purpose.

Attached is a test for the new functionality.

- --
Frank
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFH4n6d5vihyNWuA4URArUcAKDfpaUBKcfbyNU4pbu1P2IjDdbBGQCfZsHx
TYToCExKCckLEX6sKcgF6eo=
=YoeM
-----END PGP SIGNATURE-----

--Boundary-00=_d6n4Ha4ObaQQXSE
Content-Type: text/x-c++src; charset="iso-8859-1";
        name="esft_constructor_test.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
        filename="esft_constructor_test.cpp"

//
// esft_constructor_test.cpp
//
// A test for the new enable_shared_from_this support for calling
// shared_from_this from constructors (that is, prior to the
// object's ownership being passed to an external shared_ptr).
//
// Copyright (c) 2008 Frank Mori Hess
// Copyright (c) 2008 Peter Dimov
//
// 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)
//

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <memory>

class X: public boost::enable_shared_from_this< X >
{
private:

    int destroyed_;
    int deleted_;
    int expected_;

private:

    X( X const& );
    X& operator=( X const& );

public:

    static int instances;

public:

    explicit X( int expected, boost::shared_ptr<X> *early_px = 0 ): destroyed_( 0 ), deleted_( 0 ), expected_( expected )
    {
        ++instances;
        if( early_px ) *early_px = shared_from_this();
    }

    ~X()
    {
        BOOST_TEST( deleted_ == expected_ );
        BOOST_TEST( destroyed_ == 0 );
        ++destroyed_;
        --instances;
    }

    typedef void (*deleter_type)( X* );

    static void deleter( X * px )
    {
        ++px->deleted_;
    }

    static void deleter2( X * px )
    {
        ++px->deleted_;
        delete px;
    }
};

int X::instances = 0;

int main()
{
    BOOST_TEST( X::instances == 0 );

    {
        boost::shared_ptr<X> early_px;
        X* x = new X( 1, &early_px );
        BOOST_TEST( early_px.use_count() > 0 );
        BOOST_TEST( boost::get_deleter<X::deleter_type>(early_px) == 0 );
        boost::shared_ptr<X> px( x, &X::deleter2 );
        BOOST_TEST( early_px.use_count() == 2 && px.use_count() == 2 );
        px.reset();
        BOOST_TEST( early_px.use_count() == 1 );
        BOOST_TEST( X::instances == 1 );
        X::deleter_type *pd = boost::get_deleter<X::deleter_type>(early_px);
        BOOST_TEST(pd && *pd == &X::deleter2 );
    }

    BOOST_TEST( X::instances == 0 );

    {
        boost::shared_ptr<X> early_px;
        X* x = new X( 1, &early_px );
        boost::weak_ptr<X> early_weak_px = early_px;
        early_px.reset();
        BOOST_TEST( early_weak_px.expired() == false );
        boost::shared_ptr<X> px( x, &X::deleter2 );
        BOOST_TEST( px.use_count() == 1 );
        BOOST_TEST( X::instances == 1 );
        px.reset();
        BOOST_TEST( early_weak_px.expired() );
    }

    BOOST_TEST( X::instances == 0 );

    {
        boost::shared_ptr<X> early_px;
        X x( 1, &early_px );
        BOOST_TEST( early_px.use_count() > 0 );
        boost::shared_ptr<X> px( &x, &X::deleter );
        BOOST_TEST( early_px.use_count() == 2 && px.use_count() == 2 );
        early_px.reset();
        BOOST_TEST( px.use_count() == 1 );
        BOOST_TEST( X::instances == 1 );
    }

    BOOST_TEST( X::instances == 0 );

    {
        boost::weak_ptr<X> early_weak_px;
        {
            boost::shared_ptr<X> early_px;
            X x( 0, &early_px );
            boost::weak_ptr<X> early_weak_px = early_px;
            early_px.reset();
            BOOST_TEST( early_weak_px.expired() == false );
            BOOST_TEST( X::instances == 1 );
        }
        BOOST_TEST( early_weak_px.expired() );
    }

    BOOST_TEST( X::instances == 0 );

    return boost::report_errors();
}

--Boundary-00=_d6n4Ha4ObaQQXSE--


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