【OSG】超级指针

前言

当你申请一个资源的时候,当你使用超级指针来管理这个资源的时候,你不必关心它何时被释放,如何被释放;在这里推荐一个文档辅助大家理解《OpenSceneGraph基本渲染理论》,这个文档百度很多免费的可以阅读。

思想

使用栈中的资源管理堆中的东西

实现代码1

OSG环境已经配置好的基础上,代码只是为了让大家理解超级指针这个概念,有很多代码的地方还是不太恰当,比如友元类的使用上,大家不必细究。

#include 
#include 
#include 

#ifdef _DEBUG
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgViewerd.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgUtil.lib")
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "OpenThreads.lib")
#endif

class   Real 
{
public:
    Real()
    {
        count = 0;
        std::cout<<"Real has create"<<std::endl;
    }
    ~Real()
    {
        std::cout<<"Real has Destroy"<<std::endl;
    }
    void ref()
    {
        count ++;
    }
    void unref()
    {
        count --;
        if(!count)
        {
            delete(this);
        }
    }
private:
    int count;
};

template <class T>
class ref_pter
{
public:
    ref_pter()
    {
        _ptr = NULL;
    }

    ref_pter(T *ptr)
    {
        _ptr = ptr;
        if(_ptr )
        {
            _ptr->ref();
        }
    }

    ~ref_pter()
    {
        if(_ptr)
        {
            _ptr->unref();
        }
    }
    ref_pter operator =  (T *pa)
    {
        T * temp = _ptr;
        _ptr = pa;
        if(_ptr)
        {
            _ptr->ref();
        }
        if(temp)
        {
            temp->unref();
        }
        return * this;
    }
    T* get()
    {
        return _ptr;
    }

    operator T *() const
    {
     	return _ptr;
    }  
    T* operator ->()
    {
        return _ptr;
    }
    T* operator *()
    {
        return _ptr;
    }
private:
    T * _ptr;
};

void main()
{
   ref_pter<osgViewer::Viewer> viewer = new osgViewer::Viewer;
   viewer->setSceneData(osgDB::readNodeFile("glider.osg"));
    viewer->run();
}

实现代码2

#include 
#include 
#include 

#ifdef _DEBUG
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgViewerd.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgUtil.lib")
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "OpenThreads.lib")
#endif

class   Real 
{
public:
    Real()
    {
        count = 0;
        std::cout<<"Real has create"<<std::endl;
    }
    ~Real()
    {
        std::cout<<"Real has Destroy"<<std::endl;
    }
    void ref()
    {
        count ++;
    }
    void unref()
    {
        count --;
        if(!count)
        {
            delete(this);
        }
    }
public:
    int count;
};

template <class T>
class ref_pter
{
public:
    ref_pter()
    {
        _ptr = NULL;
    }

    ref_pter(T *ptr)
    {
        _ptr = ptr;
        if(_ptr )
        {
            _ptr->ref();
        }
    }
    ~ref_pter()
    {
        if(_ptr)
        {
            _ptr->unref();
        }
    }
    ref_pter operator =  (T *pa)
    {
        T * temp = _ptr;
        _ptr = pa;
        if(_ptr)
        {
            _ptr->ref();
        }
        if(temp)
        {
            temp->unref();
        }
        return * this;
    }
    T* get()
    {
        return _ptr;
    }

    operator T *() const
    {

        return _ptr;
    }

    T* operator ->()
    {
        return _ptr;
    }
    T* operator *()
    {
        return _ptr;
    }
private:
    T * _ptr;
};

void main()
{
    ref_pter<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    viewer->setSceneData(osgDB::readNodeFile("glider.osg"));
    viewer->run();
}

*** 备注:其中的ref()函数和unref函数,都是osg本身的函数,所以名字只能叫这个,如果改一下函数名可能就会有错误,具体深入的了解,还要看一下OpenSceneGraph的源码。

你可能感兴趣的:(OpenSceneGraph)