½º¸¶Æ®Æ÷ÀÎÅÍ

1. ½º¸¶Æ®Æ÷ÀÎÅ͸¦ ÀÌ¿ëÇÏ¸é °´Ã¼ÀÇ °øÀ¯, Ä«¿îÆ® °ü¸®, Æ÷ÀÎÅ͸¦ °ü¸® Çϴµ¥ ÀÖ¾î Æí¸®ÇÏ´Ù.

±¸ÇöµÈ ¼Ò½º¸¦ °¡Áö°í »ç¿ë ¹æ¹ý°ú ½ÇÇà °á°ú¸¦ ¸ÕÀú ¼³¸íÇÑ´Ù.

#include <iostream>

#include "MyRTTI.h"

#include "MyRefObj.h"

 

class   CCar : public   MyRefObj

{

    MyDeclareRTTI;

 

public:

    void Show() {    std::cout << "This is Car." << std::endl;  }

};

 

MySmartPointer( CCar );

MyImplementBaseRTTI(CCar);

 

void main()

{

    CCarPtr spCar1;

    {

        CCarPtr spCar2;

        {

            CCarPtr spCar = new CCar;  //spCarÀÇ ÂüÁ¶ Ä«¿îÅÍ 1

            spCar1 = spCar;   //ÂüÁ¶ Ä«¿îÅÍ 2

            spCar1 = 0;       //ÂüÁ¶ Ä«¿îÅÍ 1

            spCar2 = spCar;   //ÂüÁ¶ Ä«¿îÅÍ 2

            spCar = 0;        //ÂüÁ¶ Ä«¿îÅÍ 1

        }

    }

}   

<<½º¸¶ÅÍ Æ÷ÀÎÅ͸¦ »ç¿ë ¹æ¹ý>>

1. ÂüÁ¶ °¹¼ö¸¦ °ü¸®ÇÏ´Â MyRefObj Ŭ·¡½º¸¦ »ó¼ÓÇÑ´Ù.

2. ½º¸¶ÅÍ Æ÷ÀÎÅ͸¦ ¸ÅÅ©·Î¸¦ ÀÌ¿ëÇØ ½º¸¶ÅÍ Æ÷ÀÎÅÍ Å¬·¡½º¸¦ ¼±¾ðÇÑ´Ù.

MySmartPointer(  CCar )

MySmartPointer()¾È¿¡ »ç¿ë ÇÏ°íÀÚ Çϴ Ŭ·¡½º À̸§À» ³Ö´Â´Ù.

<<½ÇÇà °á°ú>>

À§ÀÇ ¼Ò½º¿¡¼­ spCarÀÇ ÂüÁ¶ Ä«¿îÅÍ´Â ÁÖ¼®°ú °°´Ù.

°´Ã¼ »ý¼º½Ã¿¡ 1,
´Ù¸¥ Æ÷ÀÎÅÍ¿¡ ÂüÁ¶ µÉ ¶§ 2,
´Ù¸¥ Æ÷ÀÎÅÍ¿¡ 0À» ³ÖÀ¸¸é 1
spCar = 0À» ½ÇÇàÇÑÈÄ spCar2¿¡ Æ÷ÀÎÅÍ´Â °è¼Ó »ì¾Æ ÀÖ´Ù.
spCar2°¡ spCar¸¦ ÂüÁ¶ Ç߱⠶§¹®¿¡, ½ÇÁ¦ µ¥ÀÌÅÍ´Â °è¼Ó À¯Áö µÇ´Â °ÍÀÌ´Ù.

½º¸¶ÅÍ Æ÷ÀÎÅÍÀÇ µ¥ÀÌÅÍ´Â ÂüÁ¶ Ä«¿îÅÍ°¡ 0ÀÎ °æ¿ì °´Ã¼ »èÁ¦°¡ ÀÏ¾î ³­´Ù.
½º¸¶ÅÍ Æ÷ÀÎÅÍÀÇ µ¥ÀÌÅ͸¦ Áö¿ì±â À§ÇØ delete¸¦ ½ÇÇà Çϸé, °´Ã¼ÀÇ »èÁ¦´Â »èÁ¦ ÇÏÁö ¾ÊÀ¸´Ï Á¶½ÉÇϱ⠹ٶõ´Ù.

±¸Â÷´Ï·Î ÀÎÇØ ÀϺΠ¼Ò½º¸¸ ¸®½ºÆ®·Î ¿Ã¸°´Ù.

//MyRefObj.h

#ifndef __MYREFOBJ_H

#define __MYREFOBJ_H

 

#include "MyRTTI.h"

#include "MySmartPointer.h"

 

class MyRefObj

{

    MyDeclareRTTI;

 

public:

    virtual ~MyRefObj ();

 

    void    IncRef();

    void    DecRef();

    int     GetRefCount();

    static unsigned int GetTotalObject();

 

protected:

    // construction (abstract base class)

    MyRefObj();

 

    int                        m_iRef;

    static unsigned int        ms_uiTotalObject;

};

 

MySmartPointer( MyRefObj );

 

//----------------------------------------------------------------------------

inline void MyRefObj::IncRef()

{

    m_iRef++;

}

