GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


A Paste by Anonymous
1
 
#ifndef _RESOURCEMGRT_H__
#define _RESOURCEMGRT_H__

///////////////////////////////////////////////////////////////////////

#include <string>
#include <map>
#include <vector>
#include "Singleton.h"

///////////////////////////////////////////////////////////////////////

template< class T >
class ResourceMgr : public Singleton< ResourceMgr<T> > {
private:
    std::map< std::string, T* > m_ResourceList;
    std::vector< std::string > m_SearchDirs;

public:
    ResourceMgr( bool defaultDir = true ) {
        if( defaultDir)
            m_SearchDirs.push_back( "" );
    }

    ~ResourceMgr() {
        Clear();
    }

    void Clear() {
        std::map< std::string, T* >::iterator it = m_ResourceList.begin();
        for( ; it != m_ResourceList.end(); ++it ) 
            delete (*it).second;
        m_ResourceList.clear();
    }

    T* GetResource( std::string& name ) {
        // Check if resource already exists
        if( m_ResourceList.find( name ) != m_ResourceList.end() )
            return m_ResourceList[name];
        T* temp = new T;
        if( m_SearchDirs.size() ) {
            std::vector<std::string>::iterator it = m_SearchDirs.begin();
            for( ; it != m_SearchDirs.end(); ++it) {
                if( temp->Load( *it, name ) ) {
                    m_ResourceList[name] = temp;
                    return m_ResourceList[name];
                }
            }
        }
        // Oops
        return 0;
    }

    void AddSearchDir( std::string& dir ) {
        m_SearchDirs.push_back( dir );
    }
};


///////////////////////////////////////////////////////////////////////

#endif