- 浏览: 261657 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
daknife:
谢谢你的这篇文章,让我大概了解了select的一部分底层原理。 ...
Linux-2.6.25 select系统调用源码分析 -
gjlzjb:
非常有用,谢谢哈。另外问下,您是否用过Pheonix Syst ...
Why Map/Reduce? -
zhangyafei_kimi:
canbo 写道请问,我怎么生成安装包,提供给其它用户安装呢? ...
下载最新的Google Chrome源码并编译 -
canbo:
请问,我怎么生成安装包,提供给其它用户安装呢?
下载最新的Google Chrome源码并编译
boost.shared_ptr源码整理和使用说明
- 博客分类:
- C and C++
Source
#pragma once
//shared_ptr的简单实现版本
//基于引用记数的智能指针
//它可以和stl容器完美的配合
namespace kimi_boost
{
template<class T>
class shared_ptr
{
public:
typedef T element_type;
typedef T value_type;
typedef T * pointer;
typedef T& reference;
typedef unsigned long size_type;
explicit shared_ptr(T* p=0) : px(p)
{
try { pn = new size_type(1); }
catch (...) { delete(p); throw; }
}
shared_ptr& operator= (T* p)
{
if(this->px == p) return *this;
dispose();
try { pn = new size_type(1); }
catch (...) { delete(p); throw; }
px=p;
return *this;
}
shared_ptr(const shared_ptr& r) throw(): px(r.px)
{
++*r.pn;
pn = r.pn;
}
shared_ptr& operator= (const shared_ptr& r) throw()
{
if(this == &r) return *this;
dispose();
px = r.px;
++*r.pn;
pn = r.pn;
return *this;
}
template<typename Y> friend class shared_ptr;
//为了让有继续关系的shared_ptr类型赋值或构造
template<typename Y>
shared_ptr(const shared_ptr<Y>& r)
{
px = r.px;
++*r.pn;
pn = r.pn; // shared_count::op= doesn't throw
}
template<typename Y>
shared_ptr& operator= (const shared_ptr<Y>& r)
{
dispose();
px = r.px;
++*r.pn;
pn = r.pn; // shared_count::op= doesn't throw
return *this;
}
template<typename Y>
shared_ptr(Y* py)
{
try { pn = new size_type(1); }
catch (...) { delete(py); throw; }
px=py;
}
template<typename Y>
shared_ptr& operator= (Y* py)
{
if(this->px == py) return *this;
dispose();
try { pn = new size_type(1); }
catch (...) { delete(py); throw; }
px=py;
return *this;
}
~shared_ptr() { dispose(); }
void reset(T* p=0)
{
if ( px == p ) return;
if (--*pn == 0)
{ delete(px); }
else
{ // allocate new reference
// counter
// fix: prevent leak if new throws
try { pn = new size_type; }
catch (...) {
// undo effect of —*pn above to
// meet effects guarantee
++*pn;
delete(p);
throw;
} // catch
} // allocate new reference counter
*pn = 1;
px = p;
} // reset
reference operator*() const throw(){ return *px; }
pointer operator->() const throw(){ return px; }
pointer get() const throw(){ return px; }
size_type use_count() const throw()//
{ return *pn; }
bool unique() const throw()//
{ return *pn == 1; }
private:
void dispose() throw()
{
if (--*pn == 0)
{ delete px; delete pn; }
}
T * px; // contained pointer
size_type* pn; // reference counter
}; // shared_ptr
template<typename A,typename B>
inline bool operator==(shared_ptr<A> const & l, shared_ptr<B> const & r)
{
return l.get() == r.get();
}
template<typename A,typename B>
inline bool operator!=(shared_ptr<A> const & l, shared_ptr<B> const & r)
{
return l.get() != r.get();
}
}//namespace kimi_boost
Test code
class A
{
public:
A(int _a=0):a(_a){}
~A(){}
protected:
int a;
};
class B : public A
{
public:
B(int _a=0):A(_a){}
~B(){}
};
void kimi_shared_ptr_test()
{
using namespace std;
using kimi_boost::shared_ptr;
shared_ptr<int> sp;
shared_ptr<int> sp2(new int(2));
shared_ptr<int> sp3(sp2);
shared_ptr<int> sp4;
shared_ptr<int> sp5;
sp=sp2;
sp4=new int(3);
sp.reset(new int(10000));
vector< shared_ptr<int> > vsp;
vsp.push_back(sp);
vsp.push_back(sp2);
vsp.push_back(sp3);
vsp.push_back(sp4);
vsp.push_back(sp5);
shared_ptr<A> spa(new A(2));
shared_ptr<B> spb(new B(3));
spa = spb;
shared_ptr<A> spa2(new B(2345));
cout<<(sp2==sp3)<<endl;
cout<<(spa==spb)<<endl;
}
Output
1
1
发表评论
-
The Elements of Programing Style
2009-08-09 18:26 1235把代码写清楚,别耍小聪明。 想干什么,讲的简单点、直接点。 只 ... -
6个变态的C语言Hello World程序
2009-06-01 09:37 807转载自:http://cocre.com/?p=914 下面 ... -
在VS2005中使用IBM Purify的注意事项
2009-05-12 12:24 3929Rational Purify 使用及分析实例可以见这里htt ... -
boost.pool源码整理和使用说明
2007-07-22 13:49 270Source #ifndef __KIMI_BOOST_PO ... -
一个STL风格的动态二维数组
2007-07-22 18:05 1492#ifndef __KIMI_BOOST_ARRAY2#def ... -
boost.any源码整理和使用说明
2007-08-24 22:44 1994Source #include <algorithm& ... -
boost.array源码整理和使用说明
2007-08-24 22:45 1217Source #include <cstddef> ... -
boost.BOOST_STATIC_ASSERT源码整理和使用说明
2007-08-24 22:49 1307Source #include <boost/conf ... -
boost.lexical_cast源码整理和使用说明
2007-08-24 22:55 1478Source #include <cstddef> ... -
编译期判断类的继承性
2007-08-24 23:00 1069介绍一个雕虫小技:编译期判断类的继承性。具体来说就是类型U是否 ... -
boost.type_traits源码整理和使用说明(1)
2007-08-28 01:35 2028Introduction The Boost type-tr ... -
泛型快速排序
2007-08-28 03:20 903Source #ifndef kimi_quicksort ... -
C++ Meta Programming 和 Boost MPL(1)
2007-08-30 23:01 1559本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(2)
2007-08-30 23:02 1486本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(3)
2007-08-30 23:06 1459本系列全部转载自kuibyshev.bokee.com ... -
C++ Meta Programming 和 Boost MPL(4)
2007-08-30 23:07 1632本系列全部转载自kuibyshev.bokee.com ... -
泛型归并排序
2007-09-18 00:23 1182#define SENTINEL_CARD (-1) # ... -
泛型插入排序
2007-09-18 00:25 1172#pragma once #include <iter ... -
boost.tuple源码整理和使用说明
2007-10-07 23:13 1536Introduction A tuple (or n-tup ... -
才发现VC中也可以检测内存泄漏
2009-03-30 14:54 1340#include <stdio.h> ...
相关推荐
C++ 智能指针(shared_ptr/weak_ptr)源码 源码位置:gcc-6.1.0\gcc-6.1.0\libstdc++-v3\include\tr1 这里只单列shared_ptr.h文件用于分析
shared_ptr arm, use arm asm code
shared_ptr boost audio video AVDataPool
该文档是C++ auto_ptr源码,感兴趣的可以自行下载学习,方便大家学习参考。
这是一个演示智能指针shared_ptr的一个Demo。本代码所对应的博客地址为https://blog.csdn.net/wfh2015/article/details/80699378
同时,shared_ptr重载了"*"和"->"操作符以模仿原始指针的⾏为,并且提供了显⽰bool类型转换以判断指针的有效性。 shared_ptr<int> sp1(new int(100)); assert(sp1); *sp1=200; shared_ptr<string> sp2(new string(...
参考陈硕的多线程服务端编程>>,中的用shared_ptr实现copy-on-write技术,不过这里的线程采用的是c++11的线程库
本文基于shared_ptr的源代码,提取了shared_ptr的类图和对象图,然后分析了shared_ptr如何保证文档所宣称的线程安全性。本文的分析基于boost 1.52版本,编译器是VC 2010。 shared_ptr的线程安全性boost官方文档对...
四种智能指针的使用、机制和缺陷分析
linux程序 C语言ptr_struct_file.cbuff_type_test.c
shared_ptr基本用法 shared_ptr采用引用计数的方式管理所指向的对象。当有一个新的shared_ptr指向同一个对象时(复制shared_...一般我们使用make_shared来获得shared_ptr。 cout<<"test shared_ptr base usage:
std::shared_ptr大概总结有以下几点: (1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用的资源。 (2) 使用引用计数来标识是否有多余指针指向该资源。(注意,shart_ptr本身指针会占1个引用) (3) 在...
一个用起来比较简单的无线信号处理发送芯片,可直接与单片机的串口相连,发送信号,对单片机开发人员很有用途
介绍ptr6000无线模块的详细信息,并附有源程序,以及相关的设置定义等
为了⽀持这样的⽤法,shared_ptr提供了类似的转型函数 static_pointer_cast()、const_pointer_cast()、dynamic_pointer_cast(),它们与标准的转型操作符static_cast()、const_cast()和dynamic_cast()类似,但返回的...
C++中的智能指针首先出现在“准”标准库boost中。 随着使用的人越来越多,为了让开发人员更方便、更安全的使用动态内存,C++11也引入了智能指针来管理动态对象。...先来一段简单的代码,看看shared_ptr的简单使用: #
shared_ptr(智能指针)举例 程序使⽤动态内存出于以下三种原因之⼀ : 1、程序不知道⾃⼰需要多少对象; 2、程序不知道所需对象的准确类型; 3、程序需要在多个对象间共享数据 ⼀. 要确保⽤ new 动态分配的内存空间...
这个引⽤计数器就减1,如果此时发现引⽤计数器为0,那么说明它是管理这个 指针的最后⼀个 shared_ptr 了,于是我们释放指针指向的资源。 在底层实现中,这个引⽤计数器保存在某个内部类型⾥(这个类型中还包含了 ...