/*
* Copyright (c) 1997-1999
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*/ #ifndef __SGI_STL_MEMORY
#define __SGI_STL_MEMORY #include <stl_algobase.h>
#include <stl_alloc.h>
#include <stl_construct.h>
#include <stl_tempbuf.h>
#include <stl_uninitialized.h>
#include <stl_raw_storage_iter.h> __STL_BEGIN_NAMESPACE #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
defined(__STL_MEMBER_TEMPLATES) template<class _Tp1> struct auto_ptr_ref {
_Tp1* _M_ptr;
auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
}; #endif template <class _Tp> class auto_ptr {
private:
_Tp* _M_ptr; // 维护一个类型指针 public:
typedef _Tp element_type; // 构造函数,初始化所维护的指针
explicit auto_ptr(_Tp* __p = ) __STL_NOTHROW : _M_ptr(__p) {} // 拷贝构造函数,转移控制权,将__a维护的指针转交给当前对象来维护;
auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {} #ifdef __STL_MEMBER_TEMPLATES
template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
: _M_ptr(__a.release()) {}
#endif /* __STL_MEMBER_TEMPLATES */ // 赋值构造函数,重新设定控制权
auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
if (&__a != this) {
delete _M_ptr; // 解除当前控制权,将所维护的指针所指向的对象析构掉。
_M_ptr = __a.release(); // 重新设定控制权,将__a维护的指针转交给当前对象来维护。
}
return *this;
} #ifdef __STL_MEMBER_TEMPLATES
template <class _Tp1>
auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
if (__a.get() != this->get()) {
delete _M_ptr;
_M_ptr = __a.release();
}
return *this;
}
#endif /* __STL_MEMBER_TEMPLATES */ // Note: The C++ standard says there is supposed to be an empty throw
// specification here, but omitting it is standard conforming. Its
// presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
// this is prohibited.
~auto_ptr() { delete _M_ptr; } // 析构函数,释放所维护指针指向的资源 _Tp& operator*() const __STL_NOTHROW {
return *_M_ptr; // 重载*操作符,返回对象引用
}
_Tp* operator->() const __STL_NOTHROW {
return _M_ptr; // 重载->操作符,返回对象地址
}
_Tp* get() const __STL_NOTHROW {
return _M_ptr; // 获取所维护指针
}
_Tp* release() __STL_NOTHROW {
_Tp* __tmp = _M_ptr;
_M_ptr = ; // 转移控制权,当前对象不再维护任何指针
return __tmp; // 让出控制权
}
// 重新设定控制权,如果以默认值调用,则释放对象,结束控制权
void reset(_Tp* __p = ) __STL_NOTHROW {
if (__p != _M_ptr) {
delete _M_ptr;
_M_ptr = __p;
}
} // According to the C++ standard, these conversions are required. Most
// present-day compilers, however, do not enforce that requirement---and,
// in fact, most present-day compilers do not support the language
// features that these conversions rely on. #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
defined(__STL_MEMBER_TEMPLATES) public:
auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
: _M_ptr(__ref._M_ptr) {} auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
if (__ref._M_ptr != this->get()) {
delete _M_ptr;
_M_ptr = __ref._M_ptr;
}
return *this;
} template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW
{ return auto_ptr_ref<_Tp1>(this->release()); }
template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
{ return auto_ptr<_Tp1>(this->release()); } #endif /* auto ptr conversions && member templates */
}; __STL_END_NAMESPACE #endif /* __SGI_STL_MEMORY */ // Local Variables:
// mode:C++
// End:

