⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstl_pointer.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
字号:
/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_POINTER_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_POINTER_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "mstl_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
class smart_ptr
{
public:
    typedef  smart_ptr<T>  self;
    typedef  def_size_t    size_type;

    static const size_type unshareable = size_t_max;

private:
    struct refPtr
    {
        T* pdata;
        size_type ref;

        explicit refPtr( T* p ) : pdata(p), ref(1)  {}

        ~refPtr()  {  delete pdata;  }

    private:
        refPtr( const refPtr& rhs );
        refPtr& operator=( const refPtr& rhs );
    } *m_ptr;

public:
    explicit smart_ptr( T* p = NULL_POINTER ) : m_ptr( new refPtr(p) )  {}

    smart_ptr( const self& rhs )  {  create( rhs );  }

    smart_ptr& operator=( const self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( rhs );
            swap( temp );
        }
        return *this;
    }

    template< typename U >
    smart_ptr( const smart_ptr<U>& rhs )  {  create( rhs );  }

    template< typename U >
    smart_ptr& operator=( const smart_ptr<U>& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( rhs );
            swap( temp );
        }
        return *this;
    }

    ~smart_ptr()  {  release();  }

    bool operator==( const self& rhs ) const
        {  return ( m_ptr == rhs.m_ptr );  }
    bool operator!=( const self& rhs ) const
        {  return ( m_ptr != rhs.m_ptr );  }

    T& operator*()               {  clone(false);  return *(m_ptr->pdata);  }
    T* operator->()              {  clone(false);  return m_ptr->pdata;  }
    const T& operator*() const   {  return *(m_ptr->pdata);  }
    const T* operator->() const  {  return m_ptr->pdata;  }

    bool operator!() const  {  return !m_ptr;  }

    void reset( T* p = NULL_POINTER )
    {
        self temp( p );
        swap( temp );
    }

    void swap( self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            refPtr* temp = m_ptr;
            m_ptr = rhs.m_ptr;
            rhs.m_ptr = temp;
        }
    }

    void release()
    {
        if( m_ptr->ref == unshareable )
            delete m_ptr;
        else if( --(m_ptr->ref) < 1 )
            delete m_ptr;
        m_ptr = NULL_POINTER;
    }

protected:
    void clone( bool share = true )  //share为true表示可共享
    {
        if( (m_ptr->ref > 1) && (m_ptr->ref != unshareable) )
        {
            self temp( new T(*(m_ptr->pdata)) );
            swap( temp );
        }
        m_ptr->ref = share ? 1 : unshareable;
    }

    void create( const self& rhs )
    {
        if( rhs.m_ptr->ref != unshareable )
        {
            m_ptr = rhs.m_ptr;
            ++( m_ptr->ref );
        }
        else
            m_ptr = ( rhs.m_ptr->pdata )
                    ? new refPtr( new T(*(rhs.m_ptr->pdata)) ) : NULL_POINTER;
    }

    template< typename U >
    void create( const smart_ptr<U>& rhs )
    {
        if( rhs.m_ptr->ref != unshareable )
        {
            m_ptr = rhs.m_ptr;
            ++( m_ptr->ref );
        }
        else
            m_ptr = ( rhs.m_ptr->pdata )
                    ? new refPtr( new T(*(rhs.m_ptr->pdata)) ) : NULL_POINTER;
    }

};  //end smart_ptr

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
class auto_ptr
{
private:
    T* m_ptr;

public:
    typedef  auto_ptr<T>  self;

    explicit auto_ptr( T* p = NULL_POINTER ):m_ptr(p)  {}

    auto_ptr( const self& rhs )
    : m_ptr( (rhs.m_ptr) ? new T(*(rhs.m_ptr)) : NULL_POINTER )  {}

    auto_ptr& operator=( const self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( new T(*(rhs.m_ptr)) );
            swap( temp );
        }
        return *this;
    }

    template< typename U >
    auto_ptr( const auto_ptr<U>& rhs )
    : m_ptr( (rhs.m_ptr) ? new T(*(rhs.m_ptr)) : NULL_POINTER )  {}

    template< typename U >
    auto_ptr& operator=( const auto_ptr<U>& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( new T(*(rhs.m_ptr)) );
            swap( temp );
        }
        return *this;
    }

    ~auto_ptr()
    {
        if( m_ptr )
            delete m_ptr;
    }

    T& operator*()               {  return *m_ptr;  }
    T* operator->()              {  return m_ptr;  }
    const T& operator*() const   {  return *m_ptr;  }
    const T* operator->() const  {  return m_ptr;  }

    bool operator!() const  {  return !m_ptr;  }

    T* get() const  {  return m_ptr;  }

    void release()
    {
        if( m_ptr )
        {
            delete m_ptr;
            m_ptr = NULL_POINTER;
        }
    }

    void reset( T* p = NULL_POINTER )
    {
        if( m_ptr )
            delete m_ptr;
        m_ptr = p;
    }

    void swap( self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            T* temp = m_ptr;
            m_ptr = rhs.m_ptr;
            rhs.m_ptr = temp;
        }
    }

    bool operator==( const self& rhs ) const
        {  return ( m_ptr == rhs.m_ptr );  }
    bool operator!=( const self& rhs ) const
        {  return ( m_ptr != rhs.m_ptr );  }

};  //end auto_ptr

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template<typename T>
inline void swap( smart_ptr<T>& lhs, smart_ptr<T>& rhs )
{
    lhs.swap( rhs );
}

template<typename T>
inline void swap( auto_ptr<T>& lhs, auto_ptr<T>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -