📄 share.h
字号:
#ifndef _SHARE_H_2002_11_28_11_44_
#define _SHARE_H_2002_11_28_11_44_
/***********************************************************************************\
\***********************************************************************************/
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// -------------------------------------------------------------------------------------------
// base class for reference counted pointers
// -------------------------------------------------------------------------------------------
class CSharedPtrBase
{
public:
// a "const" reference pointer can alter these two items, because
// adding or removing a reference may alter the list, even though
// it doesn't alter the pointed-to object.
mutable const CSharedPtrBase* m_last;
mutable const CSharedPtrBase* m_next;
//
// void * m_data;
};
// -------------------------------------------------------------------------------------------
// template for type-safe usage of reference counted pointers
// -------------------------------------------------------------------------------------------
template <class type>
class CSharePtr : public CSharedPtrBase
{
public:
// constructors / destructors (more below in templated area)
CSharePtr();
explicit CSharePtr( type* data ); // construct from a newly allocated object
CSharePtr( std::auto_ptr< type > data );
~CSharePtr();
// explicitly clear all references to this object and delete it.
//void free();
type* get() const;
// Releases ownership of the object and if this is the last reference
// returns a std::auto_ptr<> to it, otherwise returns NULL.
std::auto_ptr< type > release();
// Returns true if this is the only CSharePtr to the object
bool unique() const;
CSharePtr& operator=( type* data ); // assign a newly allocated object
CSharePtr& operator=( std::auto_ptr< type > data );
type* operator->() const;
type& operator*() const;
bool operator!() const;
// templated constructors - must be inlined here, due to MSC limitations.
// construct pointer from another templated ref pointer, without typecast.
template <class arg_type>
CSharePtr( const CSharePtr<arg_type>& arg )
{
set( arg.get() );
add_link( arg );
}
CSharePtr( const CSharePtr& arg );
// same, but with a typecast.
template <class arg_type>
CSharePtr( const type*, const CSharePtr<arg_type>& arg )
{
set( static_cast<type*>( arg.get() ) );
add_link( &arg );
}
// assign and typecast a reference counted pointer
template <class arg_type>
void assign( const CSharePtr<arg_type>& arg )
{
assign( static_cast<arg_type*>( arg.get() ), arg );
}
// assign but don't typecast a reference counted pointer
template <class arg_type>
CSharePtr& operator=( const CSharePtr<arg_type>& arg )
{
assign( arg.get(), arg );
return *this;
}
CSharePtr& operator=( const CSharePtr& arg );
bool operator==( const type* right ) const
{
return get() == right;
}
bool operator!=( const type* right ) const
{
return get() != right;
}
protected:
void set( type * new_data );
void add_link( const CSharedPtrBase& arg ) const; // link to another list
void assign( type* data, const CSharedPtrBase& arg );
void construct( type* data ); // construct a new reference count for
// a newly-allocated object.
type * m_data;
};
// -------------------------------------------------------------------------------------------
// template for type-safe usage of reference counted pointers to arrays
// -------------------------------------------------------------------------------------------
template <class type>
class CSharedArray : public CSharedPtrBase
{
public:
// constructors / destructors (more below in templated area)
CSharedArray();
explicit CSharedArray( type* data ); // construct from a newly allocated array
CSharedArray( const CSharedArray& arg );
~CSharedArray();
//void free(); // explicitly clear all references
// to this object and delete it.
type* get() const;
CSharedArray& operator=( type* data ); // assign a newly allocated object
type & operator[]( ptrdiff_t index ) const;
bool operator!() const;
CSharedArray& operator=( const CSharedArray& arg );
bool operator==( const type* right ) const
{
return get() == right;
}
bool operator!=( const type* right ) const
{
return get() != right;
}
protected:
void set( type * new_data );
void add_link( const CSharedPtrBase& arg ) const; // link to another list
void assign( type* data, const CSharedPtrBase& arg );
void construct( type* data ); // construct a new reference count for
// a newly-allocated array.
type * m_data;
};
// -------------------------------------------------------
// inline implementations of functions for ref_ptr template
//---------------------------------------------------------
template <class type>
inline CSharePtr<type>::CSharePtr()
{
set( 0 );
}
template <class type>
inline CSharePtr<type>::CSharePtr( type* data )
{
construct( data );
}
template <class type>
inline CSharePtr<type>::CSharePtr( std::auto_ptr< type > data )
{
construct( data.release() );
}
template <class type>
inline CSharePtr< type >::CSharePtr( const CSharePtr& arg )
{
set( arg.get() );
add_link( arg );
}
// delete object if this is the last reference
template <class type>
inline CSharePtr<type>::~CSharePtr()
{
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 object
// -------------------------------------------------------
/*template <class type>
inline void CSharePtr<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 object
// -------------------------------------------------------
template <class type>
inline CSharePtr<type>& CSharePtr<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;
}
template <class type>
inline CSharePtr<type>& CSharePtr<type>::operator=( std::auto_ptr< type > data )
{
return operator=( data.release() );
}
// -----------------------------------------------------
// Returns the data pointer with an appropriate type cast
// -----------------------------------------------------
template <class type>
inline type* CSharePtr<type>::get() const
{
return m_data;
}
// -----------------------------------------------------
// Returns true if this is the only CSharePtr to the object
// -----------------------------------------------------
template < typename type >
inline bool CSharePtr< type >::unique() const
{
return m_last == this || get() == 0;
}
// -----------------------------------------------------
// Releases ownership of the object and if this is the last reference
// returns a std::auto_ptr to it, otherwise returns NULL.
// -----------------------------------------------------
template < typename type >
inline std::auto_ptr< type > CSharePtr< type >::release()
{
if ( get() == 0 )
return std::auto_ptr< type >();
if ( m_last == this )
{
std::auto_ptr< type > result( get() );
set( 0 );
return result;
}
m_next->m_last = m_last;
m_last->m_next = m_next;
set( 0 );
return std::auto_ptr< type >();
}
template <class type>
inline type* CSharePtr<type>::operator->() const
{
return get();
}
template <class type>
inline type& CSharePtr<type>::operator*() const
{
return *get();
}
template <class type>
inline bool CSharePtr<type>::operator!() const
{
return get() != 0;
}
// -----------------------------------------------------
// Sets a new data pointer
// -----------------------------------------------------
template < class type >
inline void CSharePtr< type >::set( type* new_data )
{
m_data = new_data;
}
// -----------------------------------------------------
// default copy assignment operator
// -----------------------------------------------------
template < class type >
inline CSharePtr< type >& CSharePtr< type >::operator=( const CSharePtr& arg )
{
assign( arg.get(), arg );
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -