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简化的智能指针的更多相关文章

  1. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  2. 不可不表的OSG智能指针之强指针与弱指针 《转载》

    不可不表的OSG智能指针之强指针与弱指针 <转载> 使用OSG的人都知道OSG的内存管理方式采用了智能指针,通过智能指针的方式让OSG自己处理对象的销毁工作.在OSG中有两个智能指针类型, ...

  3. C++中智能指针的设计和使用

    转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/7561235 智能指针(smart pointer)是存储指向动态分配(堆 ...

  4. 智能指针 与 oc中的指针

     智能指针 与 oc中的指针 智能指针的原理及实现 当类中有指针成员时,一般有两种方式来管理指针成员:一是采用值型的方式管理,每个类对象都保留一份指针指向的对象的拷贝:另一种更优雅的方式是使用智能指针 ...

  5. OSG中的HUD

    OSG中的HUD 所谓HUD节点,说白了就是无论三维场景中的内容怎么改变,它都能在屏幕上固定位置显示的节点. 实现要点: 关闭光照,不受场景光照影响,所有内容以同一亮度显示 关闭深度测试 调整渲染顺序 ...

  6. osg中使用MatrixTransform来实现模型的平移/旋转/缩放

    osg中使用MatrixTransform来实现模型的平移/旋转/缩放 转自:http://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html# ...

  7. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...

  8. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...

  9. OSG中相机参数的更改

    #pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...

随机推荐

  1. Mac安装OpenCV

    安装过程参考这篇文章Mac平台上OpenCV开发环境搭建 也可以参考文档官网上的安装文档Installation in Linux(不知道为什么没有Installation in Mac...) 我的 ...

  2. qstring与char*、基本数据类型的转换

    1.qstring转化为char* QString.toStdString.c_str() 2.char*转化为QString str = QString(QLatin1String(mm)); 3. ...

  3. 解决IE6不支持position:fixed的bug

    /*完整代码 */ /* 除IE6浏览器的通用方法 */ .ie6fixedTL { position: fixed; left:; top:; } .ie6fixedBR { position: f ...

  4. 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)

    1. 打开Eclipse,右键New JBPM Project 项目,在项目名称里面填写一个项目名字,如“TestJbpm6”,然后下一步,知道Finish完成(里面会使用我们配置的运行环境). 2. ...

  5. [lintcode] Binary Tree Maximum Path Sum II

    Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...

  6. NOIP2016题目整合

    今天终于拿到官方数据,兴致勃勃地全 A 了. Day 1 T1 toy 处理一下正负号加加减减取模乱搞就好了. #include <iostream> #include <cstdi ...

  7. ios中二维码的使用之一: 二维码的生成

    iOS在7之后,具备了原生的二维码生成API; 生成二维码的准备:  #import <CoreImage/CoreImage.h> 导入框架: 开始生成: //1- 创建过滤器 CIFi ...

  8. 一起入门python5之for循环

    昨天中午本来写了的,结果手贱了一下ctrl+x以后又去复制了别的东西.结果所有写的都没有了.蛋疼.继续写吧.今天来说for循环即条件判断>>> age = 20        #首先 ...

  9. Android中的四种动画(一)

    TweenAnimation 变换动画(也叫作"补间动画"),有四种(alpha scale translate rote). FrameAnimation(也叫DrawableA ...

  10. PHP开发神器——phpstorm

    常用快捷键 快捷键 说明 ctrl+j 插入活动代码提示 ctrl+alt+t 当前位置插入环绕代码 alt+insert 生成代码菜单 Shift + Enter 新一行 ctrl+q 查看代码注释 ...