ptr.h

来自「一个symbian 冒险游戏代码」· C头文件 代码 · 共 64 行

H
64
字号
#ifndef _LANG_PTR_H
#define _LANG_PTR_H


namespace lang
{


/**
 * Smart pointer to an object of class T.
 * T must implement addReference() and release().
 * @see Object
 * 
 * @ingroup lang
 */
template <class T> class Ptr
{
public:
	/** Null pointer. */
	Ptr()													{m_o = 0;}

	/** Releases reference to the object. */
	~Ptr()													{if ( m_o ) m_o->release();}

	/** Increments object reference count and stores the pointer to an object. */
	Ptr( const Ptr<T>& other )								{T* o = other.ptr(); if ( o ) o->addReference(); m_o = o;}

	/** Increments object reference count and stores the pointer to an object. */
	Ptr( T* other )											{if ( other ) other->addReference(); m_o = other;}

	/** 
	 * Releases old reference if any, increments other object reference 
	 * count and stores the new reference. 
	 */
	Ptr<T>& operator=( const Ptr<T>& other )				{T* o = other.ptr(); if ( o ) o->addReference(); if ( m_o ) m_o->release(); m_o = o; return *this;}

	/** Return reference to the object. */
	T&		operator*() const								{return *m_o;}

	/** Access member of the object. */
	T*		operator->() const								{return m_o;}

	/** Returns pointer to the object. */
	operator T*() const										{return m_o;}

	/** Returns pointer to the object. */
	T*		ptr() const										{return m_o;}

private:
	T* m_o;
};


} // lang


/** Smart pointer to an object which implements addReference() and release(). */
#define P( MYCLASS ) lang::Ptr< MYCLASS >


#endif // _LANG_PTR_H


⌨️ 快捷键说明

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