Core Resource Manager - (Game Resource Management)

Core Resource Manager - (Game Development Resource Management)

include articles:
Definition: CORE RESOURCE MANAGER:

class CoreResMgr
{
    static ds::dlist< res::Manager* > managers;
    static short instances;
    
public:
    
    template <class Asset>  static ResMgr< Asset > * CreateResMgr(res::ResLoader< Asset > * loader)
    {
        static ResMgr< Asset > * manager = nullptr;
        if (manager == nullptr)
        {
            manager = new ResMgr< Asset >(loader);
            managers.push_back( manager );
        }
        return manager;
    }
    
    template <class Asset>  static void DestoryResMgr(ResMgr< Asset > * mgr)
    {
        for (ds::dlist< res::Manager* >::iterator i=managers.begin(); i!=nullptr; i++)
        {
            if (mgr == i.element())
            {
                i->Destroy();
                managers.erase(i);
                break;
            }
        }
    }
    
    template <class Asset>  static ResMgr< Asset > * GetResMgr()
    {
        return  CreateResMgr< Asset >(nullptr);
    }
    
    static void Destroy();
    virtual ~ CoreResMgr();
    CoreResMgr();
};

LoaderContext Member functions:


void Destroy();
This will destroy all manager every created using CoreResMgr.


void CoreResMgr::Destroy()
{
    for (ds::dlist< res::Manager* >::iterator i=managers.begin(); i!=nullptr; i++)
    {
i->Destroy();
delete i.element();
} managers.clear(); }


Constructor:

CoreResMgr();
overtime an instances of CoreResMgr is created the instance counter in increased. each destruction of a CoreResMgr decrease the instance counter. 


CoreResMgr::CoreResMgr()
{
    instances++;
}

Destructor:

~CoreResMgr();
once instances is less than or equal to zero, this indicate this there is no instances of CoreResMgr thus it must being deleted, so all managers must destroy and release memory.


CoreResMgr:: ~ CoreResMgr()
{
    instances--;
    if (instances <= 0)
    {
        Destroy();
    }
}


Declaration of static variable in .cpp file.

ds::dlist< res::Manager* > CoreResMgr::managers;
short CoreResMgr::instances = 0;


Next Article -> Using CoreResMgr and ResMgr and ResLoader - (Game Development Resource Management)

Comments