ResourceManager Class Reference

#include <ResourceManager.h>

List of all members.

Public Member Functions

 ResourceManager ()
 ~ResourceManager ()
IResourceAcquire (std::string key)
IResourceAcquireRandom (std::string type)
void Release (IResource *resource)
void LoadXMLFile (std::string filename, std::auto_ptr< IResourceFactory > factory)
void LogDebugInfo ()

Static Public Member Functions

static void Initialize ()
static void Shutdown ()

Static Public Attributes

static ResourceManagers_resourceManager = NULL

Private Member Functions

IResourceDoAcquireResource (ResourceEntry *entry)

Private Attributes

ResourceMap m_resources
ResourceTypeMap m_typeVectors


Detailed Description

Definition at line 20 of file ResourceManager.h.


Constructor & Destructor Documentation

ResourceManager::ResourceManager (  ) 

Definition at line 24 of file ResourceManager.cpp.

References Log::Message(), and Log::s_log.

Referenced by Initialize().

00024                                  {
00025   // Does anything need to happen here?
00026   Log::s_log->Message("Resource manager initialized");
00027 }

ResourceManager::~ResourceManager (  ) 

Definition at line 29 of file ResourceManager.cpp.

References m_resources, m_typeVectors, Log::Message(), ResourceEntry::resource, and Log::s_log.

00029                                   {
00030   ResourceEntry *entry;
00031   string key;
00032 
00033   m_typeVectors.clear();
00034 
00035   // Loop through all the resources, free them and the ResourceEntry the map was using
00036   while (!m_resources.empty()) {
00037     entry = m_resources.begin()->second;
00038     key = m_resources.begin()->first;
00039     delete entry->resource;
00040     m_resources.erase(key);
00041     delete entry;
00042   }
00043 
00044   if (!m_resources.empty()) {
00045     Log::s_log->Message("Warning: Could not empty resource map");
00046   }
00047 
00048   Log::s_log->Message("Resource manager shut down");
00049 }


Member Function Documentation

IResource * ResourceManager::Acquire ( std::string  key  ) 

Definition at line 51 of file ResourceManager.cpp.

References DoAcquireResource(), and m_resources.

00051                                               {
00052   ResourceEntry *entry;
00053 
00054   entry = m_resources[key];
00055   if (entry == NULL) {
00056     throw string("Key not found in resource map (" + key + ")");
00057   }
00058 
00059   return DoAcquireResource(entry);
00060 }

IResource * ResourceManager::AcquireRandom ( std::string  type  ) 

Definition at line 62 of file ResourceManager.cpp.

References DoAcquireResource(), IResource::GetKey(), m_typeVectors, Log::Message(), randInt(), ResourceEntry::resource, and Log::s_log.

00062                                                      {
00063   ResourceEntryVector resources;
00064   unsigned int index;
00065   ResourceEntry *entry;
00066 
00067   resources = m_typeVectors[type];
00068   if (resources.empty()) {
00069     throw string("Zero resources of type " + type + " found");
00070   }
00071 
00072   index = (unsigned int) randInt(resources.size() - 1);
00073   entry = resources[index];
00074   Log::s_log->Message("Got random resource: %s", entry->resource->GetKey().c_str());
00075 
00076   return DoAcquireResource(entry);
00077 }

IResource * ResourceManager::DoAcquireResource ( ResourceEntry entry  )  [private]

Definition at line 79 of file ResourceManager.cpp.

References IResource::Load(), ResourceEntry::refCount, and ResourceEntry::resource.

Referenced by Acquire(), and AcquireRandom().

00079                                                                   {
00080   // Check to see if this is the first use of this resource, load it if it is
00081   if (entry->refCount == 0) {
00082     entry->resource->Load();
00083   }
00084   // Increment the reference count
00085   ++entry->refCount;
00086 
00087   return entry->resource;
00088 }

void ResourceManager::Initialize (  )  [static]

Definition at line 141 of file ResourceManager.cpp.

References Log::Message(), ResourceManager(), Log::s_log, and s_resourceManager.

Referenced by main().

00141                                  {
00142   if (s_resourceManager != NULL) {
00143     Log::s_log->Message("Warning: Attempt to re-initialize resource manager");
00144     return;
00145   }
00146 
00147   s_resourceManager = new ResourceManager();
00148 }

