📄 managed_ptr.h
字号:
/*** XML::Tree** managed_ptr.h**** Copyright (C) 2001 Paul J. Lucas**** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#ifndef managed_ptr_H#define managed_ptr_H#include "HTML_Node.h"//*****************************************************************************//// SYNOPSIS// template< class T > class managed_ptr//// DESCRIPTION//// A managed_ptr is similar to the standard auto_ptr except that a// managed_ptr doesn't forget the pointer even when it loses management of// it. Additionally, the default is to start off unmanaged.////*****************************************************************************{public: managed_ptr( T*, bool manage = false ); managed_ptr( managed_ptr<T> const&, bool manage = false ); ~managed_ptr() { if ( managed_ ) delete p_; } managed_ptr<T>& operator=( managed_ptr<T> const& ); managed_ptr<T>& operator=( T* ); operator T*() const { return p_; } T* operator->() const { return p_; } T& operator* () const { return *p_; } void manage( bool b ) { managed_ = b; } bool managed() const { return managed_; } T* ptr() const { return p_; }private: T* p_; bool managed_;};////////// Inlines ////////////////////////////////////////////////////////////template< class T >inline managed_ptr<T>::managed_ptr( T *p, bool manage ) : p_( p ), managed_( manage ){ // do nothing else}template< class T >inline managed_ptr<T>::managed_ptr( managed_ptr<T> const &m, bool manage ) : p_( m.p_ ), managed_( manage ){ if ( managed_ ) m.managed_ = false;}template< class T >inline managed_ptr<T>& managed_ptr<T>::operator=( managed_ptr<T> const &m ) { if ( &m != this ) { if ( managed_ ) delete p_; p_ = m.p_; managed_ = false; } return *this;}template< class T>inline managed_ptr<T>& managed_ptr<T>::operator=( T *p ) { if ( managed_ ) delete p_; p_ = p; managed_ = false; return *this;}//*****************************************************************************//// SYNOPSIS// typedef managed_ptr< HTML_Tree::HTML_Node > managed_node;//// DESCRIPTION//// A managed_node is-a managed_ptr for HTML_Nodes. When a reference to a// Perl object goes out of scope, the object's reference count is// decremented. If it becomes zero, the object is deleted. However, when// a Perl object is tied to an underlying data structure that involves// links (such as a node in a tree) and the links are still attached, the// underlying object must not be deleted. Hence, a managed_node is used// to delete an underlying object only when it's appropriate to do so// (when it's been excised from the rest of the tree).////*****************************************************************************#endif /* managed_ptr_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -