看看怎么使用

// Scopers help you manage ownership of a pointer, helping you easily manage the
// a pointer within a scope, and automatically destroying the pointer at the
// end of a scope. There are two main classes you will use, which coorespond
// to the operators new/delete and new[]/delete[].
//
// Example usage (scoped_ptr):
// {
// scoped_ptr<Foo> foo(new Foo("wee"));
// } // foo goes out of scope, releasing the pointer with it.
//
// {
// scoped_ptr<Foo> foo; // No pointer managed.
// foo.reset(new Foo("wee")); // Now a pointer is managed.
// foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
// foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
// foo->Method(); // Foo::Method() called.
// foo.get()->Method(); // Foo::Method() called.
// SomeFunc(foo.release()); // SomeFunc takes owernship, foo no longer
// // manages a pointer.
// foo.reset(new Foo("wee4")); // foo manages a pointer again.
// foo.reset(); // Foo("wee4") destroyed, foo no longer
// // manages a pointer.
// } // foo wasn't managing a pointer, so nothing was destroyed.
//
// Example usage (scoped_array):
// {
// scoped_array<Foo> foo(new Foo[100]);
// foo.get()->Method(); // Foo::Method on the 0th element.
// foo[10].Method(); // Foo::Method on the 10th element.
// }

scoped_ptr顾名思义,就是离开作用域,就会自动析构。


这跟上一节的chromium之ref_counted什么区别。

class MyFoo : public base::RefCounted<MyFoo> {
}; MyFoo *foo = new MyFoo;
foo->AddRef(); foo->AddRef();

foo->Release(); 
foo->Release(); 
// foo has been delete

区别在于RefCounted必须调用Release才会自动析构,而且可以有多次引用,而scoped_ptr离开作用域就会自动析构


// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
// automatically deletes the pointer it holds (if any).
// That is, scoped_ptr<T> owns the T object that it points to.
// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
// Also like T*, scoped_ptr<T> is thread-compatible, and once you
// dereference it, you get the threadsafety guarantees of T.
//
// The size of a scoped_ptr is small:
// sizeof(scoped_ptr<C>) == sizeof(C*)
template <class C>
class scoped_ptr {
public: // The element type
typedef C element_type; // Constructor. Defaults to intializing with NULL.
// There is no way to create an uninitialized scoped_ptr.
// The input parameter must be allocated with new.
explicit scoped_ptr(C* p = NULL) : ptr_(p) { } // Destructor. If there is a C object, delete it.
// We don't need to test ptr_ == NULL because C++ does that for us.
~scoped_ptr() {
enum { type_must_be_complete = sizeof(C) };
delete ptr_;
} // Reset. Deletes the current owned object, if any.
// Then takes ownership of a new object, if given.
// this->reset(this->get()) works.
void reset(C* p = NULL) {
if (p != ptr_) {
enum { type_must_be_complete = sizeof(C) };
delete ptr_;
ptr_ = p;
}
} // Accessors to get the owned object.
// operator* and operator-> will assert() if there is no current object.
C& operator*() const {
assert(ptr_ != NULL);
return *ptr_;
}
C* operator->() const {
assert(ptr_ != NULL);
return ptr_;
}
C* get() const { return ptr_; } // Comparison operators.
// These return whether two scoped_ptr refer to the same object, not just to
// two different but equal objects.
bool operator==(C* p) const { return ptr_ == p; }
bool operator!=(C* p) const { return ptr_ != p; } // Swap two scoped pointers.
void swap(scoped_ptr& p2) {
C* tmp = ptr_;
ptr_ = p2.ptr_;
p2.ptr_ = tmp;
} // Release a pointer.
// The return value is the current pointer held by this object.
// If this object holds a NULL pointer, the return value is NULL.
// After this operation, this object will hold a NULL pointer,
// and will not own the object any more.
C* release() {
C* retVal = ptr_;
ptr_ = NULL;
return retVal;
} private:
C* ptr_; // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't
// make sense, and if C2 == C, it still doesn't make sense because you should
// never have the same object owned by two different scoped_ptrs.
template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; // Disallow evil constructors
scoped_ptr(const scoped_ptr&);
void operator=(const scoped_ptr&);
};

这里有一个方法release(),可以释放指针

Foo *ptr = nullptr;
{
scoped_ptr<Foo> foo(new Foo);
ptr = foo.release(); // ptr get obj;
}
assert(ptr != nullptr);

