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. nyoj 4 779 兰州烧饼

    兰州烧饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一 ...

  2. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  3. Mac Pro 安装 Adobe Photoshop CC for mac V2014 破解版

    一.下载 Photoshop CC for mac V2014 原版(.dmg 文件): 百度网盘下载1 百度网盘下载2 百度网盘下载3 百度网盘下载4 百度网盘下载5 百度网盘下载6 百度网盘下载7 ...

  4. 索 COM 类工厂中 CLSID 为{00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。

    具体解决方法如下: 1:在服务器上安装office的Excel软件. 2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务&q ...

  5. C\C++ 字符串的格式化与类型转化

    字符串格式化 1.sscanf int sscanf(const char *buffer,const char *format,[argument ]...) 取到指定字符为止的字符串.如在下例中, ...

  6. linux 模块加载

    错误: rmmod 时提示 rmmod: chdir(xxx): No such file or directory 解决方法: http://blog.csdn.net/luckywang1103/ ...

  7. 网络编程2-UDP编程(DatagramSocket)

    1.传输层有两个协议,一个是tcp协议,另一个是udp协议,tcp协议通过socket编程.udp通过数据报编程. UDP协议: (1)将数据.源地址.目的地址 封装成数据包,不需要建立链接 (2)每 ...

  8. NOIP2016题目整合

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

  9. iOS 容器 addChildViewController

    //在parent view controller 中添加 child view controller FirstViewController *firstViewController=[[First ...

  10. Python自动化之语法基础

    1 第一个程序 hello world 在Linux环境下执行 vim hello.py #!/usr/bin/env python #指定解释器 print("hello world&qu ...