void ResourceManager::LoadXMLFile ( std::string  filename,
std::auto_ptr< IResourceFactory factory 
)

Definition at line 106 of file ResourceManager.cpp.

References m_resources, m_typeVectors, Log::Message(), and Log::s_log.

Referenced by init().

00106                                                                                         {
00107   TiXmlElement *elem;
00108   TiXmlDocument doc( filename.c_str() );
00109 
00110   doc.LoadFile();
00111   if (doc.Error()) {
00112     // Should throw an exception
00113     Log::s_log->Message("Warning: Could not load %s. %s (line %d, column %d)", filename.c_str(), doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol());
00114     return;
00115   }
00116 
00117   for (elem = doc.FirstChildElement("Resources")->FirstChildElement("Resource"); elem; elem = elem->NextSiblingElement("Resource")) {
00118     ResourceEntry *entry = new ResourceEntry;
00119     entry->refCount = 0;
00120     try {
00121       entry->resource = factory->NewResource(elem);
00122       if (entry->resource == NULL) {
00123         throw string("Factory returned null pointer");
00124       }
00125       m_resources[entry->resource->GetKey()] = entry;
00126 
00127       m_typeVectors[entry->resource->GetType()].push_back(entry);
00128     } catch (string e) {
00129       delete entry;
00130       throw e;
00131     }
00132   }
00133 
00134   Log::s_log->Message("Resource file %s loaded", filename.c_str());
00135 }

void ResourceManager::LogDebugInfo (  ) 

Definition at line 137 of file ResourceManager.cpp.

References m_resources, Log::Message(), and Log::s_log.

00137                                    {
00138   Log::s_log->Message("%d total resources", m_resources.size());
00139 }

void ResourceManager::Release ( IResource resource  ) 

Definition at line 90 of file ResourceManager.cpp.

References IResource::GetKey(), m_resources, ResourceEntry::refCount, ResourceEntry::resource, and IResource::Unload().

Referenced by Ship::ReleaseResource(), CCFont::~CCFont(), SoundEffect::~SoundEffect(), and Texture::~Texture().

00090                                                  {
00091   ResourceEntry *entry;
00092 
00093   entry = m_resources[resource->GetKey()];
00094   if (entry == NULL) {
00095     throw string("Key not found in resource map (" + resource->GetKey() + ")");
00096   }
00097 
00098   // Decrement the reference count
00099   --entry->refCount;
00100   // Check to see if we are releasing the last reference to this resource, and unload it if we are
00101   if (entry->refCount == 0) {
00102     entry->resource->Unload();
00103   }
00104 }

void ResourceManager::Shutdown (  )  [static]

Definition at line 150 of file ResourceManager.cpp.

References Log::Message(), Log::s_log, and s_resourceManager.

Referenced by prepExit().

00150                                {
00151   if (s_resourceManager == NULL) {
00152     Log::s_log->Message("Warning: Attempt to re-shutdown resource manager");
00153     return;
00154   }
00155   delete s_resourceManager;
00156   s_resourceManager = NULL;
00157 }


Member Data Documentation

ResourceMap ResourceManager::m_resources [private]

Definition at line 42 of file ResourceManager.h.

Referenced by Acquire(), LoadXMLFile(), LogDebugInfo(), Release(), and ~ResourceManager().

ResourceTypeMap ResourceManager::m_typeVectors [private]

Definition at line 43 of file ResourceManager.h.

Referenced by AcquireRandom(), LoadXMLFile(), and ~ResourceManager().

ResourceManager * ResourceManager::s_resourceManager = NULL [static]

Definition at line 37 of file ResourceManager.h.

Referenced by Ship::AcquireResource(), CCFont::CCFont(), init(), Initialize(), Ship::ReleaseResource(), Ship::Ship(), Shutdown(), SoundEffect::SoundEffect(), Texture::Texture(), CCFont::~CCFont(), SoundEffect::~SoundEffect(), and Texture::~Texture().


The documentation for this class was generated from the following files:
Generated on Mon Jan 8 22:34:15 2007 for CrownandCutlass by  doxygen 1.4.7