根据OSG中的ref_ptr和Reference简化的智能指针
main.cpp测试代码
#include "TestSmartPointer"
void fun()
{
SP<TestSmartPointer> sp1=new TestSmartPointer("A");
SP<TestSmartPointer> sp2=sp1;
sp1=sp2;
}
void main()
{
fun();
//system("pause");
}
TestSmartPointer头文件
#pragma once #include "Referenced"
#include "SmartPointer" #include <string> class TestSmartPointer : public Referenced
{
public:
inline TestSmartPointer(std::string name):Referenced(),_name(name) {}
protected: virtual ~TestSmartPointer() {} std::string _name;
};
Referenced计数器
#ifndef REFERENCED
#define REFERENCED 1 #include <iostream>
using namespace std; /** Base class from providing referencing counted objects.*/
class Referenced
{
public:
Referenced() { _refCount=; }
Referenced(const Referenced&) { _refCount=; }
inline Referenced& operator = (const Referenced&) { return *this; } /** Increment the reference count by one, indicating that
this object has another pointer which is referencing it.*/
inline void ref() const; /** Decrement the reference count by one, indicating that
a pointer to this object is referencing it. If the
reference count goes to zero, it is assumed that this object
is no longer referenced and is automatically deleted.*/
inline void unref() const; /** Decrement the reference count by one, indicating that
a pointer to this object is referencing it. However, do
not delete it, even if ref count goes to 0. Warning, unref_nodelete()
should only be called if the user knows exactly who will
be resonsible for, one should prefer unref() over unref_nodelete()
as the later can lead to memory leaks.*/
inline void unref_nodelete() const; /** Return the number pointers currently referencing this object. */
inline int referenceCount() const { return _refCount; } protected:
virtual ~Referenced()
{
if (_refCount>)
{
cout<<"Warning: deleting still referenced object "<<this<<" of type '"<<typeid(this).name()<<"'"<<std::endl;
cout<<"the final reference count was "<<_refCount<<", memory corruption possible."<<std::endl;
}
}
mutable int _refCount;
}; inline void Referenced::ref() const
{
++_refCount;
} inline void Referenced::unref() const
{
bool needDelete = false;
--_refCount;
needDelete = _refCount<=; if (needDelete)
{
delete this;
}
}
inline void Referenced::unref_nodelete() const
{
--_refCount;
} #endif
SmartPointer智能指针模板类
#ifndef SMART_POINTER
#define SMART_POINTER 1 /** Smart pointer for handling referenced counted objects.*/
template<class T>
class SP
{
public:
typedef T element_type; SP() :_ptr(0L) {}
SP(T* t):_ptr(t) { if (_ptr) _ptr->ref(); }
SP(const SP& rp):_ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
~SP() { if (_ptr) _ptr->unref(); _ptr=; } inline SP& operator = (const SP& rp)
{
if (_ptr==rp._ptr) return *this;
T* tmp_ptr = _ptr;
_ptr = rp._ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
return *this;
} inline SP& operator = (T* ptr)
{
if (_ptr==ptr) return *this;
T* tmp_ptr = _ptr;
_ptr = ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
return *this;
} // comparison operators for SP.
inline bool operator == (const SP& rp) const { return (_ptr==rp._ptr); }
inline bool operator != (const SP& rp) const { return (_ptr!=rp._ptr); }
inline bool operator < (const SP& rp) const { return (_ptr<rp._ptr); }
inline bool operator > (const SP& rp) const { return (_ptr>rp._ptr); } // comparison operator for const T*.
inline bool operator == (const T* ptr) const { return (_ptr==ptr); }
inline bool operator != (const T* ptr) const { return (_ptr!=ptr); }
inline bool operator < (const T* ptr) const { return (_ptr<ptr); }
inline bool operator > (const T* ptr) const { return (_ptr>ptr); } inline T& operator*() { return *_ptr; } inline const T& operator*() const { return *_ptr; } inline T* operator->() { return _ptr; } inline const T* operator->() const { return _ptr; } inline bool operator!() const { return _ptr==0L; } inline bool valid() const { return _ptr!=0L; } inline T* get() { return _ptr; } inline const T* get() const { return _ptr; } /** take control over the object pointed to by SP, unreference but do not delete even if ref count goes to 0,
* return the pointer to the object.
* Note, do not use this unless you are 100% sure your code handles the deletion of the object correctly, and
* only use when absolutely required.*/
inline T* take() { return release();} inline T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=; return tmp;} private:
T* _ptr;
}; #endif
根据OSG中的ref_ptr和Reference简化的智能指针的更多相关文章
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
- 不可不表的OSG智能指针之强指针与弱指针 《转载》
不可不表的OSG智能指针之强指针与弱指针 <转载> 使用OSG的人都知道OSG的内存管理方式采用了智能指针,通过智能指针的方式让OSG自己处理对象的销毁工作.在OSG中有两个智能指针类型, ...
- C++中智能指针的设计和使用
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/7561235 智能指针(smart pointer)是存储指向动态分配(堆 ...
- 智能指针 与 oc中的指针
智能指针 与 oc中的指针 智能指针的原理及实现 当类中有指针成员时,一般有两种方式来管理指针成员:一是采用值型的方式管理,每个类对象都保留一份指针指向的对象的拷贝:另一种更优雅的方式是使用智能指针 ...
- OSG中的HUD
OSG中的HUD 所谓HUD节点,说白了就是无论三维场景中的内容怎么改变,它都能在屏幕上固定位置显示的节点. 实现要点: 关闭光照,不受场景光照影响,所有内容以同一亮度显示 关闭深度测试 调整渲染顺序 ...
- osg中使用MatrixTransform来实现模型的平移/旋转/缩放
osg中使用MatrixTransform来实现模型的平移/旋转/缩放 转自:http://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html# ...
- OSG中的示例程序简介
OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...
- OSG中的示例程序简介(转)
OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...
- OSG中相机参数的更改
#pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...
随机推荐
- Stm32 debug停留在"BKPT 0xAB"或者"SWI 0xAB"的解决办法。
一..背景: 曾经在工作中接触过STM32一段时间,但没有深入的去学习,只是用前辈搭建好的模型来实现一些功能罢了,俗话说的好,大树底下好乘凉,开发确实轻松了,可是不深究点,又觉着心里不踏实,然而也一直 ...
- Inno Setup制作应用程序安装包
我最近写了一个MFC应用程序,想发给其他的小伙伴玩一玩,直接发了个exe文件过去,结果发现小伙伴那边打不开.原来这个exe文件虽然是MFC静态编译的,但是还依赖了其他几个.dll文件,需要把这几个dl ...
- VS2013 Community配置OpenCV3.0.0
配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...
- 怎样用Lodrunner测试WAP站点的性能(两种解决方案)
其实用IE就可以的!!!! 1.借助opera实现对WAP站点的录制 第一:安装opera软件 第二:Lodrunner选择Web(HTTP/HTML)协议 第三:Lodrunner的Applicat ...
- Linux文件处理命令
1.权限处理 1.1 方法一 使用+-=的方法 1.1.1权限 rwx r 读 w 写 x 执行 1.1.2用户 ugoa u 所有者 g 用户组 o 其他人 a 表示以上所有 修改文件的方法 例: ...
- servlet之filter过滤器
1.Servlet 过滤器有以下目的 在客户端的请求访问后端资源之前,拦截这些请求. 在服务器的响应发送回客户端之前,处理这些响应. 2.Filter接口 1.每一个过滤器都需直接或间接继承Filte ...
- php正则
PHP代码 $str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="li ...
- Mac os壁纸提取
想必用过Mac os系统的朋友都知道mac壁纸,提取出来用作壁纸是十分好看的 下面给出壁纸的位置,自己用文件管理器打开然后复制出来即可! 系统盘:/Library/DesktopPictures
- javascript高级程序设计---NodeList和HTMLCollection
节点对象都是单个节点,但是有时会需要一种数据结构,能够容纳多个节点.DOM提供两种接口,用于部署这种节点的集合分别是NodeList和HTMLCollection MDN上的定义: NodeList: ...
- Redis学习笔记八:独立功能之二进制位数组
Redis 提供了 setbit.getbit.bitcount.bitop 四个命令用于处理二进制位数组. setbit 命令用于为位数组指定偏移量上的二进制位设置值,偏移量从 0 开始计数. ge ...