Boost源码学习---scoped_ptr.hpp
来源:互联网 发布:mac允许安装未知来源 编辑:程序博客网 时间:2024/05/16 06:37
scoped_ptr是一个智能指针,包装了指向堆上内存的指针。它对指针所有权加以限制,不能转让指针所有权,一旦scoped_ptr获取了指针的管理权,便不再释放,无法再从其取回来,就像scope意思一样,指针智能在作用域使用,不能转让出去。一旦离开scoped_ptr的作用域,将调用它的析构函数,释放指针,不用手动释放。下面是它的源代码:
#ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED#define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.// Copyright (c) 2001, 2002 Peter Dimov//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//// http://www.boost.org/libs/smart_ptr/scoped_ptr.htm//#include <boost/config.hpp>#include <boost/assert.hpp>#include <boost/checked_delete.hpp>#include <boost/smart_ptr/detail/sp_nullptr_t.hpp>#include <boost/detail/workaround.hpp>#ifndef BOOST_NO_AUTO_PTR# include <memory> // for std::auto_ptr#endifnamespace boost{// Debug hooks#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)void sp_scalar_constructor_hook(void * p);void sp_scalar_destructor_hook(void * p);#endif// scoped_ptr mimics a built-in pointer except that it guarantees deletion// of the object pointed to, either on destruction of the scoped_ptr or via// an explicit reset(). scoped_ptr is a simple solution for simple needs;// use shared_ptr or std::auto_ptr if your needs are more complex.template<class T> class scoped_ptr // noncopyable{private: T * px;//原生态指针//将复制构造函数和赋值操作符声明为私有,不允许在类外使用,这样就限制了其所有权的转让 scoped_ptr(scoped_ptr const &); scoped_ptr & operator=(scoped_ptr const &); typedef scoped_ptr<T> this_type;//重载为私有,不支持比较 void operator==( scoped_ptr const& ) const; void operator!=( scoped_ptr const& ) const;public: typedef T element_type;//显示构造函数,必须显示调用 explicit scoped_ptr( T * p = 0 ): px( p ) // never throws {#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook( px );#endif }#ifndef BOOST_NO_AUTO_PTR//参数为auto_ptr时的构造函数//p.release()设auto_ptr为NULL,返回其内部指针 explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_NOEXCEPT : px( p.release() ) {#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook( px );#endif }#endif//析构函数,析构对象,释放内存 ~scoped_ptr() // never throws {#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_destructor_hook( px );#endif boost::checked_delete( px ); } void reset(T * p = 0) // never throws { BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors //把p转换为scoped_prt,再调用swap交换 this_type(p).swap(*this); }//重载解引用,返回指针指向的对象 T & operator*() const // never throws { BOOST_ASSERT( px != 0 ); return *px; }//重载->,返回指针 T * operator->() const // never throws { BOOST_ASSERT( px != 0 ); return px; }//返回指针 T * get() const BOOST_NOEXCEPT { return px; }// implicit conversion to "bool"#include <boost/smart_ptr/detail/operator_bool.hpp>//交换两个指针 void swap(scoped_ptr & b) BOOST_NOEXCEPT { T * tmp = b.px; b.px = px; px = tmp; }};#if !defined( BOOST_NO_CXX11_NULLPTR )template<class T> inline bool operator==( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT{ return p.get() == 0;}template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT{ return p.get() == 0;}template<class T> inline bool operator!=( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT{ return p.get() != 0;}template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT{ return p.get() != 0;}#endiftemplate<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_NOEXCEPT{ a.swap(b);}// get_pointer(p) is a generic way to say p.get()template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_NOEXCEPT{ return p.get();}} // namespace boost#endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDEDscoped_ptr与auto_ptr不同的是:auto_ptr可以转让使用权,在转让的时候,上一个指针失去使用权。而scoped_ptr不允许转让使用权。
scoped_ptr和auto_ptr都不能放在容器,auto_ptr是因为它只允许一个auto_prt使用指针,而scoped_ptr是因为不允许转让使用权,不支持拷贝和复制,不符合容器对元素的要求。
0 0
- Boost源码学习---scoped_ptr.hpp
- Boost源码剖析--<boost/smart_ptr/scoped_ptr.hpp>
- Boost源码学习---scoped_array.hpp
- Boost源码学习---shared_ptr.hpp
- Boost源码学习---weak_ptr.hpp
- boost::scoped_ptr 源码分析
- boost:: scoped_ptr源码剖析
- scoped_ptr.hpp
- boost学习之-scoped_ptr
- boost学习scoped_ptr,shared_ptr
- mongodb 中 scoped_ptr.hpp:99: T* boost::scoped_ptr<T>::operator->() const [with T = mongo::AtomicWord
- Boost 学习(一) scoped_ptr
- Boost源码剖析--<boost/assert.hpp>
- Boost学习笔记----SmartPointer之Boost::scoped_ptr
- boost源码剖析1----内存管理scoped_ptr
- C++学习 boost学习之-scoped_ptr
- boost::scoped_ptr
- boost::scoped_ptr()
- OpenCV基础篇之图片叠加
- C#,ArcGIS Engine开发入门教程
- 第一天,向着黑马前进
- HDU - 4276 The Ghost Blows Light(spfa + 树上dp)
- fragment生命周期,总结很好。感谢博主
- Boost源码学习---scoped_ptr.hpp
- 【HDU】5047 Sawtooth 高精度
- oracle 全文检索实践
- ArcGIS API For Flex(设置地图空间范围)
- HashMap源代码分析
- 分布式搜索引擎Elasticsearch——安装部署
- poj3233 hdu 1588 (矩阵)
- 循环的角度求均值
- flex 给group填充背景