根据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 ...
随机推荐
- spring框架搭建url
MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建 http://blog.csdn.net/zhshulin/article/details/30779873 MyEclipse下 ...
- git之remote repository create(远程仓库创建)
参考:Git教程 - 廖雪峰的官方网站 1.在Git bash窗口执行如下指令创建SSH KEY: ssh-keygen -t rsa -C "sunjf@biomarker.com.cn& ...
- httpd服务访问控制
客户机地址限制 通过配置Order.Deny from.Allow from 来限制客户机 allow.deny :先"允许"后"拒绝" ,默认拒绝所有为明确的 ...
- javaweb框架构想-自己的对象存储池-遁地龙卷风
设计初衷: 网站在提供服务的过程中,会创建很多对象,bean,dao层的对象尤为频繁,然而这些对象是可以重复利用的.设计思路: 对象连接池ObjectPool才用单态的设计模式,自带线程,每隔一段时间 ...
- 自定义Listview
public class MyListView extends ListView { public MyListView(Context context) { super(context); } pu ...
- iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍
一.快速生成沙盒目录的路径 沙盒目录的各个文件夹功能 - Documents - 需要保存由"应用程序本身"产生的文件或者数据,例如:游戏进度.涂鸦软件的绘图 - 目录中的文件会被 ...
- Eclipse 语法提示
新建一个txt 拷贝下面的文本,然后保存修改扩展名为.epf #Sat Nov :: CST /instance/org.eclipse.jdt.core/org.eclipse.jdt.core.c ...
- API23时权限不被许可
In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Ins ...
- 把strassen乘法调出来了...
完美... 指针搞死我了 /// /// Author: zball /// No rights reserved /// (Creative Commons CC0) /// #include &l ...
- Extjs TabPanel 选项卡延迟加载
Extjs TabPanel 选项卡延迟加载 说明: Ext中用到tabpanel选项卡控件, 选项卡页签默认是延迟加载的, 当用户手工切换到某页签下时该页签才会加载, 在页签没有加载前, 用户对该页 ...