chromium之scoped_ptr的更多相关文章

  1. chromium之MessageLoop浅析

    对chromium的MessageLoop非常感兴趣,接下来会详细分析Windows平台的具体实现. 代码版本:chromium-4.0.210.0_p26329 先看一下依赖的文件 message_ ...

  2. 初识chromium thread的实现

    接触chromium已有一段时间,写点东西学习一下吧. 首先说一下用法,如何利用chromium封装好的thread类来开一个线程.在base里有一个封装该类的头文件thread.h,include它 ...

  3. 【Chromium中文文档】线程

    线程 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/Threading. ...

  4. 【Chromium中文文档】Profile架构(看看谷歌家的重构)

    进程模型 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/Profile_ ...

  5. 【Chromium中文文档】跨平台开发的约定与模式

    跨平台开发的约定与模式 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/C ...

  6. Chromium Graphics: GPUclient的原理和实现分析之间的同步机制-Part II

    摘要:Part I探析GPUclient之间的同步问题,以及Chromium的GL扩展同步点机制的基本原理.本文将源码的角度剖析同步点(SyncPoint)机制的实现方式. 同步点机制的实现主要涉及到 ...

  7. Chromium网页Layer Tree创建过程分析

    在Chromium中.WebKit会创建一个Graphics Layer Tree描写叙述网页.Graphics Layer Tree是和网页渲染相关的一个Tree. 网页渲染终于由Chromium的 ...

  8. Chromium硬件加速渲染的UI合成过程分析

    在Chromium中.Render端和WebGL端绘制出来的UI终于是通过Browser端显示在屏幕上的.换句话说.就是Browser端负责合成Render端和WebGL端的UI.这涉及到不同Open ...

  9. Chromium多线程模型设计和实现分析

    Chromium除了远近闻名的多进程架构之外,它的多线程模型也相当引人注目的.Chromium的多进程架构是为了解决网页的稳定性问题,而多线程模型则是为了解决网页的卡顿问题.为了达到这个目的,Chro ...

随机推荐

  1. filter() 方法创建一个新数组

    filter快速过滤创建一个新数组 var new_array = arr.filter(callback(element[, index[, array]])[, thisArg]) 参数节 cal ...

  2. endwith与startwith字符串方法匹配重写

    endwith与startwith字符串方法匹配重写 在js读取文件信息并判断文件的格式类型时出现问题,并找到解决方案,写下来与大家分享,共勉. ---DanlV 描述 本人在上传MP3格式文件时,需 ...

  3. EMSAscript

    1.javaScript 中const.var.let区别 const 定义的变量不可修改 而且必须初始化 =>解决闭包变量污染问题 var 定义的变量可以修改 如果不初始化则默认值为undef ...

  4. 重建二叉树(C++和Python实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 输入某二叉树前序遍历和中序遍历结果,请重建出该二叉树.假设输 ...

  5. sourcemaps and persistent modification in chrome

    在现代web开发中,往往我们会借助类似sass,less之类的预处理器来加快开发进度,但是随着项目的增大,你可能无法清楚明确地知道一个css rule到底是从哪个less/scss文件中编译出来的,这 ...

  6. C++ 构造转换函数和强制转换函数

    http://blog.csdn.net/chenyiming_1990/article/details/8862497 1.对于系统的预定义基本类型数据,C++提供了两种类型转换方式:隐式类型转换和 ...

  7. DevExpress之ChartControl用法

    DevExpress中的ChartControl顾名思义就是数据基于图表展示,其关键在于Series上的处理. using System; using System.Drawing; using De ...

  8. Python学习---Python下[字符串]的学习

    字符串[不可变] 重点方法: in count() center(50,'*') startswith('h') endwith('h') find('r') : 找不到返回 -1 index('r' ...

  9. 小程序——微信小程序初学踩过的坑

    微信小程序初学踩过的坑 一.前言     最近因为某些需要和个人兴趣打算开发一下微信小程序,经过在官方网站上的基本了解,我大体知道了微信小程序开发的大致过程,其实最本质的就是MVVM,借用了很多模式上 ...

  10. EJB 3.1 @Startup @Singleton sequence

    The annotation javax.ejb.Startup (@Startup) is used to mark an EJB so to make the EJB can be brought ...