/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     objectConstructor.h

  Copyright (C)2007-2009 Nintendo  All Rights Reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law. They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Revision: 1758 $
 *-------------------------------------------------------------------------*/

#ifndef NTMV_M2D_OBJECTCONSTRUCTOR_H_
#define NTMV_M2D_OBJECTCONSTRUCTOR_H_

#if _MSC_VER > 1000 || defined(__CWCC__)
#pragma once
#endif

#include <nnsys/fnd.h>
#include <ntmv/m2d/types.h>
#include <new>

namespace ntmv
{
namespace m2d
{

class ObjectConstructor
{
public:
                        ObjectConstructor(NNSFndAllocator* pAllocator)  { mpAllocator = pAllocator; }

    NNSFndAllocator*    GetAllocator() const    { return mpAllocator; }

    void*               AllocMemory(u32 size) const
    {
        void *const pMem = NNS_FndAllocFromAllocator(mpAllocator, size);
        NNS_WARNING(pMem, "can't alloc memory.");
        return pMem;
    }

    void                FreeMemory(void* mem) const { NNS_FndFreeToAllocator(mpAllocator, mem); }

    template<typename T>
    T*                  NewObj() const;

    template<typename T, typename Param1>
    T*                  NewObj(Param1 param1) const;

    template<typename T, typename Param1, typename Param2>
    T*                  NewObj(Param1 param1, Param2 param2) const;

    template<typename T, typename Param1, typename Param2, typename Param3>
    T*                  NewObj(Param1 param1, Param2 param2, Param3 param3) const;

    template<typename T, typename Param1, typename Param2, typename Param3, typename Param4>
    T*                  NewObj(Param1 param1, Param2 param2, Param3 param3, Param4 param4) const;

    template<typename T>
    T*                  NewArray(u32 num) const;

    template<typename T>
    void                DeleteObj(T* pObj) const;

    template<typename T>
    void                DeleteArray(T objAry[], u32 num) const;

    template<typename T>
    void                DeletePrimArray(T objAry[]) const;

private:
    NNSFndAllocator*    mpAllocator;
};

template<typename T>
T*
ObjectConstructor::NewObj() const
{
    if (void* pMem = AllocMemory(sizeof(T)))
    {
        return new (pMem) T();
    }
    return 0;
}

template<typename T, typename Param1>
T*
ObjectConstructor::NewObj(Param1 param1) const
{
    if (void* pMem = AllocMemory(sizeof(T)))
    {
        return new (pMem) T(param1);
    }
    return 0;
}

template<typename T, typename Param1, typename Param2>
T*
ObjectConstructor::NewObj(Param1 param1, Param2 param2) const
{
    if (void* pMem = AllocMemory(sizeof(T)))
    {
        return new (pMem) T(param1, param2);
    }
    return 0;
}

template<typename T, typename Param1, typename Param2, typename Param3>
T*
ObjectConstructor::NewObj(Param1 param1, Param2 param2, Param3 param3) const
{
    if (void* pMem = AllocMemory(sizeof(T)))
    {
        return new (pMem) T(param1, param2, param3);
    }
    return 0;
}

template<typename T, typename Param1, typename Param2, typename Param3, typename Param4>
T*
ObjectConstructor::NewObj(Param1 param1, Param2 param2, Param3 param3, Param4 param4) const
{
    if (void* pMem = AllocMemory(sizeof(T)))
    {
        return new (pMem) T(param1, param2, param3, param4);
    }
    return 0;
}

template<typename T>
T*
ObjectConstructor::NewArray(u32 num) const
{
    const u32 allocNum = num == 0 ? 1: num; // To avoid 0-byte requests
    void* pMem = AllocMemory(sizeof(T) * allocNum);
    if (! pMem)
    {
        return 0;
    }

    T *const objAry = static_cast<T*>(pMem);

    for (u32 i = 0; i < num; ++i)
    {
        new (&objAry[i]) T();
    }

    return objAry;
}

template<typename T>
void
ObjectConstructor::DeleteObj(T* pObj) const
{
    if (pObj)
    {
        pObj->Finalize(*this);
        FreeMemory(pObj);
    }
}

template<typename T>
void
ObjectConstructor::DeleteArray(T objAry[], u32 num) const
{
    if (objAry)
    {
        for (u32 i = 0; i < num; ++i)
        {
            objAry[i].Finalize(*this);
        }
        FreeMemory(objAry);
    }
}

template<typename T>
void
ObjectConstructor::DeletePrimArray(T objAry[]) const
{
    if (objAry)
    {
        FreeMemory(objAry);
    }
}

}   // namespace m2d
}   // namespace ntmv

/* NTMV_M2D_OBJECTCONSTRUCTOR_H_ */
#endif