//----------------------------------------------------------------------------

inline void MyRefObj::DecRef()

{

    m_iRef--;

}

//----------------------------------------------------------------------------

inline int MyRefObj::GetRefCount()

{

    return m_iRef;

}

 

 

#endif

 

//MySmartPointer.h

 

#ifndef __MYSMARTPOINTER_H

#define __MYSMARTPOINTER_H

 

#include "MySmartPointerMacro.h"

 

 

template <class T>

class MyPointer

{

public:

    // construction and destruction

    MyPointer (T* pkObject = 0);

    MyPointer (const MyPointer& rkPointer);

    ~MyPointer ();

 

    // implicit conversions

    operator T* () const;

    T& operator* () const;

    T* operator-> () const;

 

    // assignment

    MyPointer& operator= (const MyPointer& rkReference);

    MyPointer& operator= (T* pkObject);

 

    // comparisons

    bool operator== (T* pkObject) const;

    bool operator!= (T* pkObject) const;

    bool operator== (const MyPointer& rkReference) const;

    bool operator!= (const MyPointer& rkReference) const;

 

protected:

    void DecRefObject();

 

protected:

    // the shared object

    T* m_pkObject;

};

 

#include "MySmartPointer.inl"

 

#endif

 

 

// Magic Software, Inc.

// http://www.magic-software.com

// Copyright (c) 2000, All Rights Reserved

//

// Source code from Magic Software is supplied under the terms of a license

// agreement and may not be copied or disclosed except in accordance with the

// terms of that agreement.  The various license agreements may be found at

// the Magic Software web site.  This file is subject to the license

//

// RESTRICTED USE SOURCE CODE

// http://www.magic-software.com/License/restricted.pdf

 

//---------------------------------------------------------------------------

template <class T>

inline MyPointer<T>::MyPointer (T* pkObject)

{

    m_pkObject = pkObject;

    if ( m_pkObject )

        m_pkObject->IncRef();

}

//---------------------------------------------------------------------------

template <class T>

inline MyPointer<T>::MyPointer (const MyPointer& rkPointer)

{

    m_pkObject = rkPointer.m_pkObject;

    if ( m_pkObject )

        m_pkObject->IncRef();

}

//---------------------------------------------------------------------------

template <class T>

inline MyPointer<T>::~MyPointer ()

{

    DecRefObject();

}

//---------------------------------------------------------------------------

template <class T>

inline void MyPointer<T>::DecRefObject()

{

    if ( m_pkObject )

    {

        m_pkObject->DecRef();

        if(m_pkObject->GetRefCount() == 0)

        {

            delete m_pkObject;

            m_pkObject = NULL;

        }

    }

}

//---------------------------------------------------------------------------

template <class T>

inline MyPointer<T>::operator T* () const

{

    return m_pkObject;

}

//---------------------------------------------------------------------------

template <class T>

inline T& MyPointer<T>::operator* () const

{

    return *m_pkObject;

}

//---------------------------------------------------------------------------

template <class T>

inline T* MyPointer<T>::operator-> () const

{

    return m_pkObject;

}

//---------------------------------------------------------------------------

template <class T>

inline MyPointer<T>& MyPointer<T>::operator= (const MyPointer& rkPointer)

{

    if ( m_pkObject != rkPointer.m_pkObject )

    {

        DecRefObject();

        m_pkObject = rkPointer.m_pkObject;

        if ( m_pkObject )

            m_pkObject->IncRef();

    }

    return *this;

}

//---------------------------------------------------------------------------

template <class T>

inline MyPointer<T>& MyPointer<T>::operator= (T* pkObject)

{

    if ( m_pkObject != pkObject )

    {

        DecRefObject();

        m_pkObject = pkObject;

        if ( m_pkObject )

            m_pkObject->IncRef();

    }

    return *this;

}

//---------------------------------------------------------------------------

template <class T>

inline bool MyPointer<T>::operator== (T* pkObject) const

{

    return ( m_pkObject == pkObject );

}

//---------------------------------------------------------------------------

template <class T>

inline bool MyPointer<T>::operator!= (T* pkObject) const

{

    return ( m_pkObject != pkObject );

}

//---------------------------------------------------------------------------

template <class T>

inline bool MyPointer<T>::operator== (const MyPointer& rkPointer) const

{

    return ( m_pkObject == rkPointer.m_pkObject );

}

//---------------------------------------------------------------------------

template <class T>

inline bool MyPointer<T>::operator!= (const MyPointer& rkPointer) const

{

    return ( m_pkObject != rkPointer.m_pkObject );

}

//---------------------------------------------------------------------------

 

//MySmartPointerMacro.h

#ifndef __MYSMARTPOINTERMACRO_H

#define __MYSMARTPOINTERMACRO_H

 

#define MySmartPointer(classname) \

    class classname; \

    typedef MyPointer<classname> classname##Ptr

 

// cast a smart pointer of one type to a pointer of another type.

#define MySmartPointerCast(type,smartptr) ((type*)(void*)(smartptr))

 

#endif

 

¼Ò½º : SmartPointer.zip

ÂüÁ¶:  3D Game Engine Design, ÀúÀÚ: David H.Eberly