/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */

#ifndef KEOS_SMARTPTRPOLICIES_H
#define KEOS_SMARTPTRPOLICIES_H

#include "KeosPrerequisites.h"

namespace Keos
{
    //-----------------------------------------------------------------------
	/** Implementation of the OwnershipPolicy used by CSmartPtr
     *  Provides a classic external reference counting implementation.
     */
    template <class T>
    class CRefCount
    {
    public :

        /** Default constructor.
         *
         */
        CRefCount() : m_pCounter(new int(1))
        {
        }

        /** Clone the resource.
         *
         */
        T* Clone(T* Ptr)
        {
            ++*m_pCounter;
            return Ptr;
        }

        /** Manage the liberation of the resource.
         *
         */
        void Release(T* pPtr)
        {
            if (--*m_pCounter == 0)
            {
                delete m_pCounter;
                delete pPtr;
            }
        }

        /** Swap two instances.
         *
         */
        void Swap(CRefCount& RefCount)
        {
            std::swap(RefCount.m_pCounter, m_pCounter);
        }

    private :

        //! Reference counter
        int* m_pCounter;
    };

   
    //-----------------------------------------------------------------------
	/** Implementation of the OwnershipPolicy used by CSmartPtr
     *  Adapts COM intrusive reference counting to OwnershipPolicy-specific syntax.
     */
    template <class T>
    class CCOMRefCounted
    {
    public :

        /** Clone the resource.
         *
         */
        static T* Clone(T* pPtr)
        {
            if (pPtr)
                pPtr->AddRef();
            return pPtr;
        }

        /** Manage the liberation of the resource.
         *
         */
        static void Release(T* pPtr)
        {
            if (pPtr)
                pPtr->Release();
        }

        /** Swap two instances.
         *
         */
        static void Swap(CCOMRefCounted&)
        {
        }
    };

}; // namespace Keos

#endif // KEOS_SMARTPTRPOLICIES_H
