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

📄 share.h

📁 网络泡泡被.net管理
💻 H
📖 第 1 页 / 共 2 页
字号:

// -----------------------------------------------------
// assign an existing reference-counted object to this one.
// -----------------------------------------------------
template <class type>
inline void CSharePtr<type>::assign( type* data, const CSharedPtrBase& arg )
{
	if (this == &arg)
		return;
	if (get() == 0)
	{
		set( data );
		add_link( arg );
	}
	else
	{
		// Unlink this pointer from its current list and remember if we were
		// the last pointer in the current list.
		bool delete_old = false;
		if ( m_last != this )
		{
			m_next->m_last = m_last;
			m_last->m_next = m_next;
		}
		else
			delete_old = true;

		type* old_data = get();

		set( data );
		add_link( arg );

		if ( delete_old )
			delete old_data;
	}
}

// -----------------------------------------------------
// link to other pointers 
// -----------------------------------------------------
template <class type>
inline void CSharePtr<type>::add_link( const CSharedPtrBase& arg ) const
{
	if (get() != 0)
	{
		m_next = &arg;
		m_last = arg.m_last;
		arg.m_last->m_next = this;
		arg.m_last = this;
	}
}

// -----------------------------------------------------
// construct a new reference count for a newly-allocated object.
// -----------------------------------------------------
template <class type>
inline void CSharePtr<type>::construct( type* data )
{
	set( data );
	m_last = this;
	m_next = this;
}

// -----------------------------------------------------
// equal / not equal operators
// -----------------------------------------------------
template <class type1>
inline bool operator==( const type1* left, const CSharePtr<type1>& right )
{
	return left == right.get();
}

template <class type1>
inline bool operator!=( const type1* left, const CSharePtr<type1>& right )
{
	return left != right.get();
}

template <class type1, class type2>
inline bool operator==( const CSharePtr<type1>& left, const type2* right )
{
	return left.get() == right;
}

template <class type1>
inline bool operator!=( const CSharePtr<type1>& left, const type1* right )
{
	return left.get() != right;
}

template <class type1, class type2>
inline bool operator==( const CSharePtr<type1>& left, const CSharePtr<type2>& right )
{
	return left.get() == right.get();
}

template <class type1, class type2>
inline bool operator!=( const CSharePtr<type1>& left, const CSharePtr<type2>& right )
{
	return left.get() != right.get();
}


// -------------------------------------------------------
// inline implementations of functions for ref_ptr template
//---------------------------------------------------------

template <class type> 
inline CSharedArray<type>::CSharedArray()
{
	set( 0 );
}

template <class type> 
inline CSharedArray<type>::CSharedArray( type* data )
{
	construct( data );
}

template <class type>
inline CSharedArray< type >::CSharedArray( const CSharedArray& arg )
{
	set( arg.get() );
	add_link( arg );
}

// delete array if this is the last reference
template <class type> 
inline CSharedArray<type>::~CSharedArray()
{
	if (get() != 0)
	{
		if (m_last == this)
		{
			// note that deleting m_data may delete the current object,
			// so it is important not to read or write any data members afterward.
			type* data = get();

			set( 0 );
			delete[] data;
		}
		else
		{
			// unlink this pointer from the list of referring pointers
			m_next->m_last = m_last;
			m_last->m_next = m_next;
			set( 0 );
		}
	}
}

