Re: Reference Counting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PeterAPIIT@gmail.com

    Re: Reference Counting

    These are difference template arguments:
    >
    template template arguments
    >
    and
    >
    template typename arguments
    >
    There is also template arguments that are just integer literals or pointers.

    What is template template arguments and template typename arguments ?

    The reason i write in policy based design is because this is the
    requirement of the assignment.

    My code so far:

    Code:
    #include "StoragePolicy.h"
    
    template <typename T,
    template <class>
    class StoragePolicy = DefaultSPStorage,
    
    template <class>
    class OwnershipPolicy = RCPtr,
    
    template <class>
    class ConversionPolicy = DisallowConversion[QUOTE]
    >[/QUOTE]
    
    class smart_ptr
    {
    T* operator->()
    {
    return aType;
    }
    
    T& operator*()
    {
    return *aType;
    }
    };
    
    
    
    template <class aType>
    class DefaultSPStorage
    {
    private:
    
    /*
    SmartPtr passes StoragePolicy<T>::T*
    to Ownership- Policy.
    */
    aType * pImpl;
    
    public:
    DefaultSPStorage () : pImpl(new aType())
    {
    }
    
    explicit DefaultSPStorage<aType>(aType* pointee)
    {
    if (!pointee)
    {
    throw NullPointerException();
    }
    else
    {
    pImpl = pointee;
    }
    }
    
    
    ~DefaultSPStorage<aType>()
    {
    delete pImpl;
    }
    
    
    aType* operator->()
    {
    if (!pointee)
    {
    throw NullPointerException();
    }
    else
    {
    return pIpml;
    }
    }
    
    aType& operator*()
    {
    if (!pointee)
    {
    throw NullPointerException();
    }
    else
    {
    return *pImpl;
    }
    }
    
    aType* GetImpl(const DefaultSPStorage & that)
    {
    return that.pImpl;
    }
    
    aType& GetImplRef(const DefaultSPStorage & that)
    {
    return that.pImpl;
    }
    };
    
    
    template <class sameType>
    class RCPtr
    {
    
    private:
    unsigned int* referenceCountPtr;
    
    public:
    
    RCPtr<sameType>() : refereceCountPtr(new unsigned int(1))
    {
    }
    
    RCPtr<sameType>(const RCPtr & that)
    {
    unsigned int *temp = new unsigned int(1);
    
    temp = that.referenceCountPtr;
    referenceCountPtr = temp;
    }
    
    ~RCPtr<sameType>()
    {
    delete referenceCountPtr;
    }
    
    
    void IncreaseRef()
    {
    *referenceCountPtr++;
    }
    
    void DecreaseRef()
    {
    *referenceCountPtr--;
    }
    
    /*
    IncreaseRef
    DecreaseRef
    
    Realease
    
    */
    
    
    /*	The Ownership policy must support
    intrusive as well as nonintrusive
    reference counting.
    
    Therefore, deallocation of memory is
    called by function rather than
    implicitly destructor called because
    it can explicit called by user
    */
    };
    
    int main(int argc, char *argv[])
    {
    /*	typedef smart_ptr<int,
    DefaultSPStorage, RCPtraSmtPtr;
    */
    smart_ptr<number, DefaultSPStorage, RCPtraSmtPtr;
    
    return 0;
    }

    I really need you all help.

    Please help me.

    A billion thanks in advance for your help.
  • Kai-Uwe Bux

    #2
    Re: Reference Counting

    PeterAPIIT@gmai l.com wrote:
    >
    >These are difference template arguments:
    >>
    > template template arguments
    >>
    >and
    >>
    > template typename arguments
    >>
    >There is also template arguments that are just integer literals or
    >pointers.
    >
    >
    What is template template arguments and template typename arguments ?
    What about reading a little?

    The reason i write in policy based design is because this is the
    requirement of the assignment.
    Ok.

    My code so far:
    >
    [code]
    #include "StoragePolicy. h"
    >
    template <typename T,
    template <class>
    class StoragePolicy = DefaultSPStorag e,
    >
    template <class>
    class OwnershipPolicy = RCPtr,
    >
    template <class>
    class ConversionPolic y = DisallowConvers ion
    Way too complicated. Start with a smart pointer that only has different
    ownership policies, e.g., a reference counted ownership and a deep copy
    policy.
    class smart_ptr
    {
    T* operator->()
    {
    return aType;
    }
    Not good. The operator->() is a typical example of an operator that you will
    want a policy to provide. There should be no hard-coded aType in your smart
    pointer.
    >
    T& operator*()
    {
    return *aType;
    }
    };
    operator* on the other hand, can be defined in terms of operator->() quite
    generically as:

    reference operator* ( void ) const {
    return( *( this->operator->() ) );
    }


    Also: how is smart_ptr supposed to take advantage of all these policies?


    [snip lots of "code"]

    Maybe, you are in need of an example showing how policies actually can be
    used to (a) reuse code that is common and (b) supply the code that differs.
    Here is a simple smart pointer with two different ownership models:


    #include <algorithm>
    #include <functional>
    using std::swap;


    template < typename T >
    class refcounted_poli cy {

    typedef T value_type;
    typedef value_type * pointer;
    typedef value_type const * const_pointer;

    struct count {
    pointer the_ptr;
    unsigned long ref_count;

    count ( pointer ptr )
    : the_ptr ( ptr )
    , ref_count ( 1 )
    {}

    ~count ( void ) {
    delete ( the_ptr );
    }

    };

    count * count_ptr;

    public:

    friend
    void swap ( refcounted_poli cy & lhs, refcounted_poli cy rhs ) {
    swap( lhs.count_ptr, rhs.count_ptr );
    }

    refcounted_poli cy ( pointer ptr )
    : count_ptr( new count ( ptr ) )
    {}

    refcounted_poli cy ( refcounted_poli cy const & other )
    : count_ptr ( other.count_ptr )
    {
    ++ count_ptr->ref_count;
    }

    ~refcounted_pol icy ( void ) {
    -- count_ptr->ref_count;
    if ( count_ptr->ref_count == 0 ) {
    delete( count_ptr );
    }
    }

    refcounted_poli cy & operator= ( refcounted_poli cy other ) {
    swap ( *this, other );
    return( *this );
    }

    const_pointer operator-( void ) const {
    return( count_ptr->t_ptr );
    }

    pointer operator-( void ) {
    return( count_ptr->t_ptr );
    }

    };


    template < typename T >
    class deepcopy_policy {

    typedef T value_type;
    typedef value_type * pointer;
    typedef value_type const * const_pointer;

    pointer the_ptr;

    public:

    friend
    void swap ( deepcopy_policy & lhs, deepcopy_policy rhs ) {
    swap( lhs.the_ptr, rhs.the_ptr );
    }

    deepcopy_policy ( pointer ptr )
    : the_ptr( ptr )
    {}

    deepcopy_policy ( deepcopy_policy const & other )
    : the_ptr ( other.the_ptr ? new value_type ( *other.the_ptr ) : 0 )
    {}

    ~deepcopy_polic y ( void ) {
    delete ( the_ptr );
    }

    deepcopy_policy & operator= ( deepcopy_policy other ) {
    swap ( *this, other );
    return( *this );
    }

    const_pointer operator-( void ) const {
    return( the_ptr );
    }

    pointer operator-( void ) {
    return( the_ptr );
    }

    };




    template < typename T,
    template <classclass ownership_polic y >
    class smart_ptr :
    public ownership_polic y< T >
    {

    typedef ownership_polic y< T ownership;

    public:

    typedef T value_type;
    typedef value_type * pointer;
    typedef value_type const * const_pointer;
    typedef value_type & reference;
    typedef value_type const & const_reference ;

    smart_ptr ( pointer ptr = 0 )
    : ownership ( ptr )
    {}

    const_reference operator* ( void ) const {
    return( *( this->operator->() ) );
    }

    reference operator* ( void ) {
    return( *( this->operator->() ) );
    }

    friend
    bool operator== ( smart_ptr const & lhs, smart_ptr const & rhs ) {
    return ( lhs.operator->() == rhs.operator->() );
    }

    friend
    bool operator!= ( smart_ptr const & lhs, smart_ptr const & rhs ) {
    return ( lhs.operator->() != rhs.operator->() );
    }

    friend
    bool operator< ( smart_ptr const & lhs, smart_ptr const & rhs ) {
    return ( std::less<point er>
    ( lhs.operator->(), rhs.operator->() ) );
    }

    friend
    bool operator<= ( smart_ptr const & lhs, smart_ptr const & rhs ) {
    return ( std::less_equal <pointer>
    ( lhs.operator->(), rhs.operator->() ) );
    }


    friend
    bool operator( smart_ptr const & lhs, smart_ptr const & rhs ) {
    return ( std::greater<po inter>
    ( lhs.operator->(), rhs.operator->() ) );
    }

    friend
    bool operator>= ( smart_ptr const & lhs, smart_ptr const & rhs ) {
    return ( std::greater_eq ual<pointer>
    ( lhs.operator->(), rhs.operator->() ) );
    }

    };




    #include <iostream>

    struct Base {

    Base ( void ) {
    std::cout << "base is born.\n";
    }

    Base ( Base const & other ) {
    std::cout << "base is copied.\n";
    }

    virtual ~Base () {
    std::cout << "base dies.\n";
    }

    friend
    std::ostream & operator<< ( std::ostream & ostr,
    Base const & obj ) {
    return( ostr << "base" );
    }

    };


    int main ( void ) {
    {
    smart_ptr< Base, deepcopy_policy a_ptr ( new Base() );
    smart_ptr< Base, deepcopy_policy b_ptr ( a_ptr );
    smart_ptr< Base, deepcopy_policy c_ptr ( a_ptr );
    a_ptr = smart_ptr< Base, deepcopy_policy >();
    std::cout << "first handle deleted.\n";
    b_ptr = smart_ptr< Base, deepcopy_policy >();
    std::cout << "second handle deleted.\n";
    }
    std::cout << "------------------------\n";
    {
    smart_ptr< Base, refcounted_poli cy a_ptr ( new Base() );
    smart_ptr< Base, refcounted_poli cy b_ptr ( a_ptr );
    smart_ptr< Base, refcounted_poli cy c_ptr ( a_ptr );
    a_ptr = smart_ptr< Base, refcounted_poli cy >();
    std::cout << "first handle deleted.\n";
    b_ptr = smart_ptr< Base, refcounted_poli cy >();
    std::cout << "second handle deleted.\n";
    }
    }

    Note that derived classes are not handled correctly (true cloning is missing
    from the deepcopy policy). Also, there is no support for incomplete types.
    In short, many things that you would expect from a good smart pointer are
    missing.


    Best

    Kai-Uwe Bux

    Comment

    Working...