auto_ptr源码剖析的更多相关文章

  1. 《STL源码剖析》相关面试题总结

    原文链接:http://www.cnblogs.com/raichen/p/5817158.html 一.STL简介 STL提供六大组件,彼此可以组合套用: 容器容器就是各种数据结构,我就不多说,看看 ...

  2. 面试题总结(三)、《STL源码剖析》相关面试题总结

    声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...

  3. 智能指针分析及auto_ptr源码

    简介 C++没有内存自动回收机制,对堆内存的管理就是简单的new和delete,每次new出来的内存都需要手动delete释放.但由于忘记.流程复杂或者异常退出等,都有可能导致没有执行delete释放 ...

  4. jQuery之Deferred源码剖析

    一.前言 大约在夏季,我们谈过ES6的Promise(详见here),其实在ES6前jQuery早就有了Promise,也就是我们所知道的Deferred对象,宗旨当然也和ES6的Promise一样, ...

  5. Nodejs事件引擎libuv源码剖析之:高效线程池(threadpool)的实现

    声明:本文为原创博文,转载请注明出处. Nodejs编程是全异步的,这就意味着我们不必每次都阻塞等待该次操作的结果,而事件完成(就绪)时会主动回调通知我们.在网络编程中,一般都是基于Reactor线程 ...

  6. Apache Spark源码剖析

    Apache Spark源码剖析(全面系统介绍Spark源码,提供分析源码的实用技巧和合理的阅读顺序,充分了解Spark的设计思想和运行机理) 许鹏 著   ISBN 978-7-121-25420- ...

  7. 基于mybatis-generator-core 1.3.5项目的修订版以及源码剖析

    项目简单说明 mybatis-generator,是根据数据库表.字段反向生成实体类等代码文件.我在国庆时候,没事剖析了mybatis-generator-core源码,写了相当详细的中文注释,可以去 ...

  8. STL"源码"剖析-重点知识总结

    STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合 ...

  9. SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现

    SpringMVC完成初始化流程之后,就进入Servlet标准生命周期的第二个阶段,即“service”阶段.在“service”阶段中,每一次Http请求到来,容器都会启动一个请求线程,通过serv ...

随机推荐

  1. 原创:Equinox OSGi应用嵌入Jersey框架搭建REST服务

    一.环境 eclipse版本:eclipse-luna 4.4 jre版本:1.8 二.Equinox OSGi应用嵌入Jersey框架搭建REST服务 1.新建插件工程HelloWebOSGI a. ...

  2. Java类中中文问题

    一个奇怪问题 java类中要保存一个xml文件到数据库,2种传值方式其中1种不知何故会最终导致解析xml时报错. xml文件内容由StringBuffer定义,其中一段内容如下: sb.append( ...

  3. ThinkPHP3.2.3使用cli命令行模式

    使用tp3.2.3的cli模式时,报错.加上绝对路径还是报错.所以采用普通模式 if(version_compare(PHP_VERSION,'5.3.0','<')) die('require ...

  4. maven + eclipse + tomcat热部署

    环境:eclipse  + tomcat  + maven + m2eclipse 解决这种问题有两种方案(方案二更好用,不想看方案一的人可以直接绕过). 方案一: 第一种方案摘自http://blo ...

  5. CSS3--overflow属性

    overflow:当内容溢出元素框时发生的事情: overflow:默认,内容不会裁剪,会呈现在元素框之外: overflow:hidden:内容会被裁剪,并且其余部分是不可见的(清除浮动) over ...

  6. Ubuntu Server 配置网络

    ------------------ Ubuntu 14.04   x86_64 ----------------- 设置静态IP:vi /etc/network/interfaces. 然后再编辑 ...

  7. PL/SQL导出到execl中,数据前面的0发生丢失的解决办法

    ERR出现的场景再现: 使用 PL/SQL导出按钮,选择‘CSV文件’,保存为1.csv,后用execl打开,复制到VuGen中作为login脚本的参数化文件username. ERR及发现过程: 在 ...

  8. Guava----Function

    1. Function接口,提供两个方法: apply方法: 可以自定义自己想实现的功能 @Nullable T apply(@Nullable F input); 1. 实例: import com ...

  9. YY前端课程3

    1. 常用的字符实体(html实体):空格=      <=<       >=>       版权符号=© 2. ID就像身份证号一样,是唯一的,html页面的ID不能重复: ...

  10. Web APP开发技巧总结(转)

    一.META/LINK相关: 1.百度禁止转码 通过百度手机打开网页时,百度可能会对你的网页进行转码,往你页面贴上它的广告,非常之恶心.不过我们可以通过这个meta标签来禁止它: <meta h ...