// -------------------------------------------------------
// free all references to this array
// -------------------------------------------------------
/*template <class type> 
inline void CSharedArray<type>::free()
{
	// explicitly clear all references to this object and delete it.
	if (get() != 0)
	{
		// clear all other pointers in the list
		while (m_next != this)
		{
			m_next->m_data = 0;
			m_next = m_next->m_next;
		}
		// this operation may delete the current object.
		type* data = get();

		set( 0 );
		delete[] data;
	}
}
*/
// -------------------------------------------------------
// assign a newly allocated array
// -------------------------------------------------------
template <class type> 
inline CSharedArray<type>& CSharedArray<type>::operator=( type* data )
{
	if (get() == 0)
	{
		construct( data );
	}
	else
	{
		CSharedPtrBase const* last = m_last;
		CSharedPtrBase const* next = m_next;
		type*                      old_data = get();

		construct( data );

		if (last == this)
		{
			// deleting old_data may delete the current object,
			// so it is important not to read or write any data members afterward.
			delete[] old_data;
		}
		else
		{
			// unlink this pointer from the list of referring pointers
			next->m_last = last;
			last->m_next = next;
		}
	}
	return *this;
}

// -----------------------------------------------------
// Returns the data pointer with an appropriate type cast
// -----------------------------------------------------
template <class type>
inline type* CSharedArray<type>::get() const
{
	return static_cast< type * >( m_data );
}

template < typename type >
inline type & CSharedArray< type >::operator[]( ptrdiff_t index ) const
{
	return get()[ index ];
}

template <class type>
inline bool CSharedArray<type>::operator!() const
{	
	return get() != 0; 
}

// -----------------------------------------------------
// Sets a new data pointer
// -----------------------------------------------------
template < class type >
inline void CSharedArray< type >::set( type* new_data )
{
	m_data = new_data;
}

// -----------------------------------------------------
// default copy assignment operator
// -----------------------------------------------------
template < class type >
inline CSharedArray< type >& CSharedArray< type >::operator=( const CSharedArray& arg )
{
	assign( arg.get(), arg );
	return *this;
}

// -----------------------------------------------------
// assign an existing reference-counted object to this one.
// -----------------------------------------------------
template <class type>
inline void CSharedArray<type>::assign( type* data, const CSharedPtrBase& arg )
{
	if (this == &arg)
		return;
	if (get() == 0)
	{
		set( data );
		add_link( arg );
	}
	else
	{
		// Unlink this pointer from its current list and remember if we were
		// the last pointer in the current list.
		bool delete_old = false;
		if ( m_last != this )
		{
			m_next->m_last = m_last;
			m_last->m_next = m_next;
		}
		else
			delete_old = true;

		type* old_data = get();

		set( data );
		add_link( arg );

		if ( delete_old )
			delete[] old_data;
	}
}

// -----------------------------------------------------
// link to other pointers 
// -----------------------------------------------------
template <class type>
inline void CSharedArray<type>::add_link( const CSharedPtrBase& arg ) const
{
	if (get() != 0)
	{
		m_next = &arg;
		m_last = arg.m_last;
		arg.m_last->m_next = this;
		arg.m_last = this;
	}
}

// -----------------------------------------------------
// construct a new reference count for a newly-allocated object.
// -----------------------------------------------------
template <class type>
inline void CSharedArray<type>::construct( type* data )
{
	set( data );
	m_last = this;
	m_next = this;
}

// -----------------------------------------------------
// equal / not equal operators
// -----------------------------------------------------
template <class type1>
inline bool operator==( const type1* left, const CSharedArray<type1>& right )
{
	return left == right.get();
}

template <class type1>
inline bool operator!=( const type1* left, const CSharedArray<type1>& right )
{
	return left != right.get();
}

template <class type1>
inline bool operator==( const CSharedArray<type1>& left, const type1* right )
{
	return left.get() == right;
}

template <class type1>
inline bool operator!=( const CSharedArray<type1>& left, const type1* right )
{
	return left.get() != right;
}

template <class type1, class type2>
inline bool operator==( const CSharedArray<type1>& left, const CSharedArray<type2>& right )
{
	return left.get() == right.get();
}

template <class type1, class type2>
inline bool operator!=( const CSharedArray<type1>& left, const CSharedArray<type2>& right )
{
	return left.get() != right.get();
}





#endif //_SHARE_H_2002_11_28_11_44_

⌨️ 快捷键